pep-0557 dataclasses top level module vs part of collections?
It seems a suggested use is "from dataclasses import dataclass" But people are already familiar with "from collections import namedtuple" which suggests to me that "from collections import dataclass" would be a more natural sounding API addition. But the dataclasses module has additional APIs beyond @dataclass which clearly do not belong at the top level in collections. Idea: How about making the current dataclasses.dataclass decorator function instead be a callable class instance (ie: it still functions as property, todays dataclasses.dataclass becomes collections.dataclass.__call__) with all of the current contents of the dataclasses module as attributes of a collections.dataclass class/instance singleton? It feels like a more natural API to me: from collections import dataclass @dataclass class ... and the following APIs show up on dataclass itself: dataclass.Field, dataclass.field, dataclass.fields, dataclass.make, dataclass.astuple, dataclass.replace, dataclass.asdict, dataclass.FrozenInstanceError, dataclass.InitVar instead of being in a separate dataclasses module and being a different style of thing to import than namedtuple. [ if this was discussed earlier for this pep and rejected and I missed it, my apologies, just drop me a reference to that thread if you've got one ] This isn't a blocker for me. I like having a dataclass implementation no matter how we arrange it. If we go with what's checked in today, a top level dataclasses module, so be it. I'm not going to bikeshed this to death it just feels odd to have such an API outside of collections but figured it was worth suggesting. Part of me just doesn't like the plural dataclasses module name. I can get over that. -gps
On Thu, Dec 21, 2017 at 4:21 PM, Gregory P. Smith <greg@krypto.org> wrote:
It seems a suggested use is "from dataclasses import dataclass"
But people are already familiar with "from collections import namedtuple" which suggests to me that "from collections import dataclass" would be a more natural sounding API addition.
FWIW, I'd consider this a good time to add a new top-level classtools/classutils module (a la functools). There are plenty of other things that would fit there that we've shoved into other places. -eric
On Dec 21, 2017, at 3:21 PM, Gregory P. Smith <greg@krypto.org> wrote:
It seems a suggested use is "from dataclasses import dataclass"
But people are already familiar with "from collections import namedtuple" which suggests to me that "from collections import dataclass" would be a more natural sounding API addition.
This might make sense if it were a single self contained function. But dataclasses are their own little ecosystem that warrants its own module namespace:
import dataclasses dataclasses.__all__ ['dataclass', 'field', 'FrozenInstanceError', 'InitVar', 'fields', 'asdict', 'astuple', 'make_dataclass', 'replace']
Also, remember that dataclasses have a dual role as a data holder (which is collection-like) and as a generator of boilerplate code (which is more like functools.total_ordering). I support Eric's decision to make this a separate module. Raymond
On Thu, Dec 21, 2017 at 10:47 PM Raymond Hettinger < raymond.hettinger@gmail.com> wrote:
On Dec 21, 2017, at 3:21 PM, Gregory P. Smith <greg@krypto.org> wrote:
It seems a suggested use is "from dataclasses import dataclass"
But people are already familiar with "from collections import namedtuple" which suggests to me that "from collections import dataclass" would be a more natural sounding API addition.
This might make sense if it were a single self contained function. But dataclasses are their own little ecosystem that warrants its own module namespace:
import dataclasses dataclasses.__all__ ['dataclass', 'field', 'FrozenInstanceError', 'InitVar', 'fields', 'asdict', 'astuple', 'make_dataclass', 'replace']
Also, remember that dataclasses have a dual role as a data holder (which is collection-like) and as a generator of boilerplate code (which is more like functools.total_ordering).
I support Eric's decision to make this a separate module.
sounds good. lets leave it that way. dataclasses it is. if we were further along in figuring out how to remove the distinction between a class and a module as a namespace I'd suggest the module name itself be dataclass with a __call__ method so that the module could be the decorator so we could avoid the antipattern of importing a name from a module into your local namespace. but we're not, so we can't. :) -gps
Raymond
participants (3)
-
Eric Snow -
Gregory P. Smith -
Raymond Hettinger