Skip to content

Commit

Permalink
aiohttp: fix AppKey warning & remove 3.12 guards
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Nov 24, 2023
1 parent d8524cb commit f67be59
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 29 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/

## [Unreleased](https://github.com/hynek/svcs/compare/23.21.0...HEAD)

### Fixed

- AIOHTTP: The registry is now stored using `aiohttp.web.AppKey`s on the application.
This is an implementation detail and shouldn't matter, but it fixes a warning on AIOHTTP 3.9 and later.


## [23.21.0](https://github.com/hynek/svcs/compare/23.20.0...23.21.0) - 2023-11-21

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ typing = [
"fastapi",
"flask",
"starlette",
"aiohttp; python_version<'3.12'",
"pyramid; python_version<'3.12'",
"aiohttp",
"pyramid",
]
docs = [
"sphinx>=7.2.2",
Expand Down
21 changes: 16 additions & 5 deletions src/svcs/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,29 @@
)


try:
_AIOHTTP_KEY_REGISTRY = web.AppKey(_KEY_REGISTRY, svcs.Registry)
except (
AttributeError
): # pragma: no cover -- not adding a tox env for aiohttp<3.9
_AIOHTTP_KEY_REGISTRY = _KEY_REGISTRY # type: ignore[assignment]

# No equivalent of AppKey for Requests, yet?
_AIOHTTP_KEY_CONTAINER = _KEY_CONTAINER


def svcs_from(request: web.Request) -> svcs.Container:
"""
Get the current container from *request*.
"""
return request[_KEY_CONTAINER] # type: ignore[no-any-return]
return request[_AIOHTTP_KEY_CONTAINER] # type: ignore[no-any-return]


def get_registry(app: web.Application) -> svcs.Registry:
"""
Get the registry from *app*.
"""
return app[_KEY_REGISTRY] # type: ignore[no-any-return]
return app[_AIOHTTP_KEY_REGISTRY]


def init_app(
Expand All @@ -54,7 +65,7 @@ def init_app(
Inserts the *svcs* middleware at *middleware_pos* which is 0 by default, so
you can use :func:`svcs_from` and :func:`aget` in other middlewares.
"""
app[_KEY_REGISTRY] = registry or svcs.Registry()
app[_AIOHTTP_KEY_REGISTRY] = registry or svcs.Registry()
app.middlewares.insert(middleware_pos, svcs_middleware)
app.on_cleanup.append(aclose_registry)

Expand All @@ -65,8 +76,8 @@ def init_app(
async def svcs_middleware(
request: web.Request, handler: Callable
) -> web.Response:
async with svcs.Container(request.app[_KEY_REGISTRY]) as container:
request[_KEY_CONTAINER] = container
async with svcs.Container(request.app[_AIOHTTP_KEY_REGISTRY]) as container:
request[_AIOHTTP_KEY_CONTAINER] = container

return await handler(request) # type: ignore[no-any-return]

Expand Down
22 changes: 0 additions & 22 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,6 @@ commands =
tests: coverage run -m pytest {posargs}
mypy: mypy tests/typing docs/examples/fastapi/simple_fastapi_app.py docs/examples/starlette/simple_starlette_app.py

# The next two environments are necessary because AIOHTTP and Pyramid do not
# support Python 3.12 yet.

[testenv:py312-tests-optional]
deps =
coverage[toml]
httpx
fastapi
flask
starlette

[testenv:py312-mypy]
deps =
fastapi
flask
commands =
mypy \
tests/typing/flask.py \
tests/typing/fastapi.py \
docs/examples/fastapi/simple_fastapi_app.py \
docs/examples/starlette/simple_starlette_app.py


[testenv:coverage-report]
description = Report coverage over all test runs.
Expand Down

0 comments on commit f67be59

Please sign in to comment.