Skip to content

Dataclass inheritance cannot override final attribute #21843

Description

@nelsyeung

Bug Report

Dataclass inheritance where both the parent and the child contain the same typing.Final attribute causes Cannot override final attribute. This is at least inconsistent with frozen=True. If I don't use typing.Final and use frozen=True, it works, even though both should be the same.

To Reproduce

https://mypy-play.net/?gist=09184eeaf7501d79abb0c890a7019ba3

import dataclasses
import typing as t

@dataclasses.dataclass
class Foo:
    v: t.Final[int] = 0

@dataclasses.dataclass
class Bar(Foo):
    v: t.Final[int] = 1

Expected Behavior

No error, as I'm just changing the default value.

Actual Behavior

main.py:10: error: Cannot override final attribute "v" (previously declared in base class "Foo")  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 2.3.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions