python / python/mypy

[1.19 regression] Incorrect return type inference on enum methods

Open
#20,434 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-enum topic-inference topic-type-narrowing
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Typing on a class method on a list now fails, and the suggested fix still doesn't work. Gists are best way to show this.

(A clear and concise description of what the bug is.)

To Reproduce

Gist URL: https://gist.github.com/mypy-play/86bb1ad0946410002b776f162c0e075c
Playground URL: https://mypy-play.net/?mypy=1.19.0&python=3.13&gist=86bb1ad0946410002b776f162c0e075c

Gist has three methods, get_levels_1 get_levels_2 and get_levels_3. get_levels_1 is how i previously had the code on 1.18.2, and it gets this error

main.py:32: error: Incompatible return value type (got "list[Self]", expected "list[Level]")  [return-value]
main.py:32: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:32: note: Consider using "Sequence" instead, which is covariant
main.py:32: note: Perhaps you need a type annotation for "levels"? Suggestion: "list[Level]"

When changing return type to list[Level] (get_levels_2) you get this error

main.py:32: error: Incompatible return value type (got "list[Self]", expected "list[Level]")  [return-value]
main.py:32: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:32: note: Consider using "Sequence" instead, which is covariant
main.py:32: note: Perhaps you need a type annotation for "levels"? Suggestion: "list[Level]"

Only typing the levels argument works (get_levels_3)

Expected Behavior

On 1.18.2 get_levels_1 works fine and this regresses in 1.19.0

Your Environment

Mypy version used: 1.19.0
Mypy command-line flags: n/a
Mypy configuration options from mypy.ini (and other config files):

[mypy]
plugins = pydantic.mypy
ignore_missing_imports = True
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
check_untyped_defs = True
warn_unused_ignores = True
warn_no_return = True
warn_return_any = True
warn_redundant_casts = True
strict_equality = True

Python version used: 3.13.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked gist or mypy-play example with mypy 1.19.0 and compare it with 1.18.2, focusing on the get_levels_1, get_levels_2, and get_levels_3 methods. Trace the enum method return-type inference and add a regression test showing that the previously valid annotation no longer produces an error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.