classification
Title: dataclasses non-default argument follows default argument
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, lijok, xtreak
Priority: normal Keywords:

Created on 2020-01-11 06:21 by lijok, last changed 2020-01-11 19:28 by eric.smith. This issue is now closed.

Messages (5)
msg359782 - (view) Author: lijok (lijok) Date: 2020-01-11 06:21
from dataclasses import dataclass


@dataclass
class A:
    PARAM: int


@dataclass
class B(A):
    ARG: int
    PARAM: int = 1

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\dataclasses.py", line 1021, in dataclass
    return wrap(cls)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\dataclasses.py", line 1013, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\dataclasses.py", line 927, in _process_class
    _init_fn(flds,
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\dataclasses.py", line 503, in _init_fn
    raise TypeError(f'non-default argument {f.name!r} '
TypeError: non-default argument 'ARG' follows default argument
msg359783 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-01-11 06:35
> TypeError will be raised if a field without a default value follows a field with a default value. This is true either when this occurs in a single class, or as a result of class inheritance.

I think this is a combination of the above statement at end of [0] and inheritance following the order of the fields at [1]

[0] https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass
[1] https://docs.python.org/3/library/dataclasses.html#inheritance
msg359784 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-01-11 06:38
See also issue36077
msg359787 - (view) Author: lijok (lijok) Date: 2020-01-11 07:13
> I think this is a combination of the above statement at end of [0] and inheritance following the order of the fields at [1]

Ah, I see, so if I understand correctly the init method for the example given would become __init__(self, PARAM: int = 1, ARG: int) since the fields are kept in an ordered mapping

Thank you
msg359818 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-01-11 19:28
> Ah, I see, so if I understand correctly the init method for the example given would become __init__(self, PARAM: int = 1, ARG: int) since the fields are kept in an ordered mapping

Correct.
History
Date User Action Args
2020-01-11 19:28:37eric.smithsetstatus: open -> closed
messages: + msg359818

assignee: eric.smith
resolution: not a bug
stage: resolved
2020-01-11 07:13:46lijoksetmessages: + msg359787
2020-01-11 06:38:44xtreaksetmessages: + msg359784
2020-01-11 06:35:24xtreaksetnosy: + xtreak
messages: + msg359783
2020-01-11 06:21:26lijokcreate