-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Make None
compatible with SupportsStr
protocol
#13184
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
Conversation
This comment has been minimized.
This comment has been minimized.
mypy/subtypes.py
Outdated
@@ -239,7 +239,9 @@ def visit_none_type(self, left: NoneType) -> bool: | |||
members = self.right.type.protocol_members | |||
# None is compatible with Hashable (and other similar protocols). This is | |||
# 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__")) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
This comment has been minimized.
This comment has been minimized.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Thanks! |
Previously we had a special case for
None
and__hash__
, but it can be extened for__str__
as well.In the future we can change all our
None
hacks to useNoneType
, but not today :)Closes #13081