Skip to content

dataclass does not properly support generics #7520

Closed
@hackaugusto

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions