cadwyn 4.4.3__py3-none-any.whl → 4.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cadwyn might be problematic. Click here for more details.
- cadwyn/__init__.py +9 -9
- cadwyn/_asts.py +10 -8
- cadwyn/_importer.py +1 -1
- cadwyn/_utils.py +15 -1
- cadwyn/changelogs.py +1 -1
- cadwyn/route_generation.py +14 -7
- cadwyn/routing.py +5 -10
- cadwyn/schema_generation.py +29 -23
- cadwyn/structure/__init__.py +7 -7
- cadwyn/structure/data.py +25 -13
- cadwyn/structure/endpoints.py +1 -1
- cadwyn/structure/schemas.py +1 -1
- cadwyn/structure/versions.py +2 -14
- {cadwyn-4.4.3.dist-info → cadwyn-4.5.0.dist-info}/METADATA +2 -2
- cadwyn-4.5.0.dist-info/RECORD +28 -0
- {cadwyn-4.4.3.dist-info → cadwyn-4.5.0.dist-info}/WHEEL +1 -1
- cadwyn-4.4.3.dist-info/RECORD +0 -28
- {cadwyn-4.4.3.dist-info → cadwyn-4.5.0.dist-info}/entry_points.txt +0 -0
- {cadwyn-4.4.3.dist-info → cadwyn-4.5.0.dist-info}/licenses/LICENSE +0 -0
cadwyn/__init__.py
CHANGED
|
@@ -22,21 +22,21 @@ from .structure import (
|
|
|
22
22
|
__version__ = importlib.metadata.version("cadwyn")
|
|
23
23
|
__all__ = [
|
|
24
24
|
"Cadwyn",
|
|
25
|
-
"VersionedAPIRouter",
|
|
26
|
-
"VersionBundle",
|
|
27
25
|
"HeadVersion",
|
|
26
|
+
"RequestInfo",
|
|
27
|
+
"ResponseInfo",
|
|
28
28
|
"Version",
|
|
29
|
-
"
|
|
30
|
-
"generate_versioned_routers",
|
|
29
|
+
"VersionBundle",
|
|
31
30
|
"VersionChange",
|
|
32
31
|
"VersionChangeWithSideEffects",
|
|
32
|
+
"VersionedAPIRouter",
|
|
33
|
+
"convert_request_to_next_version_for",
|
|
34
|
+
"convert_response_to_previous_version_for",
|
|
33
35
|
"endpoint",
|
|
34
|
-
"schema",
|
|
35
36
|
"enum",
|
|
36
|
-
"convert_response_to_previous_version_for",
|
|
37
|
-
"convert_request_to_next_version_for",
|
|
38
|
-
"RequestInfo",
|
|
39
|
-
"ResponseInfo",
|
|
40
37
|
"generate_versioned_models",
|
|
38
|
+
"generate_versioned_routers",
|
|
41
39
|
"hidden",
|
|
40
|
+
"migrate_response_body",
|
|
41
|
+
"schema",
|
|
42
42
|
]
|
cadwyn/_asts.py
CHANGED
|
@@ -56,12 +56,13 @@ def get_fancy_repr(value: Any) -> Any:
|
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
def transform_grouped_metadata(value: "annotated_types.GroupedMetadata"):
|
|
59
|
-
modified_fields = []
|
|
60
59
|
empty_obj = type(value)
|
|
61
60
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
modified_fields = [
|
|
62
|
+
(key, getattr(value, key))
|
|
63
|
+
for key in value.__dataclass_fields__ # pyright: ignore[reportAttributeAccessIssue]
|
|
64
|
+
if getattr(value, key) != getattr(empty_obj, key)
|
|
65
|
+
]
|
|
65
66
|
|
|
66
67
|
return PlainRepr(
|
|
67
68
|
value.__class__.__name__
|
|
@@ -120,11 +121,12 @@ def transform_other(value: Any) -> Any:
|
|
|
120
121
|
|
|
121
122
|
|
|
122
123
|
def _get_lambda_source_from_default_factory(source: str) -> str:
|
|
123
|
-
found_lambdas: list[ast.Lambda] = [
|
|
124
|
+
found_lambdas: list[ast.Lambda] = [
|
|
125
|
+
node.value
|
|
126
|
+
for node in ast.walk(ast.parse(source))
|
|
127
|
+
if isinstance(node, ast.keyword) and node.arg == "default_factory" and isinstance(node.value, ast.Lambda)
|
|
128
|
+
]
|
|
124
129
|
|
|
125
|
-
for node in ast.walk(ast.parse(source)):
|
|
126
|
-
if isinstance(node, ast.keyword) and node.arg == "default_factory" and isinstance(node.value, ast.Lambda):
|
|
127
|
-
found_lambdas.append(node.value)
|
|
128
130
|
if len(found_lambdas) == 1:
|
|
129
131
|
return ast.unparse(found_lambdas[0])
|
|
130
132
|
# These two errors are really hard to cover. Not sure if even possible, honestly :)
|
cadwyn/_importer.py
CHANGED
|
@@ -7,7 +7,7 @@ from cadwyn.exceptions import ImportFromStringError
|
|
|
7
7
|
def import_attribute_from_string(import_str: str) -> Any:
|
|
8
8
|
module_str, _, attrs_str = import_str.partition(":")
|
|
9
9
|
if not module_str or not attrs_str:
|
|
10
|
-
message = 'Import string "{import_str}" must be in format "<module>:<attribute>".'
|
|
10
|
+
message = f'Import string "{import_str}" must be in format "<module>:<attribute>".'
|
|
11
11
|
raise ImportFromStringError(message.format(import_str=import_str))
|
|
12
12
|
|
|
13
13
|
module = import_module_from_string(module_str)
|
cadwyn/_utils.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from collections.abc import Callable
|
|
2
|
-
from typing import Any, Generic, TypeVar, Union
|
|
2
|
+
from typing import TYPE_CHECKING, Any, Generic, TypeVar, Union
|
|
3
3
|
|
|
4
4
|
from pydantic._internal._decorators import unwrap_wrapped_function
|
|
5
5
|
|
|
@@ -40,3 +40,17 @@ def fully_unwrap_decorator(func: Callable, is_pydantic_v1_style_validator: Any):
|
|
|
40
40
|
if is_pydantic_v1_style_validator and func.__closure__:
|
|
41
41
|
func = func.__closure__[0].cell_contents
|
|
42
42
|
return unwrap_wrapped_function(func)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
T = TypeVar("T", bound=type[object])
|
|
46
|
+
|
|
47
|
+
if TYPE_CHECKING:
|
|
48
|
+
lenient_issubclass = issubclass
|
|
49
|
+
|
|
50
|
+
else:
|
|
51
|
+
|
|
52
|
+
def lenient_issubclass(cls: type, other: T | tuple[T, ...]) -> bool:
|
|
53
|
+
try:
|
|
54
|
+
return issubclass(cls, other)
|
|
55
|
+
except TypeError: # pragma: no cover
|
|
56
|
+
return False
|
cadwyn/changelogs.py
CHANGED
|
@@ -160,7 +160,7 @@ def _get_openapi_representation_of_a_field(model: type[BaseModel], field_name: s
|
|
|
160
160
|
|
|
161
161
|
model_name_map = get_compat_model_name_map([CadwynDummyModelForRepresentation.model_fields["my_field"]])
|
|
162
162
|
schema_generator = GenerateJsonSchema(ref_template=REF_TEMPLATE)
|
|
163
|
-
|
|
163
|
+
_, definitions = get_definitions(
|
|
164
164
|
fields=[ModelField(CadwynDummyModelForRepresentation.model_fields["my_field"], "my_field")],
|
|
165
165
|
schema_generator=schema_generator,
|
|
166
166
|
model_name_map=model_name_map,
|
cadwyn/route_generation.py
CHANGED
|
@@ -179,13 +179,13 @@ class _EndpointTransformer(Generic[_R, _WR]):
|
|
|
179
179
|
copy_of_dependant,
|
|
180
180
|
self.versions,
|
|
181
181
|
)
|
|
182
|
-
for
|
|
182
|
+
for router in routers.values():
|
|
183
183
|
router.routes = [
|
|
184
184
|
route
|
|
185
185
|
for route in router.routes
|
|
186
186
|
if not (isinstance(route, fastapi.routing.APIRoute) and _DELETED_ROUTE_TAG in route.tags)
|
|
187
187
|
]
|
|
188
|
-
for
|
|
188
|
+
for webhook_router in webhook_routers.values():
|
|
189
189
|
webhook_router.routes = [
|
|
190
190
|
route
|
|
191
191
|
for route in webhook_router.routes
|
|
@@ -221,6 +221,8 @@ class _EndpointTransformer(Generic[_R, _WR]):
|
|
|
221
221
|
|
|
222
222
|
for by_schema_converters in version_change.alter_request_by_schema_instructions.values():
|
|
223
223
|
for by_schema_converter in by_schema_converters:
|
|
224
|
+
if not by_schema_converter.check_usage: # pragma: no cover
|
|
225
|
+
continue
|
|
224
226
|
missing_models = set(by_schema_converter.schemas) - head_request_bodies
|
|
225
227
|
if missing_models:
|
|
226
228
|
raise RouteRequestBySchemaConverterDoesNotApplyToAnythingError(
|
|
@@ -232,6 +234,8 @@ class _EndpointTransformer(Generic[_R, _WR]):
|
|
|
232
234
|
)
|
|
233
235
|
for by_schema_converters in version_change.alter_response_by_schema_instructions.values():
|
|
234
236
|
for by_schema_converter in by_schema_converters:
|
|
237
|
+
if not by_schema_converter.check_usage: # pragma: no cover
|
|
238
|
+
continue
|
|
235
239
|
missing_models = set(by_schema_converter.schemas) - head_response_models
|
|
236
240
|
if missing_models:
|
|
237
241
|
raise RouteResponseBySchemaConverterDoesNotApplyToAnythingError(
|
|
@@ -240,6 +244,9 @@ class _EndpointTransformer(Generic[_R, _WR]):
|
|
|
240
244
|
f"failed to find routes with the following response models: "
|
|
241
245
|
f"{[m.__name__ for m in missing_models]}. "
|
|
242
246
|
f"This means that you are trying to apply this converter to non-existing endpoint(s). "
|
|
247
|
+
"If this is intentional and this converter really does not apply to any endpoints, then "
|
|
248
|
+
"pass check_usage=False argument to "
|
|
249
|
+
f"{version_change.__name__}.{by_schema_converter.transformer.__name__}"
|
|
243
250
|
)
|
|
244
251
|
|
|
245
252
|
def _extract_all_routes_identifiers(
|
|
@@ -466,18 +473,18 @@ def _get_routes(
|
|
|
466
473
|
*,
|
|
467
474
|
is_deleted: bool = False,
|
|
468
475
|
) -> list[fastapi.routing.APIRoute]:
|
|
469
|
-
found_routes = []
|
|
470
476
|
endpoint_path = endpoint_path.rstrip("/")
|
|
471
|
-
|
|
477
|
+
return [
|
|
478
|
+
route
|
|
479
|
+
for route in routes
|
|
472
480
|
if (
|
|
473
481
|
isinstance(route, fastapi.routing.APIRoute)
|
|
474
482
|
and route.path.rstrip("/") == endpoint_path
|
|
475
483
|
and set(route.methods).issubset(endpoint_methods)
|
|
476
484
|
and (endpoint_func_name is None or route.endpoint.__name__ == endpoint_func_name)
|
|
477
485
|
and (_DELETED_ROUTE_TAG in route.tags) == is_deleted
|
|
478
|
-
)
|
|
479
|
-
|
|
480
|
-
return found_routes
|
|
486
|
+
)
|
|
487
|
+
]
|
|
481
488
|
|
|
482
489
|
|
|
483
490
|
def _get_route_from_func(
|
cadwyn/routing.py
CHANGED
|
@@ -23,9 +23,9 @@ _logger = getLogger(__name__)
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
class _RootHeaderAPIRouter(APIRouter):
|
|
26
|
-
"""
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
"""Root router of the FastAPI app when using header based versioning.
|
|
27
|
+
|
|
28
|
+
It will be used to route the requests to the correct versioned route
|
|
29
29
|
based on the headers. It also supports waterflowing the requests to the latest
|
|
30
30
|
version of the API if the request header doesn't match any of the versions.
|
|
31
31
|
|
|
@@ -88,9 +88,6 @@ class _RootHeaderAPIRouter(APIRouter):
|
|
|
88
88
|
return self.versioned_routers[version_chosen].routes
|
|
89
89
|
|
|
90
90
|
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
91
|
-
"""
|
|
92
|
-
The main entry point to the Router class.
|
|
93
|
-
"""
|
|
94
91
|
if "router" not in scope: # pragma: no cover
|
|
95
92
|
scope["router"] = self
|
|
96
93
|
|
|
@@ -131,10 +128,8 @@ class _RootHeaderAPIRouter(APIRouter):
|
|
|
131
128
|
self.unversioned_routes.append(self.routes[-1])
|
|
132
129
|
|
|
133
130
|
async def process_request(self, scope: Scope, receive: Receive, send: Send, routes: Sequence[BaseRoute]) -> None:
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
but in this version self.routes were replaced with routes from the function arguments
|
|
137
|
-
"""
|
|
131
|
+
# It's a copy-paste from starlette.routing.Router
|
|
132
|
+
# but in this version self.routes were replaced with routes from the function arguments
|
|
138
133
|
|
|
139
134
|
partial = None
|
|
140
135
|
partial_scope = {}
|
cadwyn/schema_generation.py
CHANGED
|
@@ -30,7 +30,6 @@ import pydantic
|
|
|
30
30
|
import pydantic._internal._decorators
|
|
31
31
|
from fastapi import Response
|
|
32
32
|
from fastapi.routing import APIRoute
|
|
33
|
-
from issubclass import issubclass
|
|
34
33
|
from pydantic import BaseModel, Field, RootModel
|
|
35
34
|
from pydantic._internal import _decorators
|
|
36
35
|
from pydantic._internal._decorators import (
|
|
@@ -44,7 +43,7 @@ from pydantic._internal._decorators import (
|
|
|
44
43
|
from pydantic.fields import ComputedFieldInfo, FieldInfo
|
|
45
44
|
from typing_extensions import Doc, Self, _AnnotatedAlias, assert_never
|
|
46
45
|
|
|
47
|
-
from cadwyn._utils import Sentinel, UnionType, fully_unwrap_decorator
|
|
46
|
+
from cadwyn._utils import Sentinel, UnionType, fully_unwrap_decorator, lenient_issubclass
|
|
48
47
|
from cadwyn.exceptions import InvalidGenerationInstructionError
|
|
49
48
|
from cadwyn.structure.common import VersionDate
|
|
50
49
|
from cadwyn.structure.data import ResponseInfo
|
|
@@ -159,9 +158,11 @@ def migrate_response_body(
|
|
|
159
158
|
*,
|
|
160
159
|
latest_body: Any,
|
|
161
160
|
version: VersionDate | str,
|
|
162
|
-
):
|
|
163
|
-
"""Convert the data to a specific version
|
|
164
|
-
|
|
161
|
+
) -> Any:
|
|
162
|
+
"""Convert the data to a specific version
|
|
163
|
+
|
|
164
|
+
Apply all version changes from latest until the passed version in reverse order
|
|
165
|
+
and wrap the result in the correct version of latest_response_model
|
|
165
166
|
"""
|
|
166
167
|
if isinstance(version, str):
|
|
167
168
|
version = date.fromisoformat(version)
|
|
@@ -213,6 +214,10 @@ def _wrap_validator(func: Callable, is_pydantic_v1_style_validator: Any, decorat
|
|
|
213
214
|
# There's an inconsistency in their interfaces so we gotta resort to this
|
|
214
215
|
mode = kwargs.pop("mode", "after")
|
|
215
216
|
kwargs["pre"] = mode != "after"
|
|
217
|
+
if (
|
|
218
|
+
isinstance(decorator_info, RootValidatorDecoratorInfo) and decorator_info.mode == "after"
|
|
219
|
+
): # pragma: no cover # TODO
|
|
220
|
+
kwargs["skip_on_failure"] = True
|
|
216
221
|
if decorator_fields is not None:
|
|
217
222
|
return _PerFieldValidatorWrapper(
|
|
218
223
|
func=func, fields=list(decorator_fields), decorator=actual_decorator, kwargs=kwargs
|
|
@@ -271,7 +276,8 @@ class _PydanticModelWrapper(Generic[_T_PYDANTIC_MODEL]):
|
|
|
271
276
|
fields: Annotated[
|
|
272
277
|
dict["_FieldName", PydanticFieldWrapper],
|
|
273
278
|
Doc(
|
|
274
|
-
"Fields that belong to this model, not to its parents.
|
|
279
|
+
"Fields that belong to this model, not to its parents. "
|
|
280
|
+
"I.e. The ones that were either defined or overridden "
|
|
275
281
|
),
|
|
276
282
|
] = dataclasses.field(repr=False)
|
|
277
283
|
validators: dict[str, _PerFieldValidatorWrapper | _ValidatorWrapper] = dataclasses.field(repr=False)
|
|
@@ -316,7 +322,7 @@ class _PydanticModelWrapper(Generic[_T_PYDANTIC_MODEL]):
|
|
|
316
322
|
for base in self.cls.mro()[1:]:
|
|
317
323
|
if base in schemas:
|
|
318
324
|
parents.append(schemas[base])
|
|
319
|
-
elif
|
|
325
|
+
elif lenient_issubclass(base, BaseModel):
|
|
320
326
|
parents.append(_wrap_pydantic_model(base))
|
|
321
327
|
self._parents = parents
|
|
322
328
|
return parents
|
|
@@ -372,10 +378,9 @@ def is_regular_function(call: Callable):
|
|
|
372
378
|
|
|
373
379
|
|
|
374
380
|
class _CallableWrapper:
|
|
375
|
-
|
|
376
|
-
They are based on putting dependencies (functions) as keys for the dictionary so if we want to be able to
|
|
377
|
-
override the wrapper, we need to make sure that it is equivalent to the original in __hash__ and __eq__
|
|
378
|
-
"""
|
|
381
|
+
# __eq__ and __hash__ are needed to make sure that dependency overrides work correctly.
|
|
382
|
+
# They are based on putting dependencies (functions) as keys for the dictionary so if we want to be able to
|
|
383
|
+
# override the wrapper, we need to make sure that it is equivalent to the original in __hash__ and __eq__
|
|
379
384
|
|
|
380
385
|
def __init__(self, original_callable: Callable) -> None:
|
|
381
386
|
super().__init__()
|
|
@@ -386,11 +391,9 @@ class _CallableWrapper:
|
|
|
386
391
|
|
|
387
392
|
@property
|
|
388
393
|
def __globals__(self):
|
|
389
|
-
|
|
390
|
-
It's supposed to be an attribute on the function but we use it as property to prevent python
|
|
391
|
-
from trying to pickle globals when we deepcopy this wrapper
|
|
392
|
-
"""
|
|
393
|
-
#
|
|
394
|
+
# FastAPI uses __globals__ to resolve forward references in type hints
|
|
395
|
+
# It's supposed to be an attribute on the function but we use it as property to prevent python
|
|
396
|
+
# from trying to pickle globals when we deepcopy this wrapper
|
|
394
397
|
return self._original_callable.__globals__
|
|
395
398
|
|
|
396
399
|
def __call__(self, *args: Any, **kwargs: Any):
|
|
@@ -420,8 +423,7 @@ class _AnnotationTransformer:
|
|
|
420
423
|
)
|
|
421
424
|
|
|
422
425
|
def change_version_of_annotation(self, annotation: Any) -> Any:
|
|
423
|
-
"""Recursively go through all annotations and change them to the
|
|
424
|
-
annotations corresponding to the version passed.
|
|
426
|
+
"""Recursively go through all annotations and change them to annotations corresponding to the version passed.
|
|
425
427
|
|
|
426
428
|
So if we had a annotation "UserResponse" from "head" version, and we passed version of "2022-11-16", it would
|
|
427
429
|
replace "UserResponse" with the the same class but from the "2022-11-16" version.
|
|
@@ -503,7 +505,7 @@ class _AnnotationTransformer:
|
|
|
503
505
|
return annotation
|
|
504
506
|
|
|
505
507
|
def _change_version_of_type(self, annotation: type):
|
|
506
|
-
if
|
|
508
|
+
if lenient_issubclass(annotation, BaseModel | Enum):
|
|
507
509
|
return self.generator[annotation]
|
|
508
510
|
else:
|
|
509
511
|
return annotation
|
|
@@ -607,7 +609,7 @@ def _add_request_and_response_params(route: APIRoute):
|
|
|
607
609
|
|
|
608
610
|
@final
|
|
609
611
|
class SchemaGenerator:
|
|
610
|
-
__slots__ = "annotation_transformer", "
|
|
612
|
+
__slots__ = "annotation_transformer", "concrete_models", "model_bundle"
|
|
611
613
|
|
|
612
614
|
def __init__(self, model_bundle: _ModelBundle) -> None:
|
|
613
615
|
self.annotation_transformer = _AnnotationTransformer(self)
|
|
@@ -619,7 +621,11 @@ class SchemaGenerator:
|
|
|
619
621
|
}
|
|
620
622
|
|
|
621
623
|
def __getitem__(self, model: type[_T_ANY_MODEL], /) -> type[_T_ANY_MODEL]:
|
|
622
|
-
if
|
|
624
|
+
if (
|
|
625
|
+
not isinstance(model, type)
|
|
626
|
+
or not lenient_issubclass(model, BaseModel | Enum)
|
|
627
|
+
or model in (BaseModel, RootModel)
|
|
628
|
+
):
|
|
623
629
|
return model
|
|
624
630
|
model = _unwrap_model(model)
|
|
625
631
|
|
|
@@ -648,10 +654,10 @@ class SchemaGenerator:
|
|
|
648
654
|
elif model in self.model_bundle.enums:
|
|
649
655
|
return self.model_bundle.enums[model]
|
|
650
656
|
|
|
651
|
-
if
|
|
657
|
+
if lenient_issubclass(model, BaseModel):
|
|
652
658
|
wrapper = _wrap_pydantic_model(model)
|
|
653
659
|
self.model_bundle.schemas[model] = wrapper
|
|
654
|
-
elif
|
|
660
|
+
elif lenient_issubclass(model, Enum):
|
|
655
661
|
wrapper = _EnumWrapper(model)
|
|
656
662
|
self.model_bundle.enums[model] = wrapper
|
|
657
663
|
else:
|
cadwyn/structure/__init__.py
CHANGED
|
@@ -16,16 +16,16 @@ from .versions import (
|
|
|
16
16
|
)
|
|
17
17
|
|
|
18
18
|
__all__ = [
|
|
19
|
-
"VersionBundle",
|
|
20
|
-
"Version",
|
|
21
19
|
"HeadVersion",
|
|
20
|
+
"RequestInfo",
|
|
21
|
+
"ResponseInfo",
|
|
22
|
+
"Version",
|
|
23
|
+
"VersionBundle",
|
|
22
24
|
"VersionChange",
|
|
23
25
|
"VersionChangeWithSideEffects",
|
|
26
|
+
"convert_request_to_next_version_for",
|
|
27
|
+
"convert_response_to_previous_version_for",
|
|
24
28
|
"endpoint",
|
|
25
|
-
"schema",
|
|
26
29
|
"enum",
|
|
27
|
-
"
|
|
28
|
-
"convert_request_to_next_version_for",
|
|
29
|
-
"RequestInfo",
|
|
30
|
-
"ResponseInfo",
|
|
30
|
+
"schema",
|
|
31
31
|
]
|
cadwyn/structure/data.py
CHANGED
|
@@ -15,7 +15,7 @@ _P = ParamSpec("_P")
|
|
|
15
15
|
|
|
16
16
|
# TODO (https://github.com/zmievsa/cadwyn/issues/49): Add form handling
|
|
17
17
|
class RequestInfo:
|
|
18
|
-
__slots__ = ("
|
|
18
|
+
__slots__ = ("_cookies", "_query_params", "_request", "body", "headers")
|
|
19
19
|
|
|
20
20
|
def __init__(self, request: Request, body: Any):
|
|
21
21
|
super().__init__()
|
|
@@ -36,7 +36,7 @@ class RequestInfo:
|
|
|
36
36
|
|
|
37
37
|
# TODO (https://github.com/zmievsa/cadwyn/issues/111): handle _response.media_type and _response.background
|
|
38
38
|
class ResponseInfo:
|
|
39
|
-
__slots__ = ("
|
|
39
|
+
__slots__ = ("_response", "body")
|
|
40
40
|
|
|
41
41
|
def __init__(self, response: Response, body: Any):
|
|
42
42
|
super().__init__()
|
|
@@ -86,9 +86,15 @@ class _AlterDataInstruction:
|
|
|
86
86
|
return self.transformer(__request_or_response)
|
|
87
87
|
|
|
88
88
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
@dataclass
|
|
90
|
+
class _BaseAlterBySchemaInstruction:
|
|
91
|
+
schemas: tuple[Any, ...]
|
|
92
|
+
check_usage: bool = True
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
##########
|
|
96
|
+
# Requests
|
|
97
|
+
##########
|
|
92
98
|
|
|
93
99
|
|
|
94
100
|
@dataclass
|
|
@@ -97,8 +103,7 @@ class _BaseAlterRequestInstruction(_AlterDataInstruction):
|
|
|
97
103
|
|
|
98
104
|
|
|
99
105
|
@dataclass
|
|
100
|
-
class _AlterRequestBySchemaInstruction(_BaseAlterRequestInstruction):
|
|
101
|
-
schemas: tuple[Any, ...]
|
|
106
|
+
class _AlterRequestBySchemaInstruction(_BaseAlterBySchemaInstruction, _BaseAlterRequestInstruction): ...
|
|
102
107
|
|
|
103
108
|
|
|
104
109
|
@dataclass
|
|
@@ -110,7 +115,10 @@ class _AlterRequestByPathInstruction(_BaseAlterRequestInstruction):
|
|
|
110
115
|
|
|
111
116
|
@overload
|
|
112
117
|
def convert_request_to_next_version_for(
|
|
113
|
-
first_schema: type,
|
|
118
|
+
first_schema: type,
|
|
119
|
+
/,
|
|
120
|
+
*additional_schemas: type,
|
|
121
|
+
check_usage: bool = True,
|
|
114
122
|
) -> "type[staticmethod[_P, None]]": ...
|
|
115
123
|
|
|
116
124
|
|
|
@@ -123,6 +131,7 @@ def convert_request_to_next_version_for(
|
|
|
123
131
|
methods_or_second_schema: list[str] | None | type = None,
|
|
124
132
|
/,
|
|
125
133
|
*additional_schemas: type,
|
|
134
|
+
check_usage: bool = True,
|
|
126
135
|
) -> "type[staticmethod[_P, None]]":
|
|
127
136
|
_validate_decorator_args(schema_or_path, methods_or_second_schema, additional_schemas)
|
|
128
137
|
|
|
@@ -141,14 +150,15 @@ def convert_request_to_next_version_for(
|
|
|
141
150
|
return _AlterRequestBySchemaInstruction(
|
|
142
151
|
schemas=schemas,
|
|
143
152
|
transformer=transformer,
|
|
153
|
+
check_usage=check_usage,
|
|
144
154
|
)
|
|
145
155
|
|
|
146
156
|
return decorator # pyright: ignore[reportReturnType]
|
|
147
157
|
|
|
148
158
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
159
|
+
###########
|
|
160
|
+
# Responses
|
|
161
|
+
###########
|
|
152
162
|
|
|
153
163
|
|
|
154
164
|
@dataclass
|
|
@@ -158,8 +168,7 @@ class _BaseAlterResponseInstruction(_AlterDataInstruction):
|
|
|
158
168
|
|
|
159
169
|
|
|
160
170
|
@dataclass
|
|
161
|
-
class _AlterResponseBySchemaInstruction(_BaseAlterResponseInstruction):
|
|
162
|
-
schemas: tuple[Any, ...]
|
|
171
|
+
class _AlterResponseBySchemaInstruction(_BaseAlterBySchemaInstruction, _BaseAlterResponseInstruction): ...
|
|
163
172
|
|
|
164
173
|
|
|
165
174
|
@dataclass
|
|
@@ -175,6 +184,7 @@ def convert_response_to_previous_version_for(
|
|
|
175
184
|
/,
|
|
176
185
|
*schemas: type,
|
|
177
186
|
migrate_http_errors: bool = False,
|
|
187
|
+
check_usage: bool = True,
|
|
178
188
|
) -> "type[staticmethod[_P, None]]": ...
|
|
179
189
|
|
|
180
190
|
|
|
@@ -194,6 +204,7 @@ def convert_response_to_previous_version_for(
|
|
|
194
204
|
/,
|
|
195
205
|
*additional_schemas: type,
|
|
196
206
|
migrate_http_errors: bool = False,
|
|
207
|
+
check_usage: bool = True,
|
|
197
208
|
) -> "type[staticmethod[_P, None]]":
|
|
198
209
|
_validate_decorator_args(schema_or_path, methods_or_second_schema, additional_schemas)
|
|
199
210
|
|
|
@@ -215,6 +226,7 @@ def convert_response_to_previous_version_for(
|
|
|
215
226
|
schemas=schemas,
|
|
216
227
|
transformer=transformer,
|
|
217
228
|
migrate_http_errors=migrate_http_errors,
|
|
229
|
+
check_usage=check_usage,
|
|
218
230
|
)
|
|
219
231
|
|
|
220
232
|
return decorator # pyright: ignore[reportReturnType]
|
cadwyn/structure/endpoints.py
CHANGED
|
@@ -23,7 +23,7 @@ class EndpointAttributesPayload:
|
|
|
23
23
|
# 1. "endpoint" must not change -- otherwise this versioning is doomed
|
|
24
24
|
# 2. "dependency_overrides_provider" is taken from router's attributes
|
|
25
25
|
# 3. "response_model" must not change for the same reason as endpoint
|
|
26
|
-
# The following for the same reason as
|
|
26
|
+
# The following for the same reason as endpoint:
|
|
27
27
|
# * response_model_include: SetIntStr | DictIntStrAny
|
|
28
28
|
# * response_model_exclude: SetIntStr | DictIntStrAny
|
|
29
29
|
# * response_model_by_alias: bool
|
cadwyn/structure/schemas.py
CHANGED
cadwyn/structure/versions.py
CHANGED
|
@@ -396,17 +396,6 @@ class VersionBundle:
|
|
|
396
396
|
path: str,
|
|
397
397
|
method: str,
|
|
398
398
|
) -> ResponseInfo:
|
|
399
|
-
"""Convert the data to a specific version by applying all version changes in reverse order.
|
|
400
|
-
|
|
401
|
-
Args:
|
|
402
|
-
endpoint: the function which usually returns this data. Data migrations marked with this endpoint will
|
|
403
|
-
be applied to the passed data
|
|
404
|
-
payload: data to be migrated. Will be mutated during the call
|
|
405
|
-
version: the version to which the data should be converted
|
|
406
|
-
|
|
407
|
-
Returns:
|
|
408
|
-
Modified data
|
|
409
|
-
"""
|
|
410
399
|
for v in self.versions:
|
|
411
400
|
if v.value <= current_version:
|
|
412
401
|
break
|
|
@@ -421,7 +410,7 @@ class VersionBundle:
|
|
|
421
410
|
if path in version_change.alter_response_by_path_instructions:
|
|
422
411
|
for instruction in version_change.alter_response_by_path_instructions[path]:
|
|
423
412
|
if method in instruction.methods: # pragma: no branch # Safe branch to skip
|
|
424
|
-
migrations_to_apply.append(instruction)
|
|
413
|
+
migrations_to_apply.append(instruction) # noqa: PERF401
|
|
425
414
|
|
|
426
415
|
for migration in migrations_to_apply:
|
|
427
416
|
if response_info.status_code < 300 or migration.migrate_http_errors:
|
|
@@ -447,8 +436,7 @@ class VersionBundle:
|
|
|
447
436
|
request_param: FastapiRequest = kwargs[request_param_name]
|
|
448
437
|
response_param: FastapiResponse = kwargs[response_param_name]
|
|
449
438
|
background_tasks: BackgroundTasks | None = kwargs.get(
|
|
450
|
-
background_tasks_param_name, # pyright: ignore[reportArgumentType
|
|
451
|
-
None,
|
|
439
|
+
background_tasks_param_name, # pyright: ignore[reportArgumentType]
|
|
452
440
|
)
|
|
453
441
|
method = request_param.method
|
|
454
442
|
response = Sentinel
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: cadwyn
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.5.0
|
|
4
4
|
Summary: Production-ready community-driven modern Stripe-like API versioning in FastAPI
|
|
5
5
|
Project-URL: Source code, https://github.com/zmievsa/cadwyn
|
|
6
6
|
Project-URL: Documentation, https://docs.cadwyn.dev
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
cadwyn/__init__.py,sha256=gWsp-oNP5LhBqWF_rsrv6sEYP9KchTu8xhxAR8uaPm0,1035
|
|
2
|
+
cadwyn/__main__.py,sha256=fGoKJPNVueqqXW4rqwmRUBBMoDGeLEyATdT-rel5Pvs,2449
|
|
3
|
+
cadwyn/_asts.py,sha256=kd6Ngwr8c-uTNx-R-pP48Scf0OdjrTyEeSYe5DLoGqo,5095
|
|
4
|
+
cadwyn/_importer.py,sha256=QV6HqODCG9K2oL4Vc15fAqL2-plMvUWw_cgaj4Ln4C8,1075
|
|
5
|
+
cadwyn/_render.py,sha256=LJ-R1TrBgMJpTkJb6pQdRWaMjKyw3R6eTlXXEieqUw0,5466
|
|
6
|
+
cadwyn/_utils.py,sha256=rlD1SkswtZ1bWgKj6PLYbVaHYkD-NzY4iUcHdPd2Y68,1475
|
|
7
|
+
cadwyn/applications.py,sha256=worXUWoexSev0cGJYzXMFBxUgibC1AsM35SYtcV996k,16948
|
|
8
|
+
cadwyn/changelogs.py,sha256=uveMizeeqNv0JuXza9Rkg0ulDEWyJL24bRxcHkHlwjI,20054
|
|
9
|
+
cadwyn/exceptions.py,sha256=VlJKRmEGfFTDtHbOWc8kXK4yMi2N172K684Y2UIV8rI,1832
|
|
10
|
+
cadwyn/middleware.py,sha256=kUZK2dmoricMbv6knPCIHpXEInX2670XIwAj0v_XQxk,3408
|
|
11
|
+
cadwyn/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
+
cadwyn/route_generation.py,sha256=tdIsdDIBY4hVUlTGMh4pTMM92LzxhV9CmGs6J8iAhaw,25152
|
|
13
|
+
cadwyn/routing.py,sha256=mZRe7ivfTY2qdgrCBO4AHvKQq6Dazf7g_8B6aCrTgN8,7221
|
|
14
|
+
cadwyn/schema_generation.py,sha256=OUOcBfIDFviSqIYy0a2EHVWWSh1n2GKbvtr5xuolFgQ,40750
|
|
15
|
+
cadwyn/static/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
16
|
+
cadwyn/static/docs.html,sha256=WNm5ANJVy51TcIUFOaqKf1Z8eF86CC85TTHPxACtkzw,3455
|
|
17
|
+
cadwyn/structure/__init__.py,sha256=Wgvjdq3vfl9Yhe-BkcFGAMi_Co11YOfTmJQqgF5Gzx4,655
|
|
18
|
+
cadwyn/structure/common.py,sha256=GUclfxKLRlFwPjT237fCtLIzdvjvC9gI3acuxBizwbg,414
|
|
19
|
+
cadwyn/structure/data.py,sha256=uViRW4uOOonXZj90hOlPNk02AIwp0fvDNoF8M5_CEes,7707
|
|
20
|
+
cadwyn/structure/endpoints.py,sha256=8lrc4xanCt7gat106yYRIQC0TNxzFkLF-urIml_d_X0,5934
|
|
21
|
+
cadwyn/structure/enums.py,sha256=bZL-iUOUFi9ZYlMZJw-tAix2yrgCp3gH3N2gwO44LUU,1043
|
|
22
|
+
cadwyn/structure/schemas.py,sha256=bck4XzCOICVuohh-ZpIv4l57hx3l-f0O23b7w8n2WvA,8215
|
|
23
|
+
cadwyn/structure/versions.py,sha256=L07vdC9d7_h4WKV8TArgZKfgfzB8-M4f62FXZm8o1TM,33232
|
|
24
|
+
cadwyn-4.5.0.dist-info/METADATA,sha256=7V9B9KEfVWvWZPDZBCNLgGYMRqvJ1w2-bX69O9z8xmY,4504
|
|
25
|
+
cadwyn-4.5.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
26
|
+
cadwyn-4.5.0.dist-info/entry_points.txt,sha256=mGX8wl-Xfhpr5M93SUmkykaqinUaYAvW9rtDSX54gx0,47
|
|
27
|
+
cadwyn-4.5.0.dist-info/licenses/LICENSE,sha256=KeCWewiDQYpmSnzF-p_0YpoWiyDcUPaCuG8OWQs4ig4,1072
|
|
28
|
+
cadwyn-4.5.0.dist-info/RECORD,,
|
cadwyn-4.4.3.dist-info/RECORD
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
cadwyn/__init__.py,sha256=w4Iu8rEu9zfptMKsBaWgAj0vJABKGJukKYQBNbuwt1Q,1035
|
|
2
|
-
cadwyn/__main__.py,sha256=fGoKJPNVueqqXW4rqwmRUBBMoDGeLEyATdT-rel5Pvs,2449
|
|
3
|
-
cadwyn/_asts.py,sha256=OVOPjjZNrPHpbicf4Gg5HmWToPOdMo8S56pKDDJATRY,5139
|
|
4
|
-
cadwyn/_importer.py,sha256=2mZrDHlfY2heZsMBW-9RBpvKsCk9I-Wa8pxZ6f2f8gY,1074
|
|
5
|
-
cadwyn/_render.py,sha256=LJ-R1TrBgMJpTkJb6pQdRWaMjKyw3R6eTlXXEieqUw0,5466
|
|
6
|
-
cadwyn/_utils.py,sha256=GK9w_qzyOI_o6UaGVfwLLYhnJFMzXistoYI9fq2E9dE,1159
|
|
7
|
-
cadwyn/applications.py,sha256=worXUWoexSev0cGJYzXMFBxUgibC1AsM35SYtcV996k,16948
|
|
8
|
-
cadwyn/changelogs.py,sha256=SdrdAKQ01mpzs-EN_zg-D0TY7wxsibjRjLMhGcI4q80,20066
|
|
9
|
-
cadwyn/exceptions.py,sha256=VlJKRmEGfFTDtHbOWc8kXK4yMi2N172K684Y2UIV8rI,1832
|
|
10
|
-
cadwyn/middleware.py,sha256=kUZK2dmoricMbv6knPCIHpXEInX2670XIwAj0v_XQxk,3408
|
|
11
|
-
cadwyn/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
12
|
-
cadwyn/route_generation.py,sha256=2qUTPHsG6u6yXUA5pLa3ubfZOc9FrISDMELmAFhDSUY,24696
|
|
13
|
-
cadwyn/routing.py,sha256=9AHSojmuLgUAQlLMIqXz-ViZ9n-fljgOsn7oxha7PjM,7341
|
|
14
|
-
cadwyn/schema_generation.py,sha256=LN2dLhH8uiqzfi1zFY284MFVO3WmoWbnS0rqpK4qDGY,40491
|
|
15
|
-
cadwyn/static/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
16
|
-
cadwyn/static/docs.html,sha256=WNm5ANJVy51TcIUFOaqKf1Z8eF86CC85TTHPxACtkzw,3455
|
|
17
|
-
cadwyn/structure/__init__.py,sha256=vej7TdTMSOg8U8Wk7GTNdA4rc6loA9083FWaTg4jAaY,655
|
|
18
|
-
cadwyn/structure/common.py,sha256=GUclfxKLRlFwPjT237fCtLIzdvjvC9gI3acuxBizwbg,414
|
|
19
|
-
cadwyn/structure/data.py,sha256=1ALPhBBCE_t4GrxM0Fa3hQ-jkORJgeWNySnZ42bsi0g,7382
|
|
20
|
-
cadwyn/structure/endpoints.py,sha256=9FFnbqPM9v0CP6J6tGhMNKVvWqA9u3ZjI2Fannr2Rl0,5933
|
|
21
|
-
cadwyn/structure/enums.py,sha256=bZL-iUOUFi9ZYlMZJw-tAix2yrgCp3gH3N2gwO44LUU,1043
|
|
22
|
-
cadwyn/structure/schemas.py,sha256=D0BD1D3v9MRdVWchU9JM2zHd0dvB0UgXHDGFCe5aQZc,8209
|
|
23
|
-
cadwyn/structure/versions.py,sha256=7vbKSj0XwYNDbDXRAhB4iHT1K1YCRAcvXsQrC6rZn7w,33731
|
|
24
|
-
cadwyn-4.4.3.dist-info/METADATA,sha256=7b1Xd6VwbPVBKhAj5chBWHl9A3XqXo5xua9RA6C2V8o,4504
|
|
25
|
-
cadwyn-4.4.3.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
26
|
-
cadwyn-4.4.3.dist-info/entry_points.txt,sha256=mGX8wl-Xfhpr5M93SUmkykaqinUaYAvW9rtDSX54gx0,47
|
|
27
|
-
cadwyn-4.4.3.dist-info/licenses/LICENSE,sha256=KeCWewiDQYpmSnzF-p_0YpoWiyDcUPaCuG8OWQs4ig4,1072
|
|
28
|
-
cadwyn-4.4.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|