Skip to content

Commit

Permalink
Merge branch 'release/1.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
s3rius committed Feb 5, 2024
2 parents 4e926d6 + 6e7ecb2 commit 7ceefb1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
11 changes: 11 additions & 0 deletions aiohttp_deps/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ def __init__(
) -> None:
self.original_handler = copy.copy(original_route)
self.graph = DependencyGraph(self.original_handler)
signature = inspect.signature(self.original_handler)
# This flag means that the function requires one argument and
# doesn't depend on any other dependencies.
# We assume that such functions should be treated as ordinary
# aiohttp handlers and therefore we don't inject any dependencies
# and pass request object directly to the handler.
self.is_ordinary = False
if self.graph.is_empty() and len(signature.parameters) == 1:
self.is_ordinary = True

async def __call__(self, request: web.Request) -> web.StreamResponse:
"""
Expand All @@ -41,6 +50,8 @@ async def __call__(self, request: web.Request) -> web.StreamResponse:
:param request: current request.
:return: response.
"""
if self.is_ordinary:
return await self.original_handler(request)
# Hack for mypy to work
values_overrides = request.app.get(VALUES_OVERRIDES_KEY)
if values_overrides is None:
Expand Down
8 changes: 6 additions & 2 deletions aiohttp_deps/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def setup_swagger( # noqa: C901
swagger_ui_url: str = "/docs",
enable_ui: bool = True,
hide_heads: bool = True,
hide_options: bool = True,
title: str = "AioHTTP",
description: Optional[str] = None,
version: str = "1.0.0",
Expand All @@ -225,13 +226,14 @@ def setup_swagger( # noqa: C901
:param swagger_ui_url: URL where swagger ui will be served.
:param enable_ui: whether you want to enable bundled swagger ui.
:param hide_heads: hide HEAD requests.
:param hide_options: hide OPTIONS requests.
:param title: Title of an application.
:param description: description of an application.
:param version: version of an application.
:return: startup event handler.
"""

async def event_handler(app: web.Application) -> None:
async def event_handler(app: web.Application) -> None: # noqa: C901
openapi_schema = {
"openapi": "3.0.0",
"info": {
Expand All @@ -245,7 +247,9 @@ async def event_handler(app: web.Application) -> None:
for route in app.router.routes():
if route.resource is None: # pragma: no cover
continue
if hide_heads and route.method == "HEAD":
if hide_heads and route.method.lower() == "HEAD": # pragma: no cover
continue
if hide_options and route.method.lower() == "OPTIONS": # pragma: no cover
continue
if isinstance(route._handler, InjectableFuncHandler):
extra_openapi = getattr(
Expand Down
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "aiohttp-deps"
description = "Dependency injection for AioHTTP"
authors = ["Taskiq team <[email protected]>"]
maintainers = ["Taskiq team <[email protected]>"]
version = "1.1.0"
version = "1.1.1"
readme = "README.md"
license = "LICENSE"
classifiers = [
Expand Down Expand Up @@ -47,12 +47,9 @@ ruff = "^0.1.7"
[tool.mypy]
strict = true
ignore_missing_imports = true
allow_subclassing_any = true
allow_untyped_calls = true
pretty = true
show_error_codes = true
implicit_reexport = true
allow_untyped_decorators = true
warn_return_any = false
warn_unused_ignores = false

Expand Down
15 changes: 15 additions & 0 deletions tests/test_func_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,18 @@ async def handler(num: int = Depends(original_dep)) -> web.Response:
resp = await client.get("/")
assert resp.status == 200
assert (await resp.json())["request"] == 2


@pytest.mark.anyio
async def test_ordinary_functions_support(
my_app: web.Application,
aiohttp_client: ClientGenerator,
) -> None:
async def handler(request: web.Request) -> web.Response:
return web.json_response({"request": "ordinary"})

my_app.router.add_get("/", handler)
client = await aiohttp_client(my_app)
resp = await client.get("/")
assert resp.status == 200
assert await resp.json() == {"request": "ordinary"}

0 comments on commit 7ceefb1

Please sign in to comment.