opt_frozen_dataclass: Enable hashing with -O - #273
Conversation
|
Dunno, seems risky 😬 |
|
Maybe replacing |
I'd argue it's needed for correctness. Otherwise, these classes are hashable when running without |
Sure, I could see that. |
Part of the problem is that setting from dataclasses import dataclass
@dataclass(unsafe_hash=True)
class A:
x: int
def __hash__(self):
return 42$ python dc.py
Traceback (most recent call last):
File "/Users/mdiener/Work/emirge/mirgecom/dc.py", line 4, in <module>
@dataclass(unsafe_hash=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mdiener/Work/emirge/miniforge3/envs/ceesd/lib/python3.11/dataclasses.py", line 1222, in wrap
return _process_class(cls, init, repr, eq, order, unsafe_hash,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mdiener/Work/emirge/miniforge3/envs/ceesd/lib/python3.11/dataclasses.py", line 1091, in _process_class
cls.__hash__ = hash_action(cls, field_list, globals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mdiener/Work/emirge/miniforge3/envs/ceesd/lib/python3.11/dataclasses.py", line 851, in _hash_exception
raise TypeError(f'Cannot overwrite attribute __hash__ '
TypeError: Cannot overwrite attribute __hash__ in class AThis error does not happen if just setting |
16d62ed to
9c936f8
Compare
Maybe that's playing whack-a-mole, but the latest revision addresses this. |
9c936f8 to
1226a0c
Compare
|
Paging @alexfikl for a third set of eyes: Is all this just a terrible idea and not worth it? |
1226a0c to
f20e04a
Compare
|
Side note: this table is really helpful when thinking through this. |
| @@ -3000,16 +3000,41 @@ def opt_frozen_dataclass( | |||
| this decorator avoid when the interpreter runs with "optimization" | |||
There was a problem hiding this comment.
| this decorator avoid when the interpreter runs with "optimization" | |
| this decorator avoids when the interpreter runs with "optimization" |
| The resulting dataclass supports hashing unless *eq* is set to *False*, | ||
| if *unsafe_hash* is left at the default or set to *True*. |
There was a problem hiding this comment.
| The resulting dataclass supports hashing unless *eq* is set to *False*, | |
| if *unsafe_hash* is left at the default or set to *True*. | |
| The resulting dataclass supports hashing, even when it is not actually frozen, | |
| if *unsafe_hash* is left at the default or set to *True*. |
There was a problem hiding this comment.
Also, this is still hashable with eq=False, like normal dataclasses.
matthiasdiener
left a comment
There was a problem hiding this comment.
Besides the minor wording comments, this LGTM. Thanks!
Hm, I haven't followed this much, but if I understand correctly.. we want to turn off Assuming that's the intention, that sounds reasonable to me. The code looks fine now with @matthiasdiener's suggestions 😁 |
f20e04a to
cb2683a
Compare
Co-authored-by: Matthias Diener <mdiener@illinois.edu>
cb2683a to
d22dec6
Compare
d22dec6 to
7b4d68a
Compare
|
Thanks all for your help in getting this into shape. I'll do another release as soon as this is in. |
Inspired by inducer/pytato#563.
cc @matthiasdiener (what do you think?)