Fix isinstance with unions of tuples#20677
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.
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.
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.
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 I was being sillyconditional_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
There was a problem hiding this comment.
No description provided.