Skip to content

InitVar fields should not be included in __match_args__ #114779

Open
@LeeeeT

Description

@LeeeeT

Bug report

Bug description:

Right now, dataclasses.dataclass generates a __match_args__ attribute with all the parameters present in the generated __init__ method. I'm not sure that's the right thing to do, given a dataclass could have an InitVar field adding a parameter to the __init__ but not actually creating a field that can be accessed from the dataclass.

In the following example, none of the cases matches:

from dataclasses import dataclass, InitVar

@dataclass
class Foo:
    bar: InitVar[int]
    baz: str

match Foo(123, 'tst'):
    case Foo(bar, baz):
        print(bar, baz)
    case Foo(baz):
        print(baz)

The generated __match_args__ is ('bar', 'baz'). I think the correct one should be ('baz',).

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions