question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 4 years ago
  • Reactions:9
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Tinchecommented, Aug 7, 2022

Alright, I consuled some other folks and I’ve changed my mind. Let’s add this.

1reaction
brendan-simon-indtcommented, Aug 23, 2022

Yep, that works. I love the way Python is supposed to be pseudocode like and easy to read and understand 😉 🙄

My lazy workaround was:

cattr.register_unstructure_hook( Path, lambda d: str(d) )       # type: ignore

🤣

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found