Skip to content

The object None works but the type NoneType doesn't work #20218

@hyperkai

Description

@hyperkai

I used the object None, then error doesn't occur but the readability isn't good not to differentiate the type None and the default value None as shown below:

*Memo:

  • mypy 1.18.2
  • Windows 11
                   # Cannot differenciate
                   # ↓↓↓↓   ↓↓↓↓
def func(name: str | None = None) -> str:
    return f'My name is {name}.'

So, I used the type NoneType to improve the readability to differentiate the type NoneType and the default value None but the error occurs as shown below:

from types import NoneType

                   # Can differenciate
                   # ↓↓↓↓↓↓↓↓   ↓↓↓↓
def func(name: str | NoneType = None) -> str:
    return f'My name is {x}.'

$ mypy test.py
test.py:5: error: NoneType should not be used as a type, please use None instead [valid-type]
Found 1 error in 1 file (checked 1 source file)

Actually, it's not reasonable that the object None works but the type NoneType doesn't work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions