-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Fix isinstance with unions of tuples #20677
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
|
Diff from mypy_primer, showing the effect of this PR on open source code: hydpy (https://github.com/hydpy-dev/hydpy)
+ hydpy/core/objecttools.py:1743: error: Unused "type: ignore" comment [unused-ignore]
psycopg (https://github.com/psycopg/psycopg)
+ psycopg/psycopg/types/numeric.py:407: error: No overload variant of "int" matches argument type "object" [call-overload]
+ psycopg/psycopg/types/numeric.py:407: note: Possible overload variants:
+ psycopg/psycopg/types/numeric.py:407: note: def int(str | Buffer | SupportsInt | SupportsIndex = ..., /) -> int
+ psycopg/psycopg/types/numeric.py:407: note: def int(str | bytes | bytearray, /, base: SupportsIndex) -> int
Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/context/slash.py:982: error: List item 0 has incompatible type "_OtherT"; expected "_T" [list-item]
pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/indexes/datetimelike.py:892: error: Unused "type: ignore" comment [unused-ignore]
pylint (https://github.com/pycqa/pylint)
+ pylint/checkers/utils.py:1969: error: Unused "type: ignore" comment [unused-ignore]
cryptography (https://github.com/pyca/cryptography)
+ src/cryptography/x509/extensions.py:1561: error: Incompatible return value type (got "list[str | Name | IPv4Address | IPv6Address | IPv4Network | IPv6Network | bytes | ObjectIdentifier]", expected "list[IPv4Address | IPv6Address | IPv4Network | IPv6Network] | list[str] | list[OtherName] | list[Name] | list[ObjectIdentifier]") [return-value]
+ src/cryptography/x509/extensions.py:1562: error: Incompatible return value type (got "list[DNSName | DirectoryName | IPAddress | OtherName | RFC822Name | RegisteredID | UniformResourceIdentifier]", expected "list[IPv4Address | IPv6Address | IPv4Network | IPv6Network] | list[str] | list[OtherName] | list[Name] | list[ObjectIdentifier]") [return-value]
jinja (https://github.com/pallets/jinja)
+ src/jinja2/nodes.py:203: error: Unused "type: ignore" comment [unused-ignore]
|
|
Most of these are positive. |
JukkaL
left a comment
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 agree that this seems like an improvement overall.
| """Flatten a nested sequence of tuples into one list of nodes.""" | ||
| t = get_proper_type(t) | ||
| if isinstance(t, UnionType): | ||
| return [b for a in t.items for b in flatten_types(a)] |
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.
hm this seems wrong? Now if you have t: tuple[type[int]] | tuple[type[str]] or something and you do if isinstance(x, t):, won't we incorrectly narrow t in the else branch?
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! We won't, but it is possible we might do the incorrect narrowing you suggest for final types in the future. I will add some more test cases to #20675.
The right fix is probably elsewhere, in conditional_types. Over here we are returning a list of types, not a union, and we should distribute over proposed type ranges instead of union-ing here:
Lines 8380 to 8388 in 8bf049f
| proposed_precise_type = UnionType.make_union( | |
| [type_range.item for type_range in proposed_type_ranges if not type_range.is_upper_bound] | |
| ) | |
| remaining_type = restrict_subtype_away( | |
| current_type, | |
| proposed_precise_type, | |
| consider_runtime_isinstance=consider_runtime_isinstance, | |
| ) | |
| return proposed_type, remaining_type |
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.
No description provided.