-
-
Notifications
You must be signed in to change notification settings - Fork 119
Still not able to structure using an alias #553
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
Comments
So that's actually working as intended, see https://catt.rs/en/latest/customizing.html#use-alias for more details. Aliases aren't used by default, that would have been a large compatibility break when it was introduced in attrs. Here's a snippet to enable them for your class: from attrs import field, mutable
from cattrs import Converter
from cattrs.gen import make_dict_structure_fn
@mutable
class Thing:
one: int
_two: int = field(alias="two")
c = Converter()
c.register_structure_hook(
Thing, make_dict_structure_fn(Thing, c, _cattrs_use_alias=True)
)
c.structure({"one": 1, "two": 2}, Thing) They can also be enabled for all attrs classes using a hook factory, let me know if you'd like an example for that. |
PSA: Make sure you call |
I know you're joking but if you apply the |
Description
It is still not possible to structure a dict using the
alias
of a field. Supposedly this was fixed in #322.Example:
This is due to the line obtaining the value of the to-be-structured data via name only, in `BaseConverter.structure_attrs_fromdict:
cattrs/src/cattrs/converters.py
Line 733 in 6290cac
The fix is easy, but I am only authorized to file issues, not fixes, with this account. Will submit MR when possible.
The text was updated successfully, but these errors were encountered: