pythonpython-3.xpython-dataclasses

how to gracefully ignore non-matching keyword matching arguments in python dataclass


With normal classes you have **kwargs in the __init__ so non-matching keyword arguments can be ignored:

class MyClass:
    def __init__(self, a, **kwargs):
        self.a=a

my_class = MyClass(20, **{"kwarg1" : 1})

Is there an equivalent for @dataclass that can gracefully ignore non-matching keyword arguments without having to include an __init__?

from dataclasses import dataclass

@dataclass
class MyClass:
    a: int

my_class = MyClass(20, **{"kwarg1" : 1}) # TypeError: MyClass.__init__() got an unexpected keyword argument 'kwarg1'

Solution

  • TL;DR No, it's impossible

    This scenario was even quoted in the original pep introducing dataclass.

    I think it's against the original assumptions around dataclass - which would be simplify semantics at the expense of making functionality less flexible.

    vide pep-0557

    Sometimes the generated init method does not suffice. For example, suppose you wanted to have an object to store *args and **kwargs:

    @dataclass(init=False)
    class ArgHolder:
        args: List[Any]
        kwargs: Mapping[Any, Any]
    
        def __init__(self, *args, **kwargs):
            self.args = args
            self.kwargs = kwargs
    
    a = ArgHolder(1, 2, three=3)