-
-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extra keys #580
Comments
For now I'm using this. It's based on the fallback_field example in the docs. It support an attrs alias but not the overrides def pre_process(
converter_arg: Converter,
*,
unknown_field: str = "",
) -> typing.Any:
"""Decorator to move unknown fields to a specific field."""
def decorator(cls: typing.Any) -> typing.Any:
struct = make_dict_structure_fn(cls, converter_arg)
def structure(d: dict[str, typing.Any], cl: typing.Any) -> typing.Any:
fields = adapted_fields(get_origin(cls) or cls) # type: ignore
all_fields = {a.alias or a.name for a in fields}
unknown_f = set(d.keys()) - all_fields
d[unknown_field] = {k: d.pop(k) for k in unknown_f}
return struct(d, cl)
converter_arg.register_structure_hook(cls, structure)
return cls
return decorator |
Hello, I'm on paternity leave currently so my availability is dependent on how well our baby sleeps 😥 I was going to ask what do you want to do with the extra keys, but I see the idea is to stick them into an attribute? Your approach is pretty solid. I think you can pull out the first two lines out of If you want to support overrides, I think you can use |
Description
Is it possible to get the keys that were not used during a call to
structure(input_dict, SomeClass)
?One way would be to use
forbid_extra_keys
to get the names of these extra keys, but then you would have to run structure a second time if you still want to use the structured dataThe text was updated successfully, but these errors were encountered: