python / python/mypy

no warning when `dataclass` extends non-dataclass with an `__init__` method

Open
#13,051 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-2-low topic-dataclasses topic-runtime-semantics
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

from dataclasses import dataclass


class Foo:
    def __init__(self, value: int) -> None:
        self.value = value  # never gets run


@dataclass
class Bar(Foo):
    value2: int


Bar(1).value  # runtime error but no mypy error

playground

not sure if this is supposed to be allowed but either way this fails at runtime, so mypy should warn about it:

AttributeError: 'Bar' object has no attribute 'value'. Did you mean: 'value2'?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal Python reproducer in the issue and tracing how dataclass inheritance is analyzed. Determine whether this case should produce a diagnostic, then add a regression test covering the missing warning and verify that the test suite passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.