You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. I'm seriously considering using attrs as a replacement for validation in my own library. However, currently (and historically) I've used the kwarg expansion format to init my classes:
For almost all cases, attrs works perfectly out of the box. However, since my input data is typically in JSON format, their keys can sometimes contain hyphens - which cannot be resolved to python variable/attribute names. Trying to use alias like so:
@defineclassExample:
annoying_attribute: str=field(alias="annoying-attribute")
# Meaning, populate `annoying_attribute` from `annoying-attribute`
results in attrs attempting to populate the signature of the init method with the hyphenated name, which obviously results in a syntax error.
I'm already aware that the best option (and officially sanctioned by the maintainers) is to instead use a factory method with cattrs, a la:
But I am still somewhat interested in preserving the legacy behavior using init, especially since this syntax is convenient and most keywords that I'm handling are not unrepresentable in this manner. Is there any way to modify the generated init nicely without reinventing the wheel? I could simply handle the substitution myself with a custom init:
but this would of course lose the annotations which would have to be redone manually (unless there's some way to copy them from __attrs_init__?). Further, I do actually think using a cattrs converter is the right direction (since my conversion from raw JSON dict to Python object is bidirectional), but I have no idea how you would call a structure equivalent from the __init__ method...
The text was updated successfully, but these errors were encountered:
Hi. I'm seriously considering using attrs as a replacement for validation in my own library. However, currently (and historically) I've used the kwarg expansion format to init my classes:
For almost all cases, attrs works perfectly out of the box. However, since my input data is typically in JSON format, their keys can sometimes contain hyphens - which cannot be resolved to python variable/attribute names. Trying to use
alias
like so:results in attrs attempting to populate the signature of the init method with the hyphenated name, which obviously results in a syntax error.
I'm already aware that the best option (and officially sanctioned by the maintainers) is to instead use a factory method with
cattrs
, a la:But I am still somewhat interested in preserving the legacy behavior using init, especially since this syntax is convenient and most keywords that I'm handling are not unrepresentable in this manner. Is there any way to modify the generated init nicely without reinventing the wheel? I could simply handle the substitution myself with a custom init:
but this would of course lose the annotations which would have to be redone manually (unless there's some way to copy them from
__attrs_init__
?). Further, I do actually think using a cattrs converter is the right direction (since my conversion from raw JSON dict to Python object is bidirectional), but I have no idea how you would call astructure
equivalent from the__init__
method...The text was updated successfully, but these errors were encountered: