Skip to content

Narrow types only if they match after removing last known values - #22582

Merged
JukkaL merged 1 commit into
python:masterfrom
p-sawicki:discard-narrowing-if-incompatible-after-cleanup
Dec 3, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
p-sawicki:discard-narrowing-if-incompatible-after-cleanup

Conversation

@p-sawicki

Copy link
Copy Markdown
Collaborator

Fixes #22514
Closes #22545

The additional type constraints added in #22286 regressed a specific case where a generic type parameter may be inferred from an expression that matches the expected type based on last known values but doesn't match after removing them.

In the example from the issue:

from enum import Enum, auto
from typing import Literal, Sequence

class Color(Enum):
    RED = auto()
    BLUE = auto()

class Shape(Enum):
    CIRCLE = auto()
    TRIANGLE = auto()
    SQUARE = auto()

Pair = tuple[Literal[Color.RED], Literal[Shape.CIRCLE]] | tuple[Literal[Color.BLUE], Literal[Shape.TRIANGLE]]

def consume(pairs: Sequence[Pair]) -> None:
    pass

consume([(Color.BLUE, Shape.TRIANGLE)])

the type parameter of the list expression T passed as argument is initally inferred as tuple[BLUE, TRIANGLE] but removing last known values widens it to tuple[Color, Shape] which no longer matches Pair. Before the changes the only constraint was that T must be a subtype of Pair so it was inferred as Pair because the literals match.

Keep the old behavior by checking if the more specific narrowing would still match after removing last known values, and discard it if it doesn't.

Fixes python#22514
Closes python#22545

The additional type constraints added in python#22286 regressed a specific
case where a generic type parameter may be inferred from an expression
that matches the expected type based on last known values but doesn't
match after removing them.

In the example from the issue:

```python
from enum import Enum, auto
from typing import Literal, Sequence

class Color(Enum):
    RED = auto()
    BLUE = auto()

class Shape(Enum):
    CIRCLE = auto()
    TRIANGLE = auto()
    SQUARE = auto()

Pair = tuple[Literal[Color.RED], Literal[Shape.CIRCLE]] | tuple[Literal[Color.BLUE], Literal[Shape.TRIANGLE]]

def consume(pairs: Sequence[Pair]) -> None:
    pass

consume([(Color.BLUE, Shape.TRIANGLE)])
```

the type parameter of the list expression `T` passed as argument is
initally inferred as `tuple[BLUE, TRIANGLE]` but removing last known
values widens it to `tuple[Color, Shape]` which no longer matches `Pair`.
Before the changes the only constraint was that `T` must be a subtype of
`Pair` so it was inferred as `Pair` because the literals match.

Keep the old behavior by checking if the more specific narrowing would
still match after removing last known values, and discard it if it
doesn't.
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

cattrs (https://github.com/python-attrs/cattrs)
- src/cattrs/converters.py:250: error: Argument 1 to "register_func_list" of "MultiStrategyDispatch" has incompatible type "list[tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Callable[[Any], Any]], str] | tuple[Callable[[Any], bool], Callable[[Any], str]] | tuple[Callable[[Any], Any], Callable[[Any], Any]] | tuple[Callable[[Any], Any], Callable[[Any], dict[str, Any]]] | tuple[Callable[[Any], bool], Callable[[type[Enum], BaseConverter], Callable[[Enum], Any]], str] | <7 more items>]"; expected "list[tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"  [arg-type]
- src/cattrs/converters.py:292: error: Argument 1 to "register_func_list" of "MultiStrategyDispatch" has incompatible type "list[tuple[Callable[[Any], Any], Callable[[Mapping[str, Any], type[T]], T]] | tuple[Callable[[Any], bool], Callable[[type[Enum], BaseConverter], Callable[[Any, type[Enum]], Enum]], str] | tuple[Callable[[Any], Any | bool], Callable[[Any], Callable[[Any, type[T]], T]], bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Callable[[Any, Any], Any]], str] | tuple[Callable[[Any], bool], Callable[[Any, bool], Callable[[Any, type[T]], type[T] | None]], bool] | <14 more items>]"; expected "list[tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"  [arg-type]
- src/cattrs/converters.py:470: error: Argument 1 to "register_func_list" of "MultiStrategyDispatch" has incompatible type "list[tuple[Any, Any, str] | tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"; expected "list[tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"  [arg-type]
- src/cattrs/converters.py:611: error: Argument 1 to "register_func_list" of "MultiStrategyDispatch" has incompatible type "list[tuple[Any, Any, str] | tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"; expected "list[tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"  [arg-type]
- src/cattrs/preconf/orjson.py:90: error: Argument 1 to "register_func_list" of "MultiStrategyDispatch" has incompatible type "list[tuple[Callable[[Any], bool], partial[Callable[[Any], Any]], str] | tuple[Callable[[Any], bool], Callable[[Any, Any], Any], bool] | tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"; expected "list[tuple[Callable[[Any], bool], Any] | tuple[Callable[[Any], bool], Any, bool] | tuple[Callable[[Any], bool], Callable[[Any, BaseConverter], Any], Literal['extended']]]"  [arg-type]

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@JukkaL
JukkaL merged commit 6e41e62 into python:master Dec 3, 2026
25 checks passed
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.

[regression] List of enumerator literal tuples not accepted as argument with Sequence parameter

2 participants