conchylicultor <etiennefg....@gmail.com> added the comment:

Solving this would significantly improve @dataclass inheritance 
(https://bugs.python.org/issue39300). Any idea when this will land ?

Currently, it is not possible to add required argument to a child dataclass 
(unless hacks like duplicating attributes):

```
import dataclasses

@dataclasses.dataclass
class A:
  x: int = 1


@dataclasses.dataclass
class B(A):
  y: int  # ERROR: non-default argument follows default argument
```

Keywords-only would solve this issue:

```
@dataclasses.dataclass
class B(A):
  y: int = dataclasses.field(kw_only=True)


B(123, y=456)
```

Note: Attrs already support this: 
https://www.attrs.org/en/stable/examples.html#keyword-only-attributes


For the behavior, I think there are two options:

```
class A:
  a0: int
  a1: int = field(kw_only=True)
  a2: int


class B(A):
  b0: int
  b1: int = field(kw_only=True)
  b2: int
```


Option 1: All attributes following `kw_only` are `kw_only` (kw_only indicates 
where to place '*')

A(a0, *, a1, a2)
B(a0, b0, *, a1, a2, b1, b2)

Option 2: `kw_only` are individually moved to the end

A(a0, a2, *, a1)
B(a0, a2, b0, b2, *, a1, b1)

I personally prefer Option 1, as it makes it easier to place the `*` with less 
boilerplate.

```
class Request:
  url: str
  timeout: int = field(default=-1, kw_only=True)
  verify: bool = False
  params: Optional[Dict[str, str]] = None
  cookies: Optional[Dict[str, str]] = None

Request(url, *, timeout=-1, verify=False, params=None, cookies=None)
```

Which looks better than:

```
class Request:
  url: str
  timeout: int = field(default=-1, kw_only=True)
  verify: bool = field(default=False, kw_only=True)
  params: Optional[Dict[str, str]] = field(default=None, kw_only=True)
  cookies: Optional[Dict[str, str]] = field(default=None, kw_only=True)
```

----------
nosy: +conchylicultor

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33129>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to