-
-
Notifications
You must be signed in to change notification settings - Fork 118
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
cattrs does not adhere to PEP 593 #127
Comments
I have just found a workaround: from typing import get_origin
def can_handle(cls):
return issubclass(get_origin(cls), Annotated)
def unstructure(annotated):
return converter.unstructure(annotated)
converter.register_unstructure_hook_func(can_handle, unstructure) |
Yep, we don't support Your workaround is good, but it won't cover certain cases - it'll unstructure according to the runtime type of something, but unstructuring an attrs class actually unstructures the fields according to their type annotation. For example you might have a hook registered for I'm attaching a solution to handle this properly if you're in a hurry (using some internal stuff):
|
Thank you very much for this! I think if Annotated is to be considered in cattrs, it is an interesting question of "where to plug it in". What I mean is: Only fall back to serializing it as the first argument of the Annotated attribute if the user did not register a hook that wants to work with them. |
Ah, you mean support hooks for |
Yup. Annotations could be a means to modify the runtime behavior of (un-)structuring. Example: Adding a "DoNotUnstructure" sentinel for annotating fields that should not be unstructured, i.e., not shown when unstructuring. It does not interfere with the typing system and should not disturb anything really, as every library should just ignore annotations it doesn't know. Of course I do not really want that as a feature per se, but this is where I see Annotated going. Take attrs. Maybe we won't need "field" in the future because everything is done in Annotated. |
Preliminary support added here: https://github.com/Tinche/cattrs/tree/feature/annotated |
Released as 1.4.0. |
Description
PEP 593 introduced
Annotated
intyping
.According to this PEP
What I Did
The text was updated successfully, but these errors were encountered: