[issue35540] dataclasses.asdict breaks with defaultdict fields
New submission from Will T : _asdict_inner attempts to manually recursively deepcopy dicts by calling type(obj) with a generator of transformed keyvalue tuples @ https://github.com/python/cpython/blob/b2f642ccd2f65d2f3bf77bbaa103dd2bc2733734/Lib/dataclasses.py#L1080 . defaultdicts are dicts so this runs but unlike other dicts their first arg has to be a callable or None: import collections import dataclasses as dc @dc.dataclass() class C: d: dict c = C(collections.defaultdict(lambda: 3, {})) d = dc.asdict(c) assert isinstance(d['d'], collections.defaultdict) assert d['d']['a'] == 3 => Traceback (most recent call last): File "boom.py", line 9, in d = dc.asdict(c) File "/Users/spinlock/.pyenv/versions/3.7.1/lib/python3.7/dataclasses.py", line 1019, in asdict return _asdict_inner(obj, dict_factory) File "/Users/spinlock/.pyenv/versions/3.7.1/lib/python3.7/dataclasses.py", line 1026, in _asdict_inner value = _asdict_inner(getattr(obj, f.name), dict_factory) File "/Users/spinlock/.pyenv/versions/3.7.1/lib/python3.7/dataclasses.py", line 1058, in _asdict_inner for k, v in obj.items()) TypeError: first argument must be callable or None I understand that it isn't this bit of code's job to support every dict (and list etc.) subclass under the sun but given defaultdict is stdlib it's imo worth supporting explicitly. -- components: Library (Lib) messages: 332166 nosy: wrmsr priority: normal severity: normal status: open title: dataclasses.asdict breaks with defaultdict fields versions: Python 3.7 ___ Python tracker <https://bugs.python.org/issue35540> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue32162] typing.Generic breaks __init_subclass__
Will T added the comment: I believe I'm experiencing a related bug in the new (3.7) version unfortunately. The current version of typing.Generic.__init_subclass__ isn't chaining to super(Generic, cls).__init_subclass__, meaning Generic's position in class bases can prevent subsequent bases from working properly. I can currently work around it with this unsightly hack but it's obviously suboptimal: _old_generic_init_subclass = object.__getattribute__(ta.Generic, '__init_subclass__').__func__ @classmethod # noqa def _new_generic_init_subclass(cls, *args, **kwargs): # noqa _old_generic_init_subclass(cls, *args, **kwargs) super(ta.Generic, cls).__init_subclass__(*args, **kwargs) ta.Generic.__init_subclass__ = _new_generic_init_subclass # noqa -- nosy: +wrmsr ___ Python tracker <https://bugs.python.org/issue32162> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue33207] typing.Generic does not correctly call super().__init_subclass__
New submission from Will T : Per the docs ( https://docs.python.org/3/reference/datamodel.html#object.__init_subclass__ ) this should be chain-calling super but currently doesn't, and thus breaks base classes listed after it which depend on this functionality. Attached a test for a real-world usecase of mine. Originally noted in https://bugs.python.org/issue32162? but on a new ticket as requested. Thanks :) -- components: Library (Lib) files: generic_init_subclass.py messages: 314828 nosy: wrmsr priority: normal severity: normal status: open title: typing.Generic does not correctly call super().__init_subclass__ type: behavior versions: Python 3.7 Added file: https://bugs.python.org/file47513/generic_init_subclass.py ___ Python tracker <https://bugs.python.org/issue33207> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue32162] typing.Generic breaks __init_subclass__
Will T added the comment: Done: https://bugs.python.org/issue33207 - thanks for the quick response! -- ___ Python tracker <https://bugs.python.org/issue32162> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue32873] Pickling of typing types
Will T added the comment: I believe I hit a bug with this fix (just pulled the code a few min ago): In [10]: pickle.loads(pickle.dumps(typing.List)) Out[10]: typing.List In [11]: pickle.loads(pickle.dumps(typing.FrozenSet)) --- PicklingError Traceback (most recent call last) in () > 1 pickle.loads(pickle.dumps(typing.FrozenSet)) PicklingError: Can't pickle typing.Frozenset: attribute lookup Frozenset on typing failed The cause is in _GenericAlias.__init__ name = orig_name[0].title() + orig_name[1:] Maybe just pass the name explicitly? For context I originally hit this trying to explicitly getattr(typing, alias_name) not by pickling but I'm pleased to see that's at least apparently intended to be valid use (I need to get the underlying special's parameter variance which is lost when you give it args). -- nosy: +wrmsr ___ Python tracker <https://bugs.python.org/issue32873> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com