Visitar URL original
[mypy 2.4 regression] Incorrect type narrowing for reversed list of tuples · Issue #22174 · python/mypy · GitHub
Skip to content

[mypy 2.4 regression] Incorrect type narrowing for reversed list of tuples #22174

Description

@haudren-woven

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:

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

Activity

  1. hauntsaninja commented on Oct 7, 2026

    @hauntsaninja
    Collaborator

    Thanks, bisects to a typeshed change #21799

  2. hauntsaninja commented on Oct 7, 2026

    @hauntsaninja
    Collaborator
  3. 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
  4. ilevkivskyi commented on Oct 7, 2026

    @ilevkivskyi
    Member

    @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?

  5. ilevkivskyi commented on Oct 10, 2026

    @ilevkivskyi
    Member

    Anyway, I am going to transfer to typeshed for now, let's see what they will say.

  6. transferred this issue frompython/mypyon Oct 10, 2026
  7. 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
  8. srittau commented on Oct 10, 2026

    @srittau
    Contributor

    I'm not sure exactly what is going on here. See this simplified gist:

    https://mypy-play.net/?mypy=2.4.0&python=3.12&flags=warn-return-any&gist=fe5b3b103252c7abc7a9dc3b9d3d0354

    mypy is inferring Any from reversed(list[tuple[Any, int]]), although it should infer Iterator[tuple[Any, int]]:

    https://github.com/python/typeshed/blob/0d9b1926fc75ea33c643171b83cea73e3dd9990c/stdlib/builtins.pyi#L2209-L2218

    https://github.com/python/typeshed/blob/0d9b1926fc75ea33c643171b83cea73e3dd9990c/stdlib/builtins.pyi#L1285

    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.

  9. ilevkivskyi commented on Oct 10, 2026

    @ilevkivskyi
    Member

    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.

  10. ilevkivskyi commented on Oct 10, 2026

    @ilevkivskyi
    Member

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions