Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when inheriting from a generic dataclass with a generic attribute #12063

Closed
drewm1980 opened this issue Jan 25, 2022 · 3 comments
Closed
Labels
bug mypy got something wrong topic-dataclasses topic-inheritance Inheritance and incompatible overrides

Comments

@drewm1980
Copy link

Bug Report

When typechecking the following code:

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 with the parent class NOT a data class:

class A_(Generic[T]):        
    x: C[T]

    def __init__(self, x: C[T]):   
        self.x = x

@dataclass
class B_(A_[int]):           
    pass                     

B_(x=C[int]())

Does not error.
If A_ is a normal class and B_ is marked as a dataclass, it also does not error. In other words it is the parent class that must be "un-dataclassed" as a workaround.

Your Environment

I have reproduced this on a recent version of mypy and python, but encountered it first in a python 3.6 environment, so it is not a new regression.

  • Mypy version used: Reproduced in mypy 0.910
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.7
  • Operating system and version: Ubuntu, but this was reproduced in a conda environment, not with the system intepreter.
@drewm1980 drewm1980 added the bug mypy got something wrong label Jan 25, 2022
@drewm1980
Copy link
Author

First reported at the end of related issue that is already fixed and closed:

#7520

@drewm1980 drewm1980 changed the title Incompatible type error when inheriting from a generic dataclass Error when inheriting from a generic dataclass with a generic attribute Jan 25, 2022
@drewm1980
Copy link
Author

@natemcmaster thank you for resolving the previous similar bug, and thanks for your work on mypy. Life in python is so much better with static types.

@AlexWaygood
Copy link
Member

Closing as a duplicate of #10986

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-dataclasses topic-inheritance Inheritance and incompatible overrides
Projects
None yet
Development

No branches or pull requests

2 participants