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
When unstructuring an attrs class where one of the fields is annotated as os.PathLike (and contains a Path), that field remains a Path (in my case, PosixPath) instead of being unstructured as str.
Curiously, annotating the field as Union[str, os.PathLike] or pathlib.Path (without changing the actual data type) resolves the issue.
I'm not saying that this must be a bug (rather than merely my mis-usage of cattrs), but I was not able to find anything in the docs that would suggest that I was doing something incorrectly.
The text was updated successfully, but these errors were encountered:
We don't support os.PathLike, no one has asked for it yet. pathlib.Path is supported though.
When generating the unstructuring code, cattrs looks at the type of the field in the class rather than the actual runtime value of the field. If cattrs doesn't know how to unstructure a field, it just passes it through, which is what you're seeing. (For structuring it's different, there it would be an exception.)
To solve the issue, you can register a super simple hook yourself:
If cattrs doesn't know how to unstructure a field, it just passes it through, which is what you're seeing.
I see. This was not obvious from the documentation.
Perhaps there is value in a "strict mode" that would ensure that either everything is unstructured to primitive types or an exception is thrown? Such a mode could then be auto-enabled for the preconfigured converters.
Description
When unstructuring an attrs class where one of the fields is annotated as
os.PathLike
(and contains a Path), that field remains a Path (in my case,PosixPath
) instead of being unstructured asstr
.Curiously, annotating the field as
Union[str, os.PathLike]
orpathlib.Path
(without changing the actual data type) resolves the issue.What I Did
I'm not saying that this must be a bug (rather than merely my mis-usage of
cattrs
), but I was not able to find anything in the docs that would suggest that I was doing something incorrectly.The text was updated successfully, but these errors were encountered: