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.

Author Aaron Ecay
Recipients Aaron Ecay
Date 2019-11-06.12:50:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1573044600.53.0.155776883054.issue38719@roundup.psfhosted.org>
In-reply-to
Content
I have discovered that InitVar's are passed in a surprising way to the __post_init__ method of python dataclasses.  The following program illustrates the problem:

=====

from dataclasses import InitVar, dataclass

@dataclass
class Foo:
    bar: InitVar[str]
    quux: InitVar[str]

    def __post_init__(self, quux: str, bar: str) -> None:
        print(f"bar is {bar}; quux is {quux}")

Foo(bar="a", quux="b")

=====

The output (on python 3.7.3 and 3.8.0a3) is (incorrectly):

bar is b; quux is a

This behavior seems like a bug to me, do you agree?

I have not looked into the reason why it behaves this way, but I suspect that the InitVar args are passed positionally, rather than as key words, to __post_init__.  This requires the order of arguments in the definition of __post_init__ to be identical to the order in which they are specified in the class.  I would expect the arguments to be passed as keywords instead, which would remove the ordering dependency.  If there is agreement that the current behavior is undesirable, I can look into creating a patch to change it.
History
Date User Action Args
2019-11-06 12:50:00Aaron Ecaysetrecipients: + Aaron Ecay
2019-11-06 12:50:00Aaron Ecaysetmessageid: <1573044600.53.0.155776883054.issue38719@roundup.psfhosted.org>
2019-11-06 12:50:00Aaron Ecaylinkissue38719 messages
2019-11-06 12:50:00Aaron Ecaycreate