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
The whole prefix and handling is unimportant, it's just the interface here that I want to highlight.
Basically I want to use some default structure/unstructure functions (just changing _ to -), but allow some special fields to have a more complicated handling if needed by decorating some functions to handle things in a different way. The part where I extend the field decorators provided by attrs is related to python-attrs/attrs#1543, let's just assume that that just works. The obstacle here is that the struct_hook/unstruct_hook do not accept self/cls interface as the first argument is always the raw value to be translated.
An interesting idea. Note that it's somewhat against the spirit of cattrs to stick serialization logic on the models, but we've been relaxing this over the years (see the use_class_methods strategy and the Annotated support in the last release).
I wonder if an attrs field transformer could be used to change the type of special_field from str to Annotated[str, override(struct_hook=..., unstruct_hook=...)]. This is also where you could adapt Example._special_field_unstructure(attrib, value) into the cattrs unstructure signature.
Oh neat, did not know that is already in since attrs does not seem to have a support for it yet. That is indeed an interesting way to avoid some de-duplication. But a question for both attrs and cattrs on that subject, what would third-party extensions look like in that context. I could see the third-party extensions extending upon something like
class AttrsMetadata:
_Metadata_name: ClassVar[str]
"""The dict key to populate in the field metadata""""
@attrs.define
class MyMetadata(AttrsMetadata):
_Metadata_name = "my_ext"
factor: int
@attrs.define
class Example:
var: Annotated[int, MyMetadata(5)] = field()
@var.converter
def _var_convert(self, attribute, value):
return value * attribute.metadata["my_ext"].factor
see the use_class_methods strategy
I see, that is a bit different. That creates a structure/unstructure for the whole class, but what I am looking for is more specific to the specific fields.
I wonder if an attrs field transformer could be used to change the type of special_field from str to Annotated[str, override(struct_hook=..., unstruct_hook=...)].
Hmm also neat, will play around with it. What interface I was looking initially is the @<field>.custom_decorator, which kinda works pending a patch and a usage of an inner class. If the thing is converted to Annotated, then using generic TypeAlias would be better.
The one neat thing of the decorator approach is the ability to chain the functions. That could also work for the struct/unstruct to build up from a default handler.
Activity
Tinche commentedon Apr 14, 2026
An interesting idea. Note that it's somewhat against the spirit of cattrs to stick serialization logic on the models, but we've been relaxing this over the years (see the use_class_methods strategy and the
Annotatedsupport in the last release).I wonder if an attrs field transformer could be used to change the type of
special_fieldfromstrtoAnnotated[str, override(struct_hook=..., unstruct_hook=...)]. This is also where you could adaptExample._special_field_unstructure(attrib, value)into the cattrs unstructure signature.LecrisUT commentedon Apr 14, 2026
Oh neat, did not know that is already in since
attrsdoes not seem to have a support for it yet. That is indeed an interesting way to avoid some de-duplication. But a question for bothattrsandcattrson that subject, what would third-party extensions look like in that context. I could see the third-party extensions extending upon something likeI see, that is a bit different. That creates a structure/unstructure for the whole class, but what I am looking for is more specific to the specific fields.
Hmm also neat, will play around with it. What interface I was looking initially is the
@<field>.custom_decorator, which kinda works pending a patch and a usage of an inner class. If the thing is converted toAnnotated, then using generic TypeAlias would be better.The one neat thing of the decorator approach is the ability to chain the functions. That could also work for the struct/unstruct to build up from a default handler.