-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
Description
from dataclasses import dataclass
@dataclass
class Foo:
a: object
@dataclass
class Bar(Foo):
a: intmain.py: error: Argument 1 of "__replace__" is incompatible with supertype "Foo"; supertype defines the argument type as "object" [override]
main.py: note: This violates the Liskov substitution principle
main.py: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
Found 1 error in 1 file (checked 1 source file)
this error has no line number and therefore cannot be ignored. this is a big problem because not being able to type:ignore the error is preventing us from updating mypy
the error also occurs when the dataclass is frozen, in which case the error is a false positive here because the type of the field should be considered covariant:
from dataclasses import dataclass
@dataclass(frozen=True)
class Foo:
a: object
@dataclass(frozen=True)
class Bar(Foo):
a: int(you could argue that the error is technically still correct because the generated __replace__ method takes the constructor arguments in a contravariant position, but dataclasses.replace is completely untyped anyway, so this additional safety has no benefit).
related issue on pyright: microsoft/pyright#9351
KotlinIsland, jorenham, Achimh3011 and andersk