Skip to content

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

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

Closed
MarcoRuzzon opened this issue Feb 11, 2022 · 1 comment
Labels
bug mypy got something wrong topic-dataclasses topic-inheritance Inheritance and incompatible overrides

Comments

@MarcoRuzzon
Copy link

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

@MarcoRuzzon MarcoRuzzon added the bug mypy got something wrong label Feb 11, 2022
@AlexWaygood AlexWaygood added topic-dataclasses topic-inheritance Inheritance and incompatible overrides labels Mar 26, 2022
@AlexWaygood
Copy link
Member

Closing as a duplicate of #12063

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