Add support for pathlib.Path
See original GitHub issue- cattrs version: 0.9.0
- Python version: 3.7
- Operating System: Win
Description
I am trying to serialize a class with a pathlib.Path object
What I Did
from pathlib import Path
import attr
@attr.s
class PathTest():
my_path: Path = attr.ib(default=Path('c:/test'))
cattr.structure({'my_path': 'c:/my_filepath',}, PathTest)
Error:
ValueError: Unsupported type: <class 'pathlib.Path'>. Register a structure hook for it.
The fix is easy, but I really feel like this is a fundamental type and should be supported out of the box
cattr.register_structure_hook(Path, lambda d, t: Path(d))
cattr.register_unstructure_hook(Path, lambda d: str(d))
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:10 (5 by maintainers)
Top Results From Across the Web
pathlib — Object-oriented filesystem paths — Python 3.11.1 ...
Source code: Lib/pathlib.py This module offers classes representing filesystem paths with semantics appropriate for different operating systems.
Read more >Add support for pathlib.Path type #97 - omry/omegaconf - GitHub
Once stronger typing is implemented in the library, consider adding support for the pathlib.Path type. Motivation: paths are often found in ...
Read more >Add support for pathlib.Path objects to arcpy - Esri Community
pathlib seems to be the preferred method for working with system paths in Python 3. We've started using it in place of `os.path`...
Read more >Python Path – How to Use the Pathlib Module with Examples
In this example, we import the Pathlib module. Then, we create a new variable called p to store the path. Here, we use...
Read more >Python 3's pathlib Module: Taming the File System
Early on, other packages still used strings for file paths, but as of Python 3.6, the pathlib module is supported throughout the standard...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Alright, I consuled some other folks and I’ve changed my mind. Let’s add this.
Yep, that works. I love the way Python is supposed to be pseudocode like and easy to read and understand 😉 🙄
My lazy workaround was:
🤣