Skip to content

Mypy does not detect automatic init generation for fieldless dataclass inheriting from abstract base #16613

Open
@Nuno-Mota

Description

@Nuno-Mota

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions