Sorry, Matt: I meant to credit you by name, but forgot to during my final edit.
I still don't like the verbosity and the look of 'with' statements or classes. Or the fact that some of the fields are indented relative to others.
And should also mention that Ethan Furman suggested using '*' and
'/' as the "type", in
https://mail.python.org/archives/list/python-ideas@python.org/message/BIAVX4O6JRPQY7S3XG2IX6BSBZLAR2NS/
, although the interaction with typing (specifically
get_type_hints) might be an issue:
class Hmm:
#
this: int
that: float
#
pos: '/'
#
these: str
those: str
#
key: '*'
#
some: list
Anyway, Matt's and Ethan's proposal are on the other thread. I'd
like to keep this thread focused on my proposal of
dataclasses.KW_ONLY and .POS_ONLY. Not that saying "I'd like focus
this thread" has ever worked in the history of python-ideas.
Eric
On Fri, Mar 12, 2021, 11:55 PM Eric V. Smith <eric@trueblade.com> wrote:I should mention another idea that showed up on python-ideas, at
https://mail.python.org/archives/list/python-ideas@python.org/message/WBL4X46QG2HY5ZQWYVX4MXG5LK7QXBWB/
. It would allow you to specify the flag via code like:
@dataclasses.dataclass
class Parent:
with dataclasses.positional():
a: int
c: bool = False
with dataclasses.keyword():
e: list
I'm not crazy about it, and it looks like it would require stack
inspection to get it to work, but I mention it here for completeness.
I think stack inspection could be avoided if we did something like:
```@dataclasses.dataclassclass Parent:class pos(dataclasses.PositionalOnly):a: intc: bool = Falseclass kw(dataclasses.KeywordOnly):e: list```
Like your proposal, the names for the two inner classes can be anything, but they must be unique. The metaclass would check if a field in the new class's namespace was a subclass of PositionalOnly or KeywordOnly, and if so recurse into its annotations to collect more fields.
This still seems hacky, but it seems to read reasonably nicely, and behaves obviously in the presence of subclassing.
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/IFE35VNDZH5YUNXY23I53QBDCUFB7GRQ/ Code of Conduct: http://python.org/psf/codeofconduct/
-- Eric V. Smith