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
dataclasses slots with init=False field raises AttributeException #88815
Comments
I am trying the new slots directive but I get an AttributeError when I try to access a field with init=False >>> from dataclasses import dataclass, field
>>>
>>> @dataclass(slots=True)
... class Example:
... a: str
... b: str = field(default="b", init=False)
...
>>> obj = Example("a")
>>> obj.__slots__
('a', 'b')
>>> obj.
obj.a obj.b
>>> obj.a
'a'
>>> obj.b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: b. Did you mean: 'b'?
>>> ❯ python --version |
I created bpo-44655 for the confusing error message. The problem with dataclasses is that the instance variable 'b' needs to be initialized, instead of the current dataclasses behavior where it relies on the class variable when reading 'b'. |
With Pablo's changes, the error now reads (in 3.11):
AttributeError: 'Example' object has no attribute 'b' Which is a vast improvement! I'm working on a PR to initialize obj.b in __init__. |
Thanks for the update eric, yeah the new error messages are much more comprehensive and I am glad to hear the original issue will be addressed as well. |
Thanks for the bug report! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: