Skip to content

Commit 7ceefb1

Browse files
committed
Merge branch 'release/1.1.1'
2 parents 4e926d6 + 6e7ecb2 commit 7ceefb1

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

aiohttp_deps/initializer.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ def __init__(
3030
) -> None:
3131
self.original_handler = copy.copy(original_route)
3232
self.graph = DependencyGraph(self.original_handler)
33+
signature = inspect.signature(self.original_handler)
34+
# This flag means that the function requires one argument and
35+
# doesn't depend on any other dependencies.
36+
# We assume that such functions should be treated as ordinary
37+
# aiohttp handlers and therefore we don't inject any dependencies
38+
# and pass request object directly to the handler.
39+
self.is_ordinary = False
40+
if self.graph.is_empty() and len(signature.parameters) == 1:
41+
self.is_ordinary = True
3342

3443
async def __call__(self, request: web.Request) -> web.StreamResponse:
3544
"""
@@ -41,6 +50,8 @@ async def __call__(self, request: web.Request) -> web.StreamResponse:
4150
:param request: current request.
4251
:return: response.
4352
"""
53+
if self.is_ordinary:
54+
return await self.original_handler(request)
4455
# Hack for mypy to work
4556
values_overrides = request.app.get(VALUES_OVERRIDES_KEY)
4657
if values_overrides is None:

aiohttp_deps/swagger.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ def setup_swagger( # noqa: C901
203203
swagger_ui_url: str = "/docs",
204204
enable_ui: bool = True,
205205
hide_heads: bool = True,
206+
hide_options: bool = True,
206207
title: str = "AioHTTP",
207208
description: Optional[str] = None,
208209
version: str = "1.0.0",
@@ -225,13 +226,14 @@ def setup_swagger( # noqa: C901
225226
:param swagger_ui_url: URL where swagger ui will be served.
226227
:param enable_ui: whether you want to enable bundled swagger ui.
227228
:param hide_heads: hide HEAD requests.
229+
:param hide_options: hide OPTIONS requests.
228230
:param title: Title of an application.
229231
:param description: description of an application.
230232
:param version: version of an application.
231233
:return: startup event handler.
232234
"""
233235

234-
async def event_handler(app: web.Application) -> None:
236+
async def event_handler(app: web.Application) -> None: # noqa: C901
235237
openapi_schema = {
236238
"openapi": "3.0.0",
237239
"info": {
@@ -245,7 +247,9 @@ async def event_handler(app: web.Application) -> None:
245247
for route in app.router.routes():
246248
if route.resource is None: # pragma: no cover
247249
continue
248-
if hide_heads and route.method == "HEAD":
250+
if hide_heads and route.method.lower() == "HEAD": # pragma: no cover
251+
continue
252+
if hide_options and route.method.lower() == "OPTIONS": # pragma: no cover
249253
continue
250254
if isinstance(route._handler, InjectableFuncHandler):
251255
extra_openapi = getattr(

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "aiohttp-deps"
33
description = "Dependency injection for AioHTTP"
44
authors = ["Taskiq team <taskiq@no-reply.com>"]
55
maintainers = ["Taskiq team <taskiq@no-reply.com>"]
6-
version = "1.1.0"
6+
version = "1.1.1"
77
readme = "README.md"
88
license = "LICENSE"
99
classifiers = [
@@ -47,12 +47,9 @@ ruff = "^0.1.7"
4747
[tool.mypy]
4848
strict = true
4949
ignore_missing_imports = true
50-
allow_subclassing_any = true
51-
allow_untyped_calls = true
5250
pretty = true
5351
show_error_codes = true
5452
implicit_reexport = true
55-
allow_untyped_decorators = true
5653
warn_return_any = false
5754
warn_unused_ignores = false
5855

tests/test_func_dependencies.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,18 @@ async def handler(num: int = Depends(original_dep)) -> web.Response:
7979
resp = await client.get("/")
8080
assert resp.status == 200
8181
assert (await resp.json())["request"] == 2
82+
83+
84+
@pytest.mark.anyio
85+
async def test_ordinary_functions_support(
86+
my_app: web.Application,
87+
aiohttp_client: ClientGenerator,
88+
) -> None:
89+
async def handler(request: web.Request) -> web.Response:
90+
return web.json_response({"request": "ordinary"})
91+
92+
my_app.router.add_get("/", handler)
93+
client = await aiohttp_client(my_app)
94+
resp = await client.get("/")
95+
assert resp.status == 200
96+
assert await resp.json() == {"request": "ordinary"}

0 commit comments

Comments
 (0)