-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
Structuring arbitrary types by identity ? #393
Comments
Yeah, I think we'll need something like this. #278 is also a form of this I feel. (Note that there's a preconfigured converter for pyyaml at https://github.com/python-attrs/cattrs/blob/main/src/cattrs/preconf/pyyaml.py) I'm not super sure how to do this exactly yet. What I'm thinking is, we add a converter parameter that's essentially a set of types the underlying serialization framework can handle, and add some default hooks making use of this. These types can just be validated by checking their class. |
I have a similar use case that might need this. Our input comes from The default |
I looked into |
Just check #278, I think the snippet should be sufficient for our use case, but it will be nice to have something similar built into the pre-configured converters. |
@PIG208 That sounds like a separate thing. If cattrs is calling You mention per-type overrides, we already support several forms of this. Might be interesting to go into details here. |
Right. My |
Yeah, that's a different thing. You can easily customize this: from cattrs import Converter
c = Converter()
def validate_string(val, _) -> str:
if not isinstance(val, str):
raise TypeError()
return val
c.register_structure_hook(str, validate_string) |
Alright, a small update here. In #403, I've snuck in As for adding support for arbitrary single types, we can point folks to #393 (comment) which should be simple and powerful enough. |
Description
Hi, I've attrs models with datetime.datetime and datetime.date fields (parsed by pyyaml) which are not supported by base converter.
I wonder if we could have a default structure hook for unknown classes that return given object if its class match expected class ?
What I Did
Minimal example:
Gives:
Related to #50
The text was updated successfully, but these errors were encountered: