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: Unusual TypeError with dataclass decorator
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, rhettinger
Priority: normal Keywords:

Created on 2018-01-13 21:26 by rhettinger, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
data_class_type_error.py rhettinger, 2018-01-13 21:26 Actual example where the problem arose
Messages (5)
msg309901 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-01-13 21:26
The following code (simplified from a real example) triggers an unexpected TypeError:

    @dataclass
    class PrioritizedItem:
        priority: 'int'
        def __eq__(self, other):
            return self.priority == other.priority

This code gives the following trackback:

    Traceback (most recent call last):
      File "/Users/raymond/Documents/tmp16.py", line 16, in <module>
        @dataclass
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/dataclasses.py", line 598, in dataclass
        return wrap(_cls)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/dataclasses.py", line 590, in wrap
        return _process_class(cls, repr, eq, order, hash, init, frozen)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/dataclasses.py", line 539, in _process_class
        _set_attribute(cls, '__hash__', None)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/dataclasses.py", line 444, in _set_attribute
        raise TypeError(f'Cannot overwrite attribute {name} '
    TypeError: Cannot overwrite attribute __hash__ in PrioritizedItem
msg309902 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-01-13 21:30
I think the error message here can be improved to explain why is being raised.  Otherwise the cause isn't self-evident.
msg309903 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-01-13 22:58
Thanks.

I'm going to be changing the errors as part of #32513, but I'll make sure to cover this case better.
msg309910 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-01-14 02:00
No matter what happens with #32513, I think the right thing to do in this case is to not error if trying to set __hash__ to None, if it already is None in __dict__.
msg310903 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-01-28 01:01
This has been fixed as part of #32513.
History
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76727
2018-01-28 01:01:36eric.smithsetstatus: open -> closed
resolution: fixed
messages: + msg310903

stage: resolved
2018-01-14 02:00:13eric.smithsetmessages: + msg309910
2018-01-13 22:58:55eric.smithsetmessages: + msg309903
2018-01-13 21:30:11rhettingersetmessages: + msg309902
2018-01-13 21:26:23rhettingercreate