Skip to content

Add support for pathlib.Path #81

@spacemanspiff2007

Description

@spacemanspiff2007
  • 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))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions