You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
regression in 1.12.0: un-suppressable error with no line number when subtyping dataclass when field has incompatible subtype #18115
fromdataclassesimportdataclass@dataclassclassFoo:
a: object@dataclassclassBar(Foo):
a: int
main.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:
fromdataclassesimportdataclass@dataclass(frozen=True)classFoo:
a: object@dataclass(frozen=True)classBar(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).
This error seems to only occur when running mypy with Python 3.13. Checking the same code with Python 3.12 produces no error for me (both locally and on the playground).
edit: this only occurring in Python 3.13 seems expected. The dataclass plugin only adds a __replace__ method to dataclass definitions when checking 3.13+:
Activity
brianschubert commentedon Nov 7, 2024
This error seems to only occur when running mypy with Python 3.13. Checking the same code with Python 3.12 produces no error for me (both locally and on the playground).
edit: this only occurring in Python 3.13 seems expected. The dataclass plugin only adds a
__replace__method to dataclass definitions when checking 3.13+:mypy/mypy/plugins/dataclasses.py
Lines 388 to 389 in 78fb78b
Fix [override] error with no line number when argument node has no li…
JukkaL commentedon Nov 8, 2024
Fixed by #18122.
Ignore override