GeezerVison is showing github.com — [mypy 2.4 regression] Incorrect type narrowing for reversed list of tuples · Issue #22174 · python/mypy
[mypy 2.4 regression] Incorrect type narrowing for reversed list of tuples #22174DescriptionBug 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:
main.py:5: note: Revealed type is "int"
Success: no issues found in 1 source file
Actual Behavior
On mypy 2.4.0 the type of the second tuple element is inferred as Any:
main.py:5: note: Revealed type is "Any"
main.py:6: error: Returning Any from function declared to return "int | None" [no-any-return]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 2.4.0
- Mypy command-line flags: --warn-return-any
- Python version used: 3.12
Activityhauntsaninja commented on Oct 7, 2026 Thanks, bisects to a typeshed change #21799 hauntsaninja commented on Oct 7, 2026 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, 2026ilevkivskyi commented on Oct 7, 2026 @hauntsaninja FWIW I don't see any issue with mypy itself here, it behaves according to the spec. list matches both overloads, first returns Iterator[tuple[str, Any]], second returns reversed[tuple[str, An]], so this is an ambiguous match caused by Any and mypy infers some fallback return type, which is just Any in this case.
Should we simply transfer this to typeshed? ilevkivskyi commented on Oct 10, 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, 2026srittau commented on Oct 10, 2026 ilevkivskyi commented on Oct 10, 2026 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.
ilevkivskyi commented on Oct 10, 2026 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. tevinch commented on Oct 11, 2026 For the concrete list[tuple[Any, int]] case, a small generic helper provides a workaround on 2.4.0:
from collections.abc import Iterator
from typing import Any, TypeVar
T = TypeVar("T")
def reverse_list(values: list[T]) -> Iterator[T]:
return reversed(values)
def foo(values: list[tuple[Any, int]]) -> int | None:
for key, value in reverse_list(values):
return value
return None
With Python 3.12.14 and --warn-return-any, the original gist reports Any and fails on mypy 2.4.0; this version reports int and passes. Both pass on 2.3.1. Deliberately returning value from a function annotated str | None still produces a return-value error.
The helper keeps the built-in reversed() call, including lazy iteration and list-subclass dispatch. It needs no cast or ignored error. This is a list-specific workaround; the upstream inference issue remains.
Complete runnable example and type/runtime checks, including empty lists, full reverse traversal and unchanged input. added a commit that references this issue on Oct 14, 2026This issue will close once commit 120a774 is merged into the 'master' branch.120a774
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|