Skip to content

Commit

Permalink
OPT: updated code to Pydantic 2.0 (get rid of deprecation warnings) (#98
Browse files Browse the repository at this point in the history
)

- use Pydantic 2.x syntax

---------

Co-authored-by: wieri494 <[email protected]>
Co-authored-by: Pavel Perestoronin <[email protected]>
  • Loading branch information
3 people authored Feb 2, 2024
1 parent e0de195 commit dcdf297
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
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(
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")
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(
pickle.HIGHEST_PROTOCOL,
alias="pickle-protocol",
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ build-backend = "poetry_dynamic_versioning.backend"
[tool.poetry.dependencies]
django = {version = "^4.0.0", optional = true}
ormsgpack = {version = "^1.4.0", optional = true, markers = "platform_python_implementation == 'CPython'"}
pydantic = "<3.0.0"
pydantic = ">2.0.0.0, <3.0.0.0"
python = "^3.8.0"
redis = {version = "^4.4.2 || ^5.0.0", optional = true}
typing-extensions = "^4.4.0"
Expand Down

0 comments on commit dcdf297

Please sign in to comment.