Skip to content

mypy should warn about list invariance when method return types are incompatible with a supertype #13735

Open
@bcj

Description

@bcj

If you try to assign or return a list of a more specific type, mypy prints a helpful note warning that "List" is invariant, and suggests you use the "Sequence" type instead.
If you have a child class override a method to modify its return type to a list of a more specific type, mypy tells you the return type is incompatible but doesn't let you know that switching to the "Sequence" type might resolve their error.

To Reproduce

https://gist.github.com/1c07da0019991a5f14b9ff3a84e393db

numbers: list[float] = [1, 2.2]
integers: list[int] = [1, 2]
numbers = integers

def get_numbers() -> list[float]:
    return integers

class Parent:
    def get_numbers(self) -> list[float]:
        return [1, 2, 3]

class Child(Parent):
    def get_numbers(self) -> list[int]:
        return [1, 2, 3]

Expected Behavior

The invariant note is included for all 3 errors

main.py:3: error: Incompatible types in assignment (expression has type "List[int]", variable has type "List[float]")
main.py:3: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:3: note: Consider using "Sequence" instead, which is covariant
main.py:6: error: Incompatible return value type (got "List[int]", expected "List[float]")
main.py:6: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:6: note: Consider using "Sequence" instead, which is covariant
main.py:13: error: Return type "List[int]" of "get_numbers" incompatible with return type "List[float]" in supertype "Parent"
main.py:13: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:13: note: Consider using "Sequence" instead, which is covariant
Found 3 errors in 1 file (checked 1 source file)

Actual Behavior

main.py:3: error: Incompatible types in assignment (expression has type "List[int]", variable has type "List[float]")
main.py:3: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:3: note: Consider using "Sequence" instead, which is covariant
main.py:6: error: Incompatible return value type (got "List[int]", expected "List[float]")
main.py:6: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:6: note: Consider using "Sequence" instead, which is covariant
main.py:13: error: Return type "List[int]" of "get_numbers" incompatible with return type "List[float]" in supertype "Parent"
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.6

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions