More convenient pathlib.Path types
Ravencentric opened this issue · 2 comments
Ravencentric commented
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:
ExistingPath
-Union[FilePath, DirectoryPath]
ResolvedFilePath
- A file that get's resolved (pathlib.Path.expanduser().resolve()
) by pydantic before returningResolvedDirectoryPath
- A directory that get's resolved (pathlib.Path.expanduser().resolve()
) by pydantic before returningResolvedPath
-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
- Compatibility between releases
- Data validation/parsing
- Data serialization -
.model_dump()
and.model_dump_json()
- JSON Schema
- Dataclasses
- Model Config
- Field Types - adding or changing a particular data type
- Function validation decorator
- Generic Models
- Other Model behaviour -
model_construct()
, pickling, private attributes, ORM mode - Plugins and integration with other tools - mypy, FastAPI, python-devtools, Hypothesis, VS Code, PyCharm, etc.
sydney-runkle commented
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 👍.
Ravencentric commented
Makes sense, thank you!