Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use _typeshed.NoneType for the type of None #11287

Closed
KotlinIsland opened this issue Oct 7, 2021 · 6 comments · Fixed by #11290
Closed

Use _typeshed.NoneType for the type of None #11287

KotlinIsland opened this issue Oct 7, 2021 · 6 comments · Fixed by #11290
Labels
bug mypy got something wrong

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Oct 7, 2021

Currently mypy uses a hardcoded type for NoneType, I think it should use the one from typeshed.

reveal_type(None.__bool__())  # note: Revealed type is "builtins.bool"
@KotlinIsland KotlinIsland added the bug mypy got something wrong label Oct 7, 2021
@erictraut
Copy link

This is defined correctly in typeshed.

@final
class NoneType:
    def __bool__(self) -> Literal[False]: ...

I'm not sure why mypy is revealing bool rather than Literal[False] in this case. Pyright reveals Literal[False] based on the above typeshed definition.

It's possible this was fixed recently in typeshed and mypy hasn't yet picked up the latest version.

@JelleZijlstra
Copy link
Member

NoneType exists only in 3.10; I don't think mypy uses it to define None. It's hardcoded here instead: https://github.com/python/mypy/blob/master/mypy/checkmember.py#L319.

@erictraut
Copy link

NoneType has been defined in _typeshed.pyi for a while, and pyright uses its definition to avoid hardcoding assumptions. Mypy's logic probably predates that.

@KotlinIsland
Copy link
Contributor Author

python/typeshed#6125

@KotlinIsland KotlinIsland changed the title None.__bool__() is bool not False Use _typeshed.NoneType for the type of None Oct 7, 2021
@KotlinIsland
Copy link
Contributor Author

KotlinIsland commented Oct 11, 2021

@JelleZijlstra I'm interested in getting mypy to use _typeshed.NoneType as the type of None, is this what you would want?

Where should I look, I can see that there is a mypy.types.NoneType.

I'm not sure how classes are represented, would this need to be replaced with a TypeInfo of _typeshed.NoneType?

@JelleZijlstra
Copy link
Member

Yes, I think you'd have to replace mypy.types.NoneType. Honestly this seems like a lot of refactoring work for little reward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants