tempor.core.pydantic_utils module

Module for pydantic-related utilities.

tempor.core.pydantic_utils.is_pydantic_dataclass(cls: type) bool[source]

A helper function to check if a class is a pydantic dataclass.

Parameters:
cls : Type

Class.

Returns:

Whether the class is a pydantic dataclass.

Return type:

bool

tempor.core.pydantic_utils.make_pydantic_dataclass(builtin_dataclass: type) type[source]

Workaround for a pydantic edge case issue when calling pydantic.dataclass(<builtin_dataclass>) more than once where builtin_dataclass has a default factory filed after a keyword parameter.

E.g. the following would normally fail, this works around the issue.

from typing import List
import dataclasses
import pydantic


@dataclasses.dataclass
class MyDataclass:
    a: str = "string"
    b: List[int] = dataclasses.field(default_factory=lambda: [1, 2, 3])


pydantic.dataclasses.dataclass(MyDataclass)  # OK.
pydantic.dataclasses.dataclass(MyDataclass)  # TypeError.
Parameters:
builtin_dataclass : Type

Python builtin dataclass.

Returns:

builtin_dataclass safely converted to pydantic dataclass.

Return type:

Type

tempor.core.pydantic_utils.validate_arguments(*args: Any, **kwargs: Any) Callable[[Callable[[P], T]], Callable[[P], T]][source]

Uses the Callable[P, T] approach to type the pydantic validate_arguments decorator. Helps mypy to correctly understand typing of functions that are decorated by this.

See: - https://stackoverflow.com/a/74080156 - https://docs.python.org/3/library/typing.html#typing.ParamSpec

Parameters:
*args : Any

Positional arguments to pass to pydantic.validate_arguments.

**kwargs : Any

Keyword arguments to pass to pydantic.validate_arguments.

Returns:

The updated pydantic.validate_arguments decorator.

Return type:

Callable[[Callable[P, T]], Callable[P, T]]