On Fri, Mar 12, 2021, 11:55 PM Eric V. Smith <e...@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.dataclass
class Parent:
     class pos(dataclasses.PositionalOnly):
         a: int
     c: bool = False
     class 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-le...@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/

Reply via email to