Skip to content

Return type on enum regression 1.18.2 -> 1.19.0 #20434

@mmiller-max

Description

@mmiller-max

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions