This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dataclasses.make_dataclass: Exception when called with different field orders
Type: behavior Stage: resolved
Components: Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Darrick Yee, eric.smith
Priority: normal Keywords:

Created on 2020-05-27 17:23 by Darrick Yee, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg370112 - (view) Author: Darrick Yee (Darrick Yee) Date: 2020-05-27 17:23
https://docs.python.org/3/library/dataclasses.html#module-dataclasses

`make_dataclass` takes a `field` parameter, which is an iterable whose entries may be tuples of `(name, type)` or `(name, type, dataclasses.Field)`.  However, an exception is thrown if such tuples are provided in a particular order.  Example:


from dataclasses import field, make_dataclass

fieldspec1 = ('field1', str)
fieldspec2 = ('field2', str, field(default='Hello'))

MyDc1 = make_dataclass('MyDc1', [fieldspec1, fieldspec2]) # Ok
MyDc2 = make_dataclass('MyDc2', [fieldspec2, fieldspec1]) # TypeError

I am guessing this is not intentional...
msg370113 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-05-27 17:26
Isn't this error that you're providing a field without a default value after one with a default value?

What's the exception look like?
msg370114 - (view) Author: Darrick Yee (Darrick Yee) Date: 2020-05-27 17:33
TypeError: non-default argument 'field1' follows default argument

You are right.  For some reason I believed it would automatically gather the required fields first when creating the new class' signature.
msg370115 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-05-27 17:40
No problem!

It can't reorder fields, because you might not be passing them by name. There's some discussion about requiring keyword-only parameters, in which case what you're doing would work (as long as they were keyword-only params).
msg370116 - (view) Author: Darrick Yee (Darrick Yee) Date: 2020-05-27 17:42
Yes, that makes sense. Thank you!

On Wed, May 27, 2020, 1:40 PM Eric V. Smith <report@bugs.python.org> wrote:

>
> Eric V. Smith <eric@trueblade.com> added the comment:
>
> No problem!
>
> It can't reorder fields, because you might not be passing them by name.
> There's some discussion about requiring keyword-only parameters, in which
> case what you're doing would work (as long as they were keyword-only
> params).
>
> ----------
> resolution:  -> not a bug
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue40796>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:59:31adminsetgithub: 84973
2020-05-27 17:42:32Darrick Yeesetmessages: + msg370116
2020-05-27 17:40:16eric.smithsetresolution: not a bug
messages: + msg370115
2020-05-27 17:34:06Darrick Yeesetstatus: open -> closed
stage: resolved
2020-05-27 17:33:48Darrick Yeesetmessages: + msg370114
2020-05-27 17:26:47eric.smithsetnosy: + eric.smith
messages: + msg370113
2020-05-27 17:23:17Darrick Yeecreate