-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[match-case] fix matching against typing.Callable
and Protocol
types.
#19471
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
base: master
Are you sure you want to change the base?
Conversation
…eckable test should be performed elsewhere.
…on and anonymous `Callable`
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
39eedb4
to
74db0aa
Compare
This comment has been minimized.
This comment has been minimized.
3418149
to
d62d6d2
Compare
This comment has been minimized.
This comment has been minimized.
d62d6d2
to
143f990
Compare
This comment has been minimized.
This comment has been minimized.
The second element is the type it would hold if it was not the proposed type, if any. | ||
UninhabitedType means unreachable. | ||
None means no new information can be inferred. | ||
If default is set it is returned instead. |
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.
Maybe this line shouldn't be separated from the previous one? Now it's a bit ambiguous whether default is returned instead of None
only or instead of both None
and Uninhabited
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.
To be honest, I do not think this part of the docstring is really accurate to begin with. Looking at the code on the main branch
https://github.com/python/mypy/blob/c6b40df63ce0fecab6bced800ce778d99587c9b8/mypy/checker.py#L7990-
L8045
How about something like:
Returns a 2-tuple:
The first element is the proposed type, if the expression can be the proposed type.
(or default, if default is set and the expression is a subtype of the proposed type).
The second element is the type it would hold if it was not the proposed type, if any.
(or default, if default is set and the expression is not a subtype of the proposed type).
UninhabitedType means unreachable.
None means no new information can be inferred.
Also, does this fix #19470 completely or partially? Please update the description to either remove "partially" or remove "fixes" magic word, so that half-resolved issue does not get autoclosed |
Co-authored-by: Stanislav Terliakov <[email protected]>
Co-authored-by: Stanislav Terliakov <[email protected]>
for more information, see https://pre-commit.ci
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: mitmproxy (https://github.com/mitmproxy/mitmproxy)
+ mitmproxy/http.py:607: error: Unused "type: ignore" comment [unused-ignore]
|
collection.abc.Callable
raises error [misc] #14014AnyCallable
Protocol differs fromcallable()
check #19470Added extra logic in
checker.py:conditional_types
function to deal with structural types such astyping.Callable
or protocols.new tests
testMatchClassPatternCallable
: testscase Callable() as fn
usagetestMatchClassPatternProtocol
: testscase Proto()
usage, whereProto
is a ProtocoltestMatchClassPatternCallbackProtocol
: testscase Proto()
usage, whereProto
is a Callback-ProtocoltestGenericAliasIsinstanceUnreachable
: derived from a mypy-primer failure in mesonbuild. Tests thatisinstance(x, Proto)
can produce unreachable error.testGenericAliasRedundantExprCompoundIfExpr
: derived from a CI failure ofpython runtest.py self
of an earlier version of this PR.modified tests
testOverloadOnProtocol
added annotations to overload implementation, which wasn't getting checked. Added missing return. Fixed return type in second branch.