[issue42742] Add abc.Mapping to dataclass

2020-12-29 Thread Anton Abrosimov
Anton Abrosimov added the comment: Link to python-ideas thread: https://mail.python.org/archives/list/python-id...@python.org/thread/XNXCUJVNOOVPAPL6LF627EOCBUUUX2DG/ -- ___ Python tracker

[issue42742] Add abc.Mapping to dataclass

2020-12-25 Thread Anton Abrosimov
Anton Abrosimov added the comment: Thanks for the good offer, I will definitely use it. -- ___ Python tracker ___ ___

[issue42742] Add abc.Mapping to dataclass

2020-12-25 Thread Eric V. Smith
Eric V. Smith added the comment: I'm just warning you that I probably won't accept it. I haven't heard of any demand for this feature. You might want to bring it up on python-ideas if you want to generate support for the proposal. -- ___ Python

[issue42742] Add abc.Mapping to dataclass

2020-12-25 Thread Anton Abrosimov
Anton Abrosimov added the comment: This Mixin only works with dataclass objects. And uses the private functionality of the dataclasses. So dataclasses.py is the right place for this. I think I can do enough tests. And I think that this is too little for a standalone project. --

[issue42742] Add abc.Mapping to dataclass

2020-12-25 Thread Eric V. Smith
Eric V. Smith added the comment: I don't think this belongs in dataclasses itself, at least not until it's been vetted widely. You might want to put it on PyPI first as a standalone project. -- ___ Python tracker

[issue42742] Add abc.Mapping to dataclass

2020-12-25 Thread Anton Abrosimov
Anton Abrosimov added the comment: I think the second option looks better. More pythonic. No need to create new classes No typing hacks. Mixin can be easily expanded. Yes, I will do refactoring, typing, documentation and tests in PR. -- ___ Python

[issue42742] Add abc.Mapping to dataclass

2020-12-25 Thread Anton Abrosimov
Anton Abrosimov added the comment: An alternative way: from collections.abc import Mapping from dataclasses import dataclass, fields, _FIELDS, _FIELD class DataclassMappingMixin(Mapping): def __iter__(self): return (f.name for f in fields(self)) def __getitem__(self, key):

[issue42742] Add abc.Mapping to dataclass

2020-12-25 Thread Eric V. Smith
Eric V. Smith added the comment: Something like that. You'd have to write some tests and try it out. -- ___ Python tracker ___ ___

[issue42742] Add abc.Mapping to dataclass

2020-12-25 Thread Anton Abrosimov
Anton Abrosimov added the comment: Thanks for the answer, I agree. The implementation should be like this? from collections.abc import Mapping from dataclasses import dataclass, fields, _FIELDS, _FIELD class _DataclassMappingMixin(Mapping): def __iter__(self): return (f.name for

[issue42742] Add abc.Mapping to dataclass

2020-12-25 Thread Eric V. Smith
Eric V. Smith added the comment: You'd need to return a different class in order to add the collections.abc.Mapping base class. Currently, dataclasses by design always return the same class that's passed in. I'd suggest adding this as a stand-alone decorator. -- assignee: ->

[issue42742] Add abc.Mapping to dataclass

2020-12-25 Thread Anton Abrosimov
New submission from Anton Abrosimov : I want to add `abc.Mapping` extension to `dataclasses.dataclass`. Motivation: 1. `asdict` makes a deep copy of the `dataclass` object. If I only want to iterate over the `field` attributes, I don't want to do a deep copy. 2. `dict(my_dataclass)` can be