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
dataclass does not properly support generics #7520
Please provide more information to help us understand the issue:
Are you reporting a bug, or opening a feature request?
I believe this is a bug.
Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
Here is MRE:
fromdataclassesimportdataclassfromtypingimportGeneric, NewType, TypeVarT=TypeVar("T")
V=NewType("V", int)
@dataclassclassBase(Generic[T]):
attr: T# If this decorator is commented than the generic works as expected@dataclassclassSub(Base[V]):
passSub(attr=V(1))
What is the actual behavior/output?
Argument "attr" to "Sub" has incompatible type "V"; expected "T"
What is the behavior/output you expect?
The code should type check.
What are the versions of mypy and Python you are using?
Do you see the same issue after installing mypy from Git master?
Yes, this is a bug. The fix is to map/expand variable types in the supertype along the MRO instead of "blindly" copying them to the subtype. Should be not too hard to implement.
Getting error related to the same issue -- inheritence in generic dataclasses -- If the TypeVar is in another Generic container
from typing import Generic, TypeVar
from dataclasses import dataclass
T = TypeVar('T', int, str)
class C(Generic[T]):
pass
@dataclass
class A(Generic[T]):
x: C[T]
@dataclass
class B(A[int]):
pass
B(x=C[int]()) # error: Argument "x" to "B" has incompatible type "C[int]"; expected "C[T]"
The equivalent code without dataclasses using __init__ works as expected.
Activity
ilevkivskyi commentedon Sep 29, 2019
Yes, this is a bug. The fix is to map/expand variable types in the supertype along the MRO instead of "blindly" copying them to the subtype. Should be not too hard to implement.
natemcmaster commentedon Aug 30, 2020
It looks like this is still a problem with the latest release. I think I know how to solve this. I can send a pull request.
Also, looks like the same problem affects the attrs plugin (#5744). I think I can fix both with basically the same code.
fix: resolve typevar before generating dataclasses init method
fix: resolve typevar before generating dataclasses init method
fix: resolve typevar before generating dataclasses init method
fix: resolve typevar before generating dataclasses init method
fix: resolve typevar before generating dataclasses init method
natemcmaster commentedon Aug 30, 2020
I opened PRs to fix:
Fix generic inheritance for dataclass init methods (#9380)
rusiaaman commentedon Sep 21, 2021
Getting error related to the same issue -- inheritence in generic dataclasses -- If the TypeVar is in another Generic container
The equivalent code without dataclasses using
__init__works as expected.Reported here #10986
Anything that we can do to address it?
drewm1980 commentedon Jan 24, 2022
I encountered the same issue as @rusiaaman.
His example reproduces in python 3.9.7
natemcmaster commentedon Jan 25, 2022
Open a new bug report with the details. This bug report has been closed.