This was discussed on this thread: https://mail.python.org/archives/list/python-ideas@python.org/message/I3RKK4VINZUBCGF2TBJN6HTDV3PVUEUQ/

Ultimately the thing that doomed it for me was the repr that would be required for eval(repr(dataclass_instance)) to work. You'd have to drop the field name, combined with re-ordering the parameters. It just seemed ugly, and it's not a battle I'm willing to fight while trying to keyword-only in to 3.10.

But I believe this current proposal is a subset of that one, and it could be added in the future if demand is high.

Also, note that attrs doesn't support this, and I don't think we need to be blazing a trail here.

Eric

On 3/16/2021 12:17 PM, Abdulla Al Kathiri wrote:
Why don’t we also add dataclasses.POS_ONLY as well? Anything before it is positional argument. 

@dataclasses.dataclass
class LotsOfFields:
    x: Any 
    y: Any
    z: Any 
    _:dataclasses.POS_ONLY 
    a: Any
    __: dataclasses.KW_ONLY
    b: Any = 0
    c: Any = 'foo'
    d: Any
    e: Any = 0.0
    f: Any
    g: Any = ()
    h: Any = 'bar'
    i: Any = 3+4j
    j: Any = 10
    k: Any

The generated __init__ would look like:

def __init__(self, x, y, z, /, a, *, b=0, c='foo', d, e=0.0, f, g=(), h='bar', i=3+4j, j=10, k):

Similar to dataclasses.KW_ONLY, you can use dataclasses.POS_ONLY only once. It should always come before dataclasses.KW_ONLY. Anything else, it should raise an error. If dataclasses.POS_ONLY came right after the class declaration (no instance attribute preceding it), an error is generated similar to what we have right now. Inheritance is build up (from super class to sub class) of three groups of arguments: pos_only arguments together, then mixed arguments, and then kw_only arguments. repr should work the same way. We would like to reconstruct the object from repr. We don’t want too much change from current behavior. 

Lets create a class that inherits from the above class.. 

@dataclasses.dataclass
class LessFields(LotsOfFields):
x2: Any
_: dataclasses.POS_ONLY 
a2: Any 
__:dataclasses.KW_ONLY 
b2: Any 

The generated __init__ would look like:

def __init__(self, x, y, z, x2, /, a, a2, *, b=0, c='foo', d, e=0.0, f, g=(), h='bar', i=3+4j, j=10, k, b2):

Another class.. 

@dataclasses.dataclass 
Class EvenLessFields(LessFields):
a3: Any
_: dataclasses.KW_ONLY
b3: Any = 9 

The generated __init__ would look like:

def __init__(self, x, y, z, x2, /, a, a2, a3, *, b=0, c='foo', d, e=0.0, f, g=(), h='bar', i=3+4j, j=10, k, b2, b3=9):

All arguments are ordered from the super super class until the subclass all in their respective path or group of arguments. 

Abdulla 

On 16 Mar 2021, at 1:18 AM, Eric V. Smith <eric@trueblade.com> wrote:

@dataclasses.dataclass
class LotsOfFields:
    a: Any
    _: dataclasses.KW_ONLY
    b: Any = 0
    c: Any = 'foo'
    d: Any
    e: Any = 0.0
    f: Any
    g: Any = ()
    h: Any = 'bar'
    i: Any = 3+4j
    j: Any = 10
    k: Any

Which I think is a lot clearer.

The generated __init__ would look like:

def __init__(self, a, *, b=0, c='foo', d, e=0.0, f, g=(), h='bar', i=3+4j, j=10, k):


_______________________________________________
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/5SA2XVRAGCPHTVTFO3WCD36BDARY2MTA/
Code of Conduct: http://python.org/psf/codeofconduct/
-- 
Eric V. Smith