Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More convenient pathlib.Path types #149

Open
3 of 13 tasks
Ravencentric opened this issue Feb 29, 2024 · 2 comments
Open
3 of 13 tasks

More convenient pathlib.Path types #149

Ravencentric opened this issue Feb 29, 2024 · 2 comments

Comments

@Ravencentric
Copy link

Ravencentric commented Feb 29, 2024

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

Working with Paths is fairly common and I usually end up writing my own field validator over and over again because the type I want is missing. I would like to see the Path types in pydantic extended with some more potentially useful types:

  1. ExistingPath - Union[FilePath, DirectoryPath]
  2. ResolvedFilePath - A file that get's resolved (pathlib.Path.expanduser().resolve()) by pydantic before returning
  3. ResolvedDirectoryPath - A directory that get's resolved (pathlib.Path.expanduser().resolve()) by pydantic before returning
  4. ResolvedPath - Union[ResolvedFilePath, ResolvedDirectoryPath]

My current solution is to just write a field validator:

    @field_validator("path_a", "path_b")
    @classmethod
    def resolve_path(cls, path: Path) -> Path:
            """Resolve all given Path fields"""
            return path.expanduser().resolve()

Affected Components

@sydney-runkle
Copy link
Member

@Ravencentric,

Thanks for the feature request. I'm sure many people would appreciate extended support for these types. The right place for these, though, is in pydantic-extra-types. I'll transfer this issue over there 👍.

@sydney-runkle sydney-runkle transferred this issue from pydantic/pydantic Mar 1, 2024
@Ravencentric
Copy link
Author

Makes sense, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants