Skip to content

Now type(None) is more consistent, refs #11550 - #11552

Closed
sobolevn wants to merge 2 commits into
python:masterfrom
sobolevn:issue-11550
Closed

sobolevn wants to merge 2 commits into
python:masterfrom
sobolevn:issue-11550

Conversation

@sobolevn

@sobolevn sobolevn commented Nov 14, 2021 •

Copy link
Copy Markdown
Member

Closes #11550

This might fail in some unexpected cases.

Comment thread mypy/semanal.py
if self.is_none_alias(rvalue):
# This is really `Type[None]`, not just `None`, but we use
# this as a backwards compatible solution. See #11550
res = NoneType()

@sobolevn sobolevn Nov 14, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can still cause issues, for example:

none = type(None)

print(none())  # E: error: Cannot instantiate type "Type[None]"

But, I've decided to keep this as-is (and do not change this to res = TypeType(NoneType())), because I'm affraid this can cause issues to our old users, who use type(None) for annotations.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we decide to change this, we can try to make Type[None] and None compatible. So, this will work:

n = type(None)
var: n = None  # will still be ok!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think types that are none should be annotated None. If someone tries to annotate something that is none as type(None) we should give an error and tell them to annotate it as : None (though I also expect this to be rather rare).

@sobolevn

Copy link
Copy Markdown
Member Author

Related #11551

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

reveal_type(type(None)) is object

2 participants