Skip to content

Conversation

@hauntsaninja
Copy link
Collaborator

No description provided.

@github-actions
Copy link
Contributor

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]

@hauntsaninja
Copy link
Collaborator Author

Most of these are positive.
The psycopg one may potentially be fixable in a future PR
The cryptography one is unfortunate but it is what it is, we would need to somehow magically infer Iterable[X] | Iterable[Y] instead of Iterable[X | Y]

Copy link
Collaborator

@JukkaL JukkaL left a 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.

@hauntsaninja hauntsaninja merged commit e858d5d into python:master Jan 29, 2026
23 checks passed
@hauntsaninja hauntsaninja deleted the narrow62 branch January 29, 2026 18:53
"""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)]

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?

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Jan 29, 2026

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:

mypy/mypy/checker.py

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants