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: dataclasses.asdict will mangle collection.Counter instances
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: brad.scarlett@gmail.com, eric.smith
Priority: normal Keywords:

Created on 2020-03-11 06:13 by brad.scarlett@gmail.com, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg363884 - (view) Author: bscarlett (brad.scarlett@gmail.com) Date: 2020-03-11 06:13
I noticed that dataclasses.asdict seems to incorrectly reconstruct collections.Counter objects with the counter values as tuple keys.

eg: 

In [1]: from collections import Counter
In [2]: from dataclasses import dataclass, asdict
In [3]: c = Counter()
In [4]: c['stuff'] += 1
In [5]: @dataclass
   ...: class Bob:
   ...:     c: Counter
   ...:
In [6]: b = Bob(c)
In [7]: c
Out[7]: Counter({'stuff': 1})
In [9]: b.c
Out[9]: Counter({'stuff': 1})
In [10]: asdict(b)
Out[10]: {'c': Counter({('stuff', 1): 1})}
In [11]: asdict(b)['c']
Out[11]: Counter({('stuff', 1): 1})

The Counter gets reconstructed with its item tuples as keys.

This problem seems to have similar aspects to https://bugs.python.org/issue35540
msg363893 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-03-11 09:24
The asdict API was a mistake, because it's not possible for it to know how to create all possible sub-objects. I can't decide what to do about it. I might just "abandon it in place", by documenting its problems and suggesting it not be used.

I should research what attrs does.
History
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84110
2020-03-11 09:24:12eric.smithsetassignee: eric.smith
messages: + msg363893
2020-03-11 07:43:52xtreaksetnosy: + eric.smith
2020-03-11 06:13:03brad.scarlett@gmail.comcreate