Skip to content

Commit

Permalink
Keep v1 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbull committed Jul 10, 2023
1 parent 65d7911 commit 0b9a1b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
13 changes: 13 additions & 0 deletions cloudpathlib/anypath.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ def validate(cls, v: str) -> Union[CloudPath, Path]:
# https://docs.pydantic.dev/2.0/migration/#typeerror-is-no-longer-converted-to-validationerror-in-validators
raise ValueError(e)

@classmethod
def __get_validators__(cls):
"""Pydantic special method. See
https://pydantic-docs.helpmanual.io/usage/types/#custom-data-types"""
yield cls._validate

@classmethod
def _validate(cls, value) -> Union[CloudPath, Path]:
"""Used as a Pydantic validator. See
https://pydantic-docs.helpmanual.io/usage/types/#custom-data-types"""
# Note __new__ is static method and not a class method
return cls.__new__(cls, value)


AnyPath.register(CloudPath) # type: ignore
AnyPath.register(Path)
Expand Down
12 changes: 12 additions & 0 deletions cloudpathlib/cloudpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,18 @@ def validate(cls, v: str) -> Self:
https://docs.pydantic.dev/2.0/usage/types/custom/"""
return cls(v)

@classmethod
def __get_validators__(cls) -> Generator[Callable[[Any], Self], None, None]:
"""Pydantic special method. See
https://pydantic-docs.helpmanual.io/usage/types/#custom-data-types"""
yield cls._validate

@classmethod
def _validate(cls, value: Any) -> Self:
"""Used as a Pydantic validator. See
https://pydantic-docs.helpmanual.io/usage/types/#custom-data-types"""
return cls(value)


# The function resolve is not available on Pure paths because it removes relative
# paths and symlinks. We _just_ want the relative path resolution for
Expand Down
9 changes: 0 additions & 9 deletions docs/docs/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@

`cloudpathlib` integrates with [Pydantic](https://pydantic-docs.helpmanual.io/)'s data validation. You can declare fields with cloud path classes, and Pydantic's validation mechanisms will run inputs through the cloud path's constructor.

### Compatibility table

| `pydantic` version | `cloudpathlib` version |
| --- | --- |
| `>=2.0.0` | `>=0.15.2` |
| `<2.0.0` | `<0.15.2` |

### Example

```python
from cloudpathlib import S3Path
from pydantic import BaseModel
Expand Down

0 comments on commit 0b9a1b1

Please sign in to comment.