[issue46134] Confusing error message for AttributeError with dataclasses

2022-04-05 Thread Alex Waygood


Alex Waygood  added the comment:

Closing due to lack of response from OP.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46134] Confusing error message for AttributeError with dataclasses

2021-12-19 Thread Eric V. Smith


Eric V. Smith  added the comment:

Please show the error you're getting, including the traceback.

--
status: pending -> open

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue46134] Confusing error message for AttributeError with dataclasses

2021-12-19 Thread Alex Waygood


Alex Waygood  added the comment:

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".

--
nosy: +AlexWaygood, eric.smith
status: open -> pending
title: Dataclass Suggestion reversed: sortindex / sort_index -> Confusing error 
message for AttributeError with dataclasses

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com