-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Make None
compatible with SupportsBool
protocol
#15889
base: master
Are you sure you want to change the base?
Conversation
Does this check the return type? |
@TeamSpen210 Hi! No, this changes only add supports "Boolable" protocols for |
This comment has been minimized.
This comment has been minimized.
I mean if you create a |
|
@@ -2890,6 +2890,12 @@ class SupportsStr(Protocol): | |||
def ss(s: SupportsStr) -> None: pass | |||
ss(None) | |||
|
|||
class SupportsBool(Protocol): | |||
def __bool__(self) -> bool: ... |
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.
Please, add two more protocols to the test: def __bool__(self) -> Literal[True]: ...
and def __bool__(self) -> Literal[False]: ...
Other than that - it looks good to me.
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.
Thanks for review! I add tests with Literal
return types, take a look, please
4ef2523
to
ef1f9cc
Compare
|
||
def sb(s: SupportsBool) -> None: pass | ||
sb(None) | ||
def sblt(s: SupportsBoolLiteralTrue) -> None: pass |
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 am not sure that this is correct. https://github.com/python/typeshed/blob/ef758b66c0947a73f6225cac0568f3f068cd61d4/stdlib/types.pyi#L627-L629
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.
Yes, you're right, it's not correct behavior, but tests passed. How I can fix it? Maybe we have other bug?
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.
None
is quite complicated, because mypy uses pre-types.NoneType
logic for None
.
Check out checkmember
and NoneType
handling there.
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'm implemented check into visit_none_type
method, because __bool__
of Protocol not handled by analyze_none_member_access
This comment has been minimized.
This comment has been minimized.
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
# None is also compatible with `SupportsStr` protocol. | ||
# None is also compatible with `SupportsStr` and `SupportsBool` protocols. | ||
if self.right.type.defn.info and "__bool__" in self.right.type.defn.info.names: | ||
bool_method = self.right.type.defn.info.names["__bool__"] |
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 think that instead we should try changing None
to have def __bool__(self) -> Literal[False]
signature.
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.
You mean the mypy.types.NoneType
?
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.
Yes, the member __bool__
of NoneType
None had special case for
__str__
and__hash__
, but it can be extened for__bool__
as well for supporting next code:https://mypy-play.net/?mypy=latest&python=3.11&gist=42d8731eb61fd5b1e9150b693375015f