Skip to content

Commit 4f04887

Browse files
authored
Merge pull request #148 from python-ellar/pydanticV2_support
Pydantic v2 migration
2 parents abb19f4 + d46821d commit 4f04887

File tree

111 files changed

+3903
-1772
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+3903
-1772
lines changed

.github/workflows/test_full.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ jobs:
3737
run: pip install flit
3838
- name: Install Dependencies
3939
run: flit install --symlink
40-
- name: Black
41-
run: black --check ellar tests examples
4240
- name: Ruff Linting Check
4341
run: ruff check ellar tests examples
4442
- name: mypy

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ install-full: ## Install dependencies
1919
pre-commit install -f
2020

2121
lint:fmt ## Run code linters
22-
black --check ellar tests examples
2322
ruff check ellar tests examples
2423
mypy ellar
2524

2625
fmt format:clean ## Run code formatters
27-
black ellar tests examples
26+
ruff format ellar tests examples
2827
ruff check --fix ellar tests examples
2928

3029
test:clean ## Run tests

ellar/app/context.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from types import TracebackType
55

66
from ellar.common.constants import ELLAR_CONFIG_MODULE
7+
from ellar.common.logger import logger
78
from ellar.common.utils.functional import SimpleLazyObject, empty
89
from ellar.core import Config
910
from ellar.di import EllarInjector
@@ -95,10 +96,10 @@ def _get_application_config() -> Config:
9596
if app_context is empty:
9697
config_module = os.environ.get(ELLAR_CONFIG_MODULE)
9798
if not config_module:
98-
raise RuntimeError(
99+
logger.warning(
99100
"You are trying to access app config outside app context "
100101
"and %s is not specified. This may cause differences in config "
101-
"values when the app." % (ELLAR_CONFIG_MODULE,)
102+
"values with the app" % (ELLAR_CONFIG_MODULE,)
102103
)
103104
return Config(config_module=config_module)
104105

ellar/auth/handlers/schemes/base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ def openapi_security_scheme(
103103
) -> t.Dict:
104104
assert cls.openapi_in, "openapi_in is required"
105105
return {
106-
cls.openapi_name
107-
or cls.__name__: {
106+
cls.openapi_name or cls.__name__: {
108107
"type": "apiKey",
109108
"description": cls.openapi_description,
110109
"in": cls.openapi_in,
@@ -153,8 +152,7 @@ def openapi_security_scheme(
153152
) -> t.Dict:
154153
assert cls.scheme, "openapi_scheme is required"
155154
return {
156-
cls.openapi_name
157-
or cls.__name__: {
155+
cls.openapi_name or cls.__name__: {
158156
"type": "http",
159157
"description": cls.openapi_description,
160158
"scheme": cls.scheme,

ellar/auth/session/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import typing as t
33

4-
from pydantic import BaseModel
4+
from ellar.pydantic import BaseModel
55

66
if sys.version_info >= (3, 8): # pragma: no cover
77
from typing import Literal

ellar/cache/schema.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import typing as t
22

33
from ellar.cache import BaseCacheBackend
4-
from pydantic import BaseModel, validator
4+
from ellar.pydantic import BaseModel, as_pydantic_validator, field_validator
55

66

7+
@as_pydantic_validator("__validate_input__", schema={"type": "object"})
78
class TBaseCacheBackend:
89
@classmethod
9-
def __get_validators__(
10-
cls: t.Type["TBaseCacheBackend"],
11-
) -> t.Iterable[t.Callable[..., t.Any]]:
12-
yield cls.validate
10+
def __validate_input__(
11+
cls: t.Type["TBaseCacheBackend"], __input: t.Any, _: t.Any
12+
) -> t.Any:
13+
if isinstance(__input, BaseCacheBackend):
14+
return __input
1315

14-
@classmethod
15-
def validate(cls: t.Type["TBaseCacheBackend"], v: t.Any) -> t.Any:
16-
if isinstance(v, BaseCacheBackend):
17-
return v
18-
19-
raise ValueError(f"Expected BaseCacheBackend, received: {type(v)}")
16+
raise ValueError(f"Expected BaseCacheBackend, received: {type(__input)}")
2017

2118

2219
class CacheModuleSchemaSetup(BaseModel):
2320
CACHES: t.Dict[str, TBaseCacheBackend] = {}
2421

25-
@validator("CACHES", pre=True)
22+
@field_validator("CACHES", mode="before")
2623
def pre_cache_validate(cls, value: t.Dict) -> t.Any:
2724
if value and not value.get("default"):
2825
raise ValueError("CACHES configuration must have a 'default' key")

ellar/common/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
trace,
9797
ws_route,
9898
)
99-
from .serializer import DataclassSerializer, Serializer, serialize_object
99+
from .serializer import Serializer, serialize_object
100100
from .templating import TemplateResponse, render_template, render_template_string
101101

102102
__all__ = [
@@ -108,7 +108,6 @@
108108
"EllarTyper",
109109
"GlobalGuard",
110110
"Serializer",
111-
"DataclassSerializer",
112111
"WebSocketException",
113112
"APIException",
114113
"AuthenticationFailed",

ellar/common/compatible/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from .cache_properties import cached_property
22
from .dict import AttributeDict, AttributeDictAccessMixin
3-
from .emails import EmailStr
43

54
__all__ = [
65
"cached_property",
7-
"EmailStr",
86
"AttributeDictAccessMixin",
97
"AttributeDict",
108
]

ellar/common/constants.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55

66
from ellar.common.types import TMessage
77
from ellar.di import AnnotationToValue
8-
from pydantic.fields import (
9-
SHAPE_LIST,
10-
SHAPE_SEQUENCE,
11-
SHAPE_SET,
12-
SHAPE_TUPLE,
13-
SHAPE_TUPLE_ELLIPSIS,
14-
)
158

169
POST = "POST"
1710
PUT = "PUT"
@@ -81,21 +74,8 @@ class CONTROLLER_METADATA(metaclass=AnnotationToValue):
8174
INCLUDE_IN_SCHEMA: str
8275

8376

84-
sequence_shapes = {
85-
SHAPE_LIST,
86-
SHAPE_SET,
87-
SHAPE_TUPLE,
88-
SHAPE_SEQUENCE,
89-
SHAPE_TUPLE_ELLIPSIS,
90-
}
9177
sequence_types = (list, set, tuple)
92-
sequence_shape_to_type = {
93-
SHAPE_LIST: list,
94-
SHAPE_SET: set,
95-
SHAPE_TUPLE: tuple,
96-
SHAPE_SEQUENCE: list,
97-
SHAPE_TUPLE_ELLIPSIS: list,
98-
}
78+
9979
primitive_types = (int, float, bool, str)
10080
METHODS_WITH_BODY = {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"}
10181
STATUS_CODES_WITH_NO_BODY = {100, 101, 102, 103, 204, 304}

ellar/common/converters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import typing as t
22
from abc import ABC, abstractmethod
33

4-
from pydantic.typing import get_args, get_origin
4+
from typing_extensions import get_args, get_origin
55

66
_origin_maps = {
77
list: t.List,

0 commit comments

Comments
 (0)