pathlib.Path support
See original GitHub issueIt would be nice if there were a Marshmallow field for (de)serializing pathlib.Path
objects since these are often more convenient to work with than raw strings and os.path
functions. I typically use Marshmallow via marshmallow_dataclass and can work around this with hooks but having builtin support for Path
s would be a great addition.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top Results From Across the Web
pathlib — Object-oriented filesystem paths — Python 3.11.1 ...
This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths ...
Read more >Python Path – How to Use the Pathlib Module with Examples
Before we jump into this, Pathlib divides the filesystem paths into two different classes that represent two types of path objects: Pure Path...
Read more >Python 3's pathlib Module: Taming the File System
In this tutorial, you will see how to work with file paths—names of directories and files—in Python. You will learn new ways to...
Read more >How To Use the pathlib Module to Manipulate Filesystem ...
The pathlib module is a powerful part of the Python Standard Library that lets us manipulate filesystem paths quickly on any operating system....
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 >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 FreeTop 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
Top GitHub Comments
I would recommend separate field types for validating the mutually exclusive attributes.
Using constructor args for optional validation is convenient, but providing validator classes avoids complex method signatures, if chains, and storing them all as instance properties. Much less code to maintain and more performant. You could minimize the API surface area by making a pass through validator for the boolean checks.
I’m not sure if fields for the “pure” paths would need to be exposed also.
pathlib
doesn’t seem to offer any validation when the path isn’t on the current file system.That’s really the question to me and probably what makes this a bit tricky. I could see something like this:
but this is a bit awkward since it wouldn’t make sense to have both
is_dir
andis_file
set to True. This could of course be checked for but it could lead to some odd semantics. I wonder if it might make more sense to define validators that get passed in with thevalidate
kwarg? I do think we might want some convenience boolean options such asresolve
,expanduser
, etc. which would callPath.resolve
,Path.expanduser
. Similarly it might be good to have apath_class
argument that takes in the type of path class to use.