mypy icon indicating copy to clipboard operation
mypy copied to clipboard

new generics should be inferred as covariant instead of invariant on frozen dataclasses

Open DetachHead opened this issue 1 year ago • 0 comments

# 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]

playground

since value is immutable, T is only used in an output position therefore the generic should be inferred as covariant

DetachHead avatar Aug 19 '24 23:08 DetachHead