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

OPT: updated code to Pydantic 2.0 (get rid of deprecation warnings) #98

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cachetory/serializers/compressors/zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import zlib
from urllib.parse import parse_qsl, urlparse

from pydantic import BaseModel, Field, conint
from pydantic import BaseModel, Field
from typing_extensions import Annotated

from cachetory.interfaces.serializers import Serializer

Expand All @@ -28,7 +29,7 @@ def from_url(cls, url: str) -> ZlibCompressor:
|---------------------|-----------------------------------------------------------------|
| `compression-level` | From `0` (no compression) to `9` (slowest and best compression) |
"""
params = _UrlParams.parse_obj(dict(parse_qsl(urlparse(url).query)))
params = _UrlParams.model_validate(dict(parse_qsl(urlparse(url).query)))
return cls(compression_level=params.compression_level)

def __init__(self, *, compression_level: int = zlib.Z_DEFAULT_COMPRESSION) -> None:
Expand All @@ -42,7 +43,7 @@ def deserialize(self, data: bytes) -> bytes:


class _UrlParams(BaseModel):
compression_level: conint(ge=-1, le=9) = Field( # type: ignore
compression_level: Annotated[int, Field(ge=-1, le=9)] = Field( # type: ignore
zlib.Z_DEFAULT_COMPRESSION,
alias="compression-level",
)
7 changes: 4 additions & 3 deletions cachetory/serializers/compressors/zstd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from urllib.parse import parse_qsl, urlparse

import zstd # type: ignore
from pydantic import BaseModel, Field, conint
from pydantic import BaseModel, Field
from typing_extensions import Annotated

from cachetory.interfaces.serializers import Serializer

Expand Down Expand Up @@ -35,7 +36,7 @@ def from_url(cls, url: str) -> ZstdCompressor:
| `compression-level` | [Compression level](https://github.com/sergey-dryabzhinsky/python-zstd#api) |
| `compression-threads` | [Number of threads](https://github.com/sergey-dryabzhinsky/python-zstd#api) |
"""
params = _UrlParams.parse_obj(dict(parse_qsl(urlparse(url).query)))
params = _UrlParams.model_validate(dict(parse_qsl(urlparse(url).query)))
return cls(compression_level=params.compression_level, compression_threads=params.compression_threads)

def __init__(
Expand All @@ -56,4 +57,4 @@ def deserialize(self, data: bytes) -> bytes:

class _UrlParams(BaseModel):
compression_level: int = Field(3, alias="compression-level")
compression_threads: conint(ge=0) = Field(0, alias="compression-threads") # type: ignore
compression_threads: Annotated[int, Field(ge=0)] = Field(0, alias="compression-threads") # type: ignore
7 changes: 4 additions & 3 deletions cachetory/serializers/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from typing import Generic
from urllib.parse import parse_qsl, urlparse

from pydantic import BaseModel, Field, conint
from pydantic import BaseModel, Field
from typing_extensions import Annotated

from cachetory.interfaces.serializers import Serializer, ValueT

Expand Down Expand Up @@ -33,7 +34,7 @@ def from_url(cls, url: str) -> PickleSerializer[ValueT]:
|-------------------|---------------------------|
| `pickle-protocol` | `pickle` protocol version |
"""
params = _UrlParams.parse_obj(dict(parse_qsl(urlparse(url).query)))
params = _UrlParams.model_validate(dict(parse_qsl(urlparse(url).query)))
return cls(pickle_protocol=params.pickle_protocol)

def __init__(self, pickle_protocol: int = pickle.HIGHEST_PROTOCOL) -> None:
Expand All @@ -53,7 +54,7 @@ def deserialize(self, data: bytes) -> ValueT:


class _UrlParams(BaseModel):
pickle_protocol: conint(ge=0, le=pickle.HIGHEST_PROTOCOL) = Field( # type: ignore
pickle_protocol: Annotated[int, Field(ge=0, le=pickle.HIGHEST_PROTOCOL)] = Field( # type: ignore
pickle.HIGHEST_PROTOCOL,
alias="pickle-protocol",
)
Loading