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
Fix TypeIs for types with type params in Unions #17232
Open
kreathon
wants to merge
11
commits into
python:master
Choose a base branch
from
kreathon:fix-typeis-for-types-with-type-params-in-unions
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix TypeIs for types with type params in Unions #17232
kreathon
wants to merge
11
commits into
python:master
from
kreathon:fix-typeis-for-types-with-type-params-in-unions
+75
−35
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: pandera (https://github.com/pandera-dev/pandera)
+ pandera/dtypes.py:555: error: Unused "type: ignore" comment [unused-ignore]
|
@JelleZijlstra I am ready for feedback 🙂 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix
TypeIs
narrowing for types with type parameters inUnion
types.Addresses: #17181
Example:
Implementation
My goal was to not split the implementation up (see first commit) into handling of the
isinstance
andTypeIs
, but to use a common implementation.Before, the code was using
is_proper_subtype
with erased types:However, this does no longer work (see example above). The idea is to use
is_subtype
(which should also be able to handle the isinstance implementation), because e.g.list[int]
is subtype oflist[Any]
.The only problem with this implementation are "trivial"
Any
cases that should not result in any narrowing. The code tries to manually handle these (I could not find any existing method that would do something similar).Test Plan
testTypeIsUnionWithTypeParams
(test case of the bug report)testTypeIsAwaitableAny
test case, because it is also an example in the PEP. Note that the behavior of this test did not change with this implementation see.testTypeIsTypeAny
test case, because ofpandera
mypy_primer
output (see below)Primer Output
pandera
New behavior should be an improvement compared to before:
Old:
New (see added test case):
Note
It seems that the narrowing still does not work as excepted in some cases (see the pandera example). The type should be
type[__main__.A]
instead oftype[Any]
. However, I think that this is a different issue.