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.

pathlib.Path support

See original GitHub issue

It 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 Paths would be a great addition.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, Aug 6, 2021

I would recommend separate field types for validating the mutually exclusive attributes.

class Path(Field)
class Directory(Path)
class File(Path)

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.

class Relative(Validator)
class Path(Validator):
    ...
    def __call__(self, path):
        for name, expected in self.checks:
            actual = getattr(path, name)()
            if  actual != expected:
                raise ValidationError(...)
Path(validate=[validate.Path(is_absolute=True), validate.Relative('/var/www/')])

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.

0reactions
mivadecommented, Aug 6, 2021

What would the is_dir/file/mount/... API look like as field validation?

That’s really the question to me and probably what makes this a bit tricky. I could see something like this:

def __init__(
    self,
    exists: Optional[bool] = None,
    is_dir: Optional[bool] = None,
    is_mount: Optional[bool] = None,
    is_file: Optional[bool] = None,
    ...
) -> None:
    ....

but this is a bit awkward since it wouldn’t make sense to have both is_dir and is_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 the validate kwarg? I do think we might want some convenience boolean options such as resolve, expanduser, etc. which would call Path.resolve, Path.expanduser. Similarly it might be good to have a path_class argument that takes in the type of path class to use.

Read more comments on GitHub >

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

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