You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
asdict fails for attributes of type Mapping with keys of type Tuple #646
@attr.s
class A:
a = attr.ib()
instance = A({(1, ): 1})
attr.asdict(instance)
produces: TypeError: unhashable type: 'list'
when the retain_collection_types flag is set to false (the default). This can take a while to figure out, since the error is not the most descriptive (and the classes are not toy examples like this one).
I just thought it would be worth mentioning. In any case, thanks for this awesome library.
I stumbled upon the same issue and it is indeed not easy to figure out what is going wrong. I'm trying to think how this could be fixed. @hynek Could you comment on the following? Thanks!
Would it be acceptable to change the default value for retain_collection_types to True? This would reduce the likelihood of this issue to appear unexpectedly. It is not a full fix though.
Would it be ok to use tuple as default collection type, when retain_collection_types=False? I would expect this to completely resolve the issue.
Activity
tovrstra commentedon Aug 27, 2020
I stumbled upon the same issue and it is indeed not easy to figure out what is going wrong. I'm trying to think how this could be fixed. @hynek Could you comment on the following? Thanks!
Would it be acceptable to change the default value for
retain_collection_typestoTrue? This would reduce the likelihood of this issue to appear unexpectedly. It is not a full fix though.Would it be ok to use
tupleas default collection type, whenretain_collection_types=False? I would expect this to completely resolve the issue.hynek commentedon Sep 1, 2020
We cannot change the defaults for attr.asdict due to backward compatibility but we can do that for the upcoming attrs.asdict.
tovrstra commentedon Sep 1, 2020
@hynek I understand. I've been thinking about related corner cases for
asdict, which may also be addressed when making a newer version.Frozen sets are not changed into a default collection type, while sets are:
This type of behavior can be made consistent in general by checking whether objects are instances of the classes defined in collections.abc.
(Before making a new version, it would also be useful to understand the motivation for the current behavior.)
hynek commentedon Oct 19, 2020
I have just merged #704 that fixes the frozenset issue – sorry for the late reply!
hynek commentedon Oct 19, 2020
I have created a new issue for the new defaults, so closing this one.
tovrstra commentedon Oct 19, 2020
No problem. Thank you for fixing!
ikalnytskyi commentedon Jun 16, 2021
I think I still experience this issue on latest stable
attrs == 21.2.0. Is this expected?hynek commentedon Jun 28, 2021
You have to pass
retain_collection_types=Truebecause we can't change the default value – that would be a very breaking change.We intend to change it when you can import it from
attrs. I hope I'll get to it later this year.Add tuple_keys to asdict
Add tuple_keys to asdict
Add tuple_keys to asdict
Serialize keys as tuples in asdict (#888)
Merge #465 #466
Merge #465 #467
⬆️ Bump attrs from 21.2.0 to 21.3.0 (#18)
Merge #3746 #3747
Merge #465 #467