KEMBAR78
Make `None` compatible with `SupportsStr` protocol by sobolevn · Pull Request #13184 · python/mypy · GitHub
Skip to content

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Jul 19, 2022

Previously we had a special case for None and __hash__, but it can be extened for __str__ as well.

>>> str(None)
'None'

In the future we can change all our None hacks to use NoneType, but not today :)

Closes #13081

@sobolevn sobolevn requested a review from JukkaL July 19, 2022 08:27
@github-actions

This comment has been minimized.

mypy/subtypes.py Outdated
# slightly sloppy since we don't check the signature of "__hash__".
return not members or members == ["__hash__"]
# None is also compatible with `SupportsStr` protocol.
supported_members = frozenset(("__hash__", "__str__"))
Copy link
Member

Choose a reason for hiding this comment

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

Not sure how important it is to micro-optimize this, but maybe move the frozenset into a global constant?

Copy link
Member Author

@sobolevn sobolevn Jul 19, 2022

Choose a reason for hiding this comment

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

I was unable to find a good global name for this 😆
This is why I went for a local variable. If mypyc can somehow optimize this to a noticable difference, I have no problem in moving it to a global namespace.

Copy link
Collaborator

Choose a reason for hiding this comment

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

For mypyc the fastest thing could be to inline the members check -- i.e. all(member in ("__hash__", "__str__") for member in members) -- since that gets optimized to two string equality checks (but it's not a big deal).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JukkaL JukkaL merged commit fa7bdd1 into master Jul 20, 2022
@JukkaL JukkaL deleted the issue-13081 branch July 20, 2022 14:54
@JukkaL
Copy link
Collaborator

JukkaL commented Jul 20, 2022

Thanks!

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.

None incompatible with SupportsStr

3 participants