mypy
mypy copied to clipboard
new generics should be inferred as covariant instead of invariant on frozen dataclasses
# mypy: enable-incomplete-feature=NewGenericSyntax
from dataclasses import dataclass
@dataclass(frozen=True)
class Foo[T]:
value: T
foo = Foo[int](value=1)
bar: Foo[object] = foo # error: Incompatible types in assignment (expression has type "Foo[int]", variable has type "Foo[object]") [assignment]
since value is immutable, T is only used in an output position therefore the generic should be inferred as covariant