Skip to content

Commit 11b6dbb

Browse files
committed
Added python 3.10 typing support.
Signed-off-by: Pavel Kirilin <win10@list.ru>
1 parent 11ab87c commit 11b6dbb

4 files changed

Lines changed: 21 additions & 17 deletions

File tree

aiohttp_deps/swagger.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import inspect
22
from collections import defaultdict
33
from logging import getLogger
4-
from typing import Any, Awaitable, Callable, Dict, Optional, Union
4+
from typing import Any, Awaitable, Callable, Dict, Optional, get_type_hints
55

66
import pydantic
77
from aiohttp import web
8-
from pydantic.utils import deep_update
8+
from deepmerge import always_merger
99
from taskiq_dependencies import DependencyGraph
1010

1111
from aiohttp_deps.initializer import InjectableFuncHandler, InjectableViewHandler
@@ -67,16 +67,11 @@ def _is_optional(annotation: Optional[inspect.Parameter]) -> bool:
6767
if annotation is None or annotation.annotation == annotation.empty:
6868
return True
6969

70-
origin = getattr(annotation.annotation, "__origin__", None)
71-
if origin is None:
72-
return False
70+
def dummy(_var: annotation.annotation) -> None: # type: ignore
71+
"""Dummy function to use for type resolution."""
7372

74-
if origin == Union:
75-
args = getattr(annotation.annotation, "__args__", ())
76-
for arg in args:
77-
if arg is type(None): # noqa: E721, WPS516
78-
return True
79-
return False
73+
var = get_type_hints(dummy).get("_var")
74+
return var == Optional[var]
8075

8176

8277
def _add_route_def( # noqa: C901
@@ -148,7 +143,7 @@ def _add_route_def( # noqa: C901
148143
)
149144

150145
openapi_schema["paths"][route.resource.canonical].update(
151-
{method.lower(): deep_update(route_info, extra_openapi)},
146+
{method.lower(): always_merger.merge(route_info, extra_openapi)},
152147
)
153148

154149

poetry.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ python = "^3.8.1"
2828
aiohttp = "^3"
2929
taskiq-dependencies = "^1"
3030
pydantic = "^2"
31+
deepmerge = "^1.1.0"
3132

3233
[tool.poetry.group.dev.dependencies]
3334
pytest = "^7.1.2"

tests/test_isoptional.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import inspect
22
from typing import Optional, Union
33

4-
import pytest
5-
64
from aiohttp_deps.swagger import _is_optional
75

86

@@ -42,7 +40,7 @@ def tfunc(param: Union[int, str]):
4240
assert not _is_optional(param)
4341

4442

45-
@pytest.mark.skip("We doesn't support 3.10 annotation style yet.")
43+
# @pytest.mark.skip("We doesn't support 3.10 annotation style yet.")
4644
def test_new_type_style():
4745
def tfunc(param: "int | None"):
4846
"""Nothing."""
@@ -52,7 +50,6 @@ def tfunc(param: "int | None"):
5250
assert _is_optional(param)
5351

5452

55-
@pytest.mark.skip("We doesn't support string annotations yet.")
5653
def test_string_annotation():
5754
def tfunc(param: "int | None"):
5855
"""Nothing."""

0 commit comments

Comments
 (0)