Python: Dealing with unknown fields in dataclass
By Eric Downing | Filed in Programming, Python | No comments yet.I have been using dataclasses in my python data retrieval scripts but on the occasions when a field is added to the API return I needed a way to make the scripts continue to run. The added benefit of notifying the script user about any unknown fields helps too.
The following code with a Warning message to show any fields that are not predefined in the dataclass.
from dataclasses import dataclass
from dataclasses import fields
def filter_unexpected_fields(cls):
original_init = cls.__init__
def new_init(self, *args, **kwargs):
expected_fields = {field.name for field in fields(cls)}
cleaned_kwargs = {key: value for key, value in kwargs.items() if key in expected_fields}
unknown_kwargs = {key: value for key, value in kwargs.items() if key not in expected_fields}
original_init(self, *args, **cleaned_kwargs)
# Handle unknown fields (optional)
if unknown_kwargs:
# Log a warning or raise an exception based on your needs
print(f"Warning: Unknown fields found: {unknown_kwargs}")
cls.__init__ = new_init
return cls
@filter_unexpected_fields
@dataclass
class Application:
id: int
name: str
a1 = { 'id': 1, 'name': 'WebGoat',"runs":5}
app = Application(**a1)
So given the following code, the “runs” field is not defined in the dataclass object, but with the “@filter_unexpected_fields” decorator, the code will continue to work but the additional warning message will notify about any undefined fields.
1 based on answer on this page: https://stackoverflow.com/questions/54678337/how-does-one-ignore-extra-arguments-passed-to-a-dataclass