Skip to content

Incompatible type; expected "Optional[T]" with generic dataclasses inheritance #12167

Closed
@MarcoRuzzon

Description

@MarcoRuzzon

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:

T = TypeVar('T')

@dataclasses.dataclass
class A(Generic[T]):
    attr: typing.Optional[T] = None

A(attr=0)  # OK!

@dataclasses.dataclass
class B(A[int]):
  ...

B(attr=0)
# Argument "attr" to "B" has incompatible type "int"; expected "Optional[T]"
  • What is the actual behavior/output?

error: Argument "attr" to "B" has incompatible type "int"; expected "Optional[T]"

  • What is the behavior/output you expect?

The code should type check, as it does without dataclass

T = TypeVar('T')

class A(Generic[T]):
    def __init__(self, attr: typing.Optional[T] = None):
        self.attr = attr

A(attr=0)  # OK!

class B(A[int]):
    ...

B(attr=0)  # OK!
  • What are the versions of mypy and Python you are using?

mypy 0.910

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

The defaults, executed with: mypy test_script.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions