I have a script toy.py, and I find its behavior kind of confusing.
from typing import List
class A:
a = 1
class B(A):
b = 2
def func(input_arg: List[A]) -> None:
"""Debugging mypy."""
print(f"{input_arg=:}")
print(f"{type(input_arg)=:}")
If I append
if __name__ == "__main__":
arg1 = B()
reveal_type([arg1])
func([arg1])
mypy passes:
mypy toy.py
toy.py:22:17: note: Revealed type is "builtins.list[toy.B*]"
but if I instead append
if __name__ == "__main__":
arg2 = [B()]
reveal_type(arg2)
func(arg2)
which I thought is equivalent to the first case, I see error
mypy toy.py
toy.py:26:17: note: Revealed type is "builtins.list[toy.B*]"
toy.py:27:10: error: Argument 1 to "func" has incompatible type "List[B]"; expected "List[A]"
toy.py:27:10: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
toy.py:27:10: note: Consider using "Sequence" instead, which is covariant
Found 1 error in 1 file (checked 1 source file)
If List is invariant, why would the first case pass?
mypy --version
mypy 0.931