Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bcj opened this issue Sep 26, 2022 · 0 comments

Comments

@bcj
Copy link

bcj commented Sep 26, 2022

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
@bcj bcj added the bug mypy got something wrong label Sep 26, 2022
@hauntsaninja hauntsaninja added diagnostics feature and removed bug mypy got something wrong labels Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants