Mypy does not detect automatic init generation for fieldless dataclass inheriting from abstract base
Open
Nobody has claimed this yet.
bug
topic-dataclasses
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
It appears that mypy ignores that the __init__ of dataclasses is automatically generated even when they have no fields.
To Reproduce
Consider the following abstract_dataclass.py MWE:
from abc import ABC, abstractmethod
from dataclasses import dataclass
class Foo(ABC):
@abstractmethod
def __init__(self) -> None: ...
def __post_init__(self) -> None:
print(f"Created '{self.__class__.__name__}' instance.")
@dataclass
class Bar(Foo): ...
@dataclass
class Baz(Foo):
a: int
Bar() # error: Cannot instantiate abstract class "Bar" with abstract attribute "__init__" [abstract]
Baz(a=3) # No error.
Running it, however:
python abstract_dataclass.py
Created 'Bar' instance.
Created 'Baz' instance.
Expected Behavior
No error should be identified.
Your Environment
- Mypy version used: 1.3.0
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.10.8
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the provided abstract_dataclass.py example and the repository's normal mypy test command. Search the dataclass handling and abstract-class checks to find where a fieldless dataclass's generated init is missed. Done means Bar() no longer reports an abstract init error while Baz(a=3) continues to type-check.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100