python / python/mypy

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

Open
#13,735 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

diagnostics feature topic-error-reporting
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

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 main.py reproduction and compare the diagnostics for assignment, return-value, and override errors. Trace where the existing List invariance notes are emitted, then ensure the override error receives the same notes and verify that the output matches the expected behavior shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.