Skip to content

Slots does not work with typing.Final dataclass attribute #115879

Closed as not planned
@NicoHood

Description

@NicoHood

Bug report

Bug description:

Hi,
I discovered a bug when using the Final attribute along with slots=True in dataclasses. The bug is more described in this stackoverflow post where someone with more knowledge has explained to me why this happens and which code line causes this issue:
https://stackoverflow.com/questions/78039353/why-does-slots-not-work-with-final-dataclass-attribute

Here is a minimal example:

#!/usr/bin/env python3

from __future__ import annotations
from dataclasses import dataclass
from typing import Final, ClassVar

@dataclass(slots=True)
class Child():
    VERSION: Final[str] = '1.0.0'

    @classmethod
    def check_version(cls, version: str) -> bool:
        return version == cls.VERSION

print(Child.check_version('1.0.0'))
# Returns False

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions