Skip to content

Commit 6fb6c33

Browse files
authored
Fixed deprecation warnings (#168)
1 parent 2127c07 commit 6fb6c33

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

taskiq/compat.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
PYDANTIC_VER = parse(version("pydantic"))
99

1010
Model = TypeVar("Model", bound="pydantic.BaseModel")
11+
IS_PYDANTIC2 = PYDANTIC_VER >= Version("2.0")
1112

12-
13-
if PYDANTIC_VER >= Version("2.0"):
13+
if IS_PYDANTIC2:
1414
T = TypeVar("T")
1515

1616
def parse_obj_as(annot: T, obj: Any) -> T:
@@ -32,6 +32,8 @@ def model_copy(
3232
) -> Model:
3333
return instance.model_copy(update=update, deep=deep)
3434

35+
validate_call = pydantic.validate_call
36+
3537
else:
3638
parse_obj_as = pydantic.parse_obj_as # type: ignore
3739

@@ -50,3 +52,5 @@ def model_copy(
5052
deep: bool = False,
5153
) -> Model:
5254
return instance.copy(update=update, deep=deep)
55+
56+
validate_call = pydantic.validate_arguments # type: ignore

taskiq/serialization.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing_extensions import Protocol, TypeVar, runtime_checkable
1919

2020
import taskiq.exceptions # noqa: WPS301
21+
from taskiq.compat import IS_PYDANTIC2, validate_call
2122

2223
DecodedType = TypeVar("DecodedType")
2324
EncodedType = TypeVar("EncodedType")
@@ -245,18 +246,34 @@ def get_pickled_exception(exc: BaseException) -> BaseException:
245246
return exc
246247

247248

248-
class ExceptionRepr(pydantic.BaseModel):
249-
"""Serialiable exception representation."""
249+
if IS_PYDANTIC2:
250250

251-
exc_type: str
252-
exc_message: Tuple[Any, ...]
253-
exc_module: Optional[str]
254-
exc_cause: Optional[Union[BaseException, "ExceptionRepr"]] = None
255-
exc_context: Optional[Union[BaseException, "ExceptionRepr"]] = None
256-
exc_suppress_context: bool = False
251+
class ExceptionRepr(pydantic.BaseModel):
252+
"""Serializable exception model for pydantic v2."""
257253

258-
class Config:
259-
arbitrary_types_allowed = True
254+
exc_type: str
255+
exc_message: Tuple[Any, ...]
256+
exc_module: Optional[str]
257+
exc_cause: Optional[Union[BaseException, "ExceptionRepr"]] = None
258+
exc_context: Optional[Union[BaseException, "ExceptionRepr"]] = None
259+
exc_suppress_context: bool = False
260+
261+
model_config = pydantic.ConfigDict(arbitrary_types_allowed=True)
262+
263+
else:
264+
265+
class ExceptionRepr(pydantic.BaseModel): # type: ignore
266+
"""Serializable exception model for pydantic v1."""
267+
268+
exc_type: str
269+
exc_message: Tuple[Any, ...]
270+
exc_module: Optional[str]
271+
exc_cause: Optional[Union[BaseException, "ExceptionRepr"]] = None
272+
exc_context: Optional[Union[BaseException, "ExceptionRepr"]] = None
273+
exc_suppress_context: bool = False
274+
275+
class Config:
276+
arbitrary_types_allowed = True
260277

261278

262279
def _prepare_exception(
@@ -297,7 +314,7 @@ def _prepare_exception(
297314
SEEN_EXCEPTIONS_CACHE.discard(id(exc))
298315

299316

300-
@pydantic.validate_arguments(config={"arbitrary_types_allowed": True}) # type: ignore
317+
@validate_call(config=pydantic.ConfigDict(arbitrary_types_allowed=True))
301318
def prepare_exception(
302319
exc: BaseException,
303320
coder: Coder[Any, Any],
@@ -312,7 +329,7 @@ def prepare_exception(
312329
return _prepare_exception(exc, coder) # type: ignore
313330

314331

315-
@pydantic.validate_arguments(config={"arbitrary_types_allowed": True}) # type: ignore
332+
@validate_call(config=pydantic.ConfigDict(arbitrary_types_allowed=True))
316333
def exception_to_python( # noqa: C901, WPS210
317334
exc: Optional[Union[BaseException, ExceptionRepr]],
318335
) -> Optional[BaseException]:

0 commit comments

Comments
 (0)