Repository navigation
[mypy 2.4 regression] Incorrect type narrowing for reversed list of tuples #22174
Description
Activity
Thanks, bisects to a typeshed change #21799
Probably python/typeshed#11646
- changed the title
[-]Incorrect type narrowing for reversed list of tuples in 2.4.0[/-][+][2.4 regression] Incorrect type narrowing for reversed list of tuples[/+]on Oct 7, 2026 @hauntsaninja FWIW I don't see any issue with mypy itself here, it behaves according to the spec.
listmatches both overloads, first returnsIterator[tuple[str, Any]], second returnsreversed[tuple[str, An]], so this is an ambiguous match caused byAnyand mypy infers some fallback return type, which is justAnyin this case.Should we simply transfer this to typeshed?
- added a commit that references this issue
on Oct 7, 2026 Anyway, I am going to transfer to typeshed for now, let's see what they will say.
- changed the title
[-][2.4 regression] Incorrect type narrowing for reversed list of tuples[/-][+][mypy 2.4 regression] Incorrect type narrowing for reversed list of tuples[/+]on Oct 10, 2026 I'm not sure exactly what is going on here. See this simplified gist:
mypy is inferring
Anyfromreversed(list[tuple[Any, int]]), although it should inferIterator[tuple[Any, int]]:The latest versions of ty, pyrefly, and pyright seem to agree, although I didn't check the stubs they are using. At this point I think it's more likely to be a bug in mypy than in the stubs.
It matches both overloads with different return types, see my comment above. Relevant part of the spec https://typing.python.org/en/latest/spec/overload.html#step-5
If the return types are not equivalent, overload matching is ambiguous. In this case, infer a return type that is assignable to each of the remaining return types and permits operations supported by any of them without errors, then stop. A type checker may infer Any or a more precise gradual type that satisfies these requirements.
Actually nvm, in this case the overlap is not "caused" by
Any(even though it appears in an argument type), so the first overload should be selected. I will transfer this back to us.- addedbugmypy got something wrongmypy got something wrong
on Oct 10, 2026
Bug Report
As of mypy 2.4.0, code that iterates over a reversed list of tuples where one of the tuple elements is Any will infer that the type of every tuple element is Any. This is a change in behavior from 2.3.1, which breaks type checking of existing code.
To Reproduce
Use this Gist
Expected Behavior
Same as mypy 2.3.1, the type of the second element of the tuple should be
int:Actual Behavior
On mypy 2.4.0 the type of the second tuple element is inferred as
Any:Your Environment