0

I'd like to initialize A from a dict which can contain unexpected keys. I'd like to initialize with the given, known keys and ignore the rest.

from dataclasses import dataclass

@dataclass
class A:
    a: str
    b: int

a1 = A(**{'a': 'Foo', 'b': 123})  # works
a2 = A(**{'a': 'Foo', 'b': 123, 'c': 'unexpected'})  # raises TypeError

Is there a Python feature that I'm missing or do I have to filter the dict upfront?

Mr. B.
  • 8,041
  • 14
  • 67
  • 117

0 Answers0