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: Confusing error message for AttributeError with dataclasses
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, eric.smith, landonjpginn
Priority: normal Keywords:

Created on 2021-12-19 18:15 by landonjpginn, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg408919 - (view) Author: Landon Ginn (landonjpginn) Date: 2021-12-19 18:15
from dataclasses import dataclass, field

@dataclass(order=True, frozen=True)
class Character:
    sort_index: int = field(init=False, repr=False)
    name: str
    job: str
    age: int
    intelligence: int = 50

    def __post_init__(self):
        object.__setattr__(self, 'sortindex', self.intelligence)

Description:
using sortindex instead of sort_index gave the following suggestion: 

Current:
AttributeError: 'Character' object has no attribute 'sort_index'. Did you mean: 'sortindex'?


Expected: 
AttributeError: 'Character' object has no attribute 'sortindex'. Did you mean: 'sort_index'?
msg408920 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-12-19 19:07
Thanks for the bug report, Landon! I think I can reproduce this with a slightly shorter code snippet, but I don't think this is a bug:

```
>>> from dataclasses import dataclass, field
>>> @dataclass
... class Character:
...     sort_index: int = field(init=False, repr=False)
...     intelligence: int
...     def __post_init__(self):
...         self.sortindex = self.intelligence
... 
>>> c = Character(intelligence=50)
>>> c
Character(intelligence=50)
>>> c.sortindex
50
>>> c.sort_index
AttributeError: 'Character' object has no attribute 'sort_index'. Did you mean: 'sortindex'?
```

This seems like the correct error message to me.

The issue is that your "Character" class has a field named "sort_index", but that field is never assigned to. Instead, you assign an attribute named "sortindex" in your __post_init__ method. So the error message is correct: an instance of your Character class has no attribute "sort_index" (it only has a field named "sort_index"), but it *does* have an attribute "sortindex".
msg408933 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-12-19 22:00
Please show the error you're getting, including the traceback.
msg416839 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2022-04-06 02:23
Closing due to lack of response from OP.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90292
2022-04-06 02:23:34AlexWaygoodsetstatus: open -> closed
resolution: not a bug
messages: + msg416839

stage: resolved
2021-12-19 22:00:16eric.smithsetstatus: pending -> open

messages: + msg408933
2021-12-19 19:07:33AlexWaygoodsetstatus: open -> pending
title: Dataclass Suggestion reversed: sortindex / sort_index -> Confusing error message for AttributeError with dataclasses
nosy: + AlexWaygood, eric.smith

messages: + msg408920
2021-12-19 18:15:09landonjpginncreate