Skip to content

dataclass does not properly support generics #7520

Closed

Description

@hackaugusto

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:

from dataclasses import dataclass
from typing import Generic, NewType, TypeVar

T = TypeVar("T")
V = NewType("V", int)

@dataclass
class Base(Generic[T]):
    attr: T

# If this decorator is commented than the generic works as expected
@dataclass
class Sub(Base[V]):
    pass

Sub(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?

mypy 0.720
yes, tested against: 8782ae7f789ad5b8cab97d3c8419f9f98b6eb285

  • What are the mypy flags you are using? (For example --strict-optional)

The defaults, executed with: mypy test_script.py

  • If mypy crashed with a traceback, please paste
    the full traceback below.

Activity

  1. ilevkivskyi commented on Sep 29, 2019

    @ilevkivskyi
    Member

    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.

  2. natemcmaster commented on Aug 30, 2020

    @natemcmaster
    Contributor

    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.

  3. added 5 commits that reference this issue on Aug 30, 2020
    410d0be
    24d0f6c
    d68a312
    b1b3816
    fb6f791
  4. natemcmaster commented on Aug 30, 2020

    @natemcmaster
    Contributor
  5. added a commit that references this issue on Nov 28, 2020
    98beb8e
  6. rusiaaman commented on Sep 21, 2021

    @rusiaaman

    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.

    Reported here #10986

    Anything that we can do to address it?

  7. drewm1980 commented on Jan 24, 2022

    @drewm1980

    I encountered the same issue as @rusiaaman.

    His example reproduces in python 3.9.7

  8. natemcmaster commented on Jan 25, 2022

    @natemcmaster
    Contributor

    Open a new bug report with the details. This bug report has been closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions