-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Slots does not work with typing.Final dataclass attribute #115879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Interesting -- possibly a duplicate of #89547 |
I don't think that this is a bug, actually. Let's "compile" what's happening into simplier python code: @dataclass(slots=True)
class Child:
VERSION: Final[str] = '1.0.0' Turns into: class Child:
__slots__ = ('VERSION',)
def __init__(self, VERSION) -> None:
self.VERSION: Final[str] = '1.0.0' Remember that all So, when you access If you want to use class Base:
VERSION: Final[str] = '1.0.0'
@dataclass(slots=True)
class Child(Base):
... I agree with @AlexWaygood that this is a duplicate of #89547 and the proper solution would be to use something like I am going to close this issue as a duplicate, but feel free to ask any questions you might have :) |
So is |
Now you can do this: class Base:
VERSION: Final[str] = '1.0.0'
@dataclass(slots=True)
class Child(Base):
... |
But what if the version must be in the child, as I have different child implementations? |
Use |
But a classvar is not final, isnt it? |
Bug report
Bug description:
Hi,
I discovered a bug when using the
Final
attribute along withslots=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:
CPython versions tested on:
3.11
Operating systems tested on:
Linux
The text was updated successfully, but these errors were encountered: