This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dataclasses slots with init=False field raises AttributeException
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, miss-islington, tefra
Priority: normal Keywords: patch

Created on 2021-07-15 19:06 by tefra, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29692 merged eric.smith, 2021-11-22 01:53
PR 29704 merged miss-islington, 2021-11-22 13:26
Messages (7)
msg397575 - (view) Author: Christodoulos Tsoulloftas (tefra) Date: 2021-07-15 19:06
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
Python 3.10.0b4+
msg397653 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-07-16 19:15
I created issue 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'.
msg397678 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-07-17 01:51
With Pablo's changes, the error now reads (in 3.11):

    obj.b
    ^^^^^
AttributeError: 'Example' object has no attribute 'b'

Which is a vast improvement!

I'm working on a PR to initialize obj.b in __init__.
msg401039 - (view) Author: Christodoulos Tsoulloftas (tefra) Date: 2021-09-04 08:11
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.
msg406772 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-11-22 13:26
New changeset d3062f672c92855b7e9e962ad4bf1a67abd4589b by Eric V. Smith in branch 'main':
bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692)
https://github.com/python/cpython/commit/d3062f672c92855b7e9e962ad4bf1a67abd4589b
msg406774 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-11-22 13:29
Thanks for the bug report!
msg406776 - (view) Author: miss-islington (miss-islington) Date: 2021-11-22 13:47
New changeset 10343bd98390ef15909e3a19f26a6178162996fd by Miss Islington (bot) in branch '3.10':
bpo-44649: Fix dataclasses(slots=True) with a field with a default, but init=False (GH-29692)
https://github.com/python/cpython/commit/10343bd98390ef15909e3a19f26a6178162996fd
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88815
2021-11-22 13:47:49miss-islingtonsetmessages: + msg406776
2021-11-22 13:29:26eric.smithsetstatus: open -> closed
resolution: fixed
messages: + msg406774

stage: patch review -> resolved
2021-11-22 13:26:23miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request27940
2021-11-22 13:26:21eric.smithsetmessages: + msg406772
2021-11-22 01:56:06eric.smithsetversions: + Python 3.11
2021-11-22 01:53:15eric.smithsetkeywords: + patch
stage: patch review
pull_requests: + pull_request27930
2021-09-04 08:11:21tefrasetmessages: + msg401039
2021-07-17 01:51:15eric.smithsetmessages: + msg397678
2021-07-16 19:15:50eric.smithsetmessages: + msg397653
2021-07-15 19:08:47eric.smithsetassignee: eric.smith

nosy: + eric.smith
2021-07-15 19:06:03tefracreate