Skip to content

Commit

Permalink
Fixed pre-commit errors
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Apr 8, 2022
1 parent 47dd7db commit 638db09
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repos:
hooks:
- id: check-toml
- id: check-yaml
exclude: "^examples/"
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
Expand Down Expand Up @@ -40,7 +41,7 @@ repos:
rev: v0.942
hooks:
- id: mypy
exclude: "^examples/"
exclude: "^examples/|^tests/django_app/"

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions examples/aiohttp/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ To run this example:
.. code-block:: bash
pip install asphalt-web[aiohttp]
PYTHONPATH=. asphalt run --unsafe config.yaml
PYTHONPATH=. asphalt run config.yaml
Then, navigate to http://localhost:8080 in your browser.
Then, navigate to http://localhost:8000 in your browser.
4 changes: 2 additions & 2 deletions examples/aiohttp/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
web:
component:
type: !!python/name:example.WebComponent
app: !!python/name:example.app
type: example:WebComponent
app: example:app
4 changes: 3 additions & 1 deletion examples/asgi/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ To run this example:
.. code-block:: bash
pip install asphalt-web[asgi]
PYTHONPATH=. asphalt run --unsafe config.yaml
PYTHONPATH=. asphalt run config.yaml
Then, navigate to http://localhost:8000 in your browser.
4 changes: 2 additions & 2 deletions examples/asgi/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
web:
component:
type: !!python/name:example.WebComponent
app: !!python/name:example.application
type: example:WebComponent
app: example:application
4 changes: 3 additions & 1 deletion examples/django/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ To run this example:
.. code-block:: bash
pip install asphalt-web[django]
PYTHONPATH=. asphalt run --unsafe config.yaml
PYTHONPATH=. asphalt run config.yaml
Then, navigate to http://localhost:8000 in your browser.
4 changes: 2 additions & 2 deletions examples/django/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
web:
component:
type: !!python/name:django_example.component.WebComponent
app: !!python/name:django_example.asgi.application
type: django_example.component:WebComponent
app: django_example.asgi:application
2 changes: 2 additions & 0 deletions examples/fastapi/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ To run this example:
pip install asphalt-web[fastapi]
PYTHONPATH=. asphalt run config.yaml
Then, navigate to http://localhost:8000 in your browser.
4 changes: 2 additions & 2 deletions examples/fastapi/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
web:
component:
type: !!python/name:example.WebComponent
app: !!python/name:example.application
type: example:WebComponent
app: example:application
2 changes: 2 additions & 0 deletions examples/starlette/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ To run this example:
pip install asphalt-web[starlette]
PYTHONPATH=. asphalt run config.yaml
Then, navigate to http://localhost:8000 in your browser.
4 changes: 2 additions & 2 deletions examples/starlette/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
web:
component:
type: !!python/name:example.WebComponent
app: !!python/name:example.application
type: example:WebComponent
app: example:application
16 changes: 10 additions & 6 deletions src/asphalt/web/aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
from __future__ import annotations

from collections.abc import AsyncIterator
from typing import Any, Awaitable, Callable, Dict, Optional
from typing import Any, Awaitable, Callable, Dict, Optional, Union

from aiohttp.web_app import Application
from aiohttp.web_middlewares import middleware
from aiohttp.web_request import Request
from aiohttp.web_response import Response
from aiohttp.web_runner import AppRunner, TCPSite
from asphalt.core import (
ContainerComponent,
Context,
context_teardown,
resolve_reference,
)
from typeguard import check_argument_types

from asphalt.core import ContainerComponent, Context, context_teardown


@middleware
async def asphalt_middleware(
Expand All @@ -27,16 +31,16 @@ def __init__(
self,
components: Dict[str, Optional[Dict[str, Any]]] = None,
*,
app: Application,
app: Union[str, Application],
site: Optional[Dict[str, Any]] = None,
) -> None:
check_argument_types()
super().__init__(components)

self.app = app
self.app = resolve_reference(app)
self.app.middlewares.append(asphalt_middleware)
self.site_options = site or {}
self.site_options.setdefault('port', 8000)
self.site_options.setdefault("port", 8000)

@context_teardown
async def start(self, ctx: Context) -> AsyncIterator[None]:
Expand Down
7 changes: 1 addition & 6 deletions src/asphalt/web/asgi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

from asyncio import create_task, sleep
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any, Dict, Generic, Optional, TypeVar

Expand All @@ -20,7 +19,6 @@
current_context,
resolve_reference,
)
from typeguard import check_argument_types
from uvicorn import Config

T_Application = TypeVar("T_Application", bound=ASGI3Application)
Expand Down Expand Up @@ -52,10 +50,7 @@ def __init__(
port: int = 8000,
) -> None:
super().__init__(components)
if isinstance(app, str):
app = resolve_reference(app)

self.app: T_Application = app
self.app: T_Application = resolve_reference(app)
self.host = host
self.port = port

Expand Down
34 changes: 18 additions & 16 deletions tests/test_aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import pytest

pytest.importorskip('aiohttp', reason='aiohttp not available')

from aiohttp.abc import Request
from aiohttp.web_app import Application
from aiohttp.web_response import json_response, Response
from aiohttp.web_routedef import RouteTableDef
from asphalt.core import Context, Dependency, inject
from httpx import AsyncClient

from asphalt.core import Context, inject, Dependency
from asphalt.web.aiohttp import AIOHTTPComponent

routes = RouteTableDef()


@routes.get("/")
@inject
async def root(
request: Request,
request,
my_resource: str = Dependency(),
another_resource: str = Dependency("another"),
) -> Response:
):
from aiohttp.web_response import json_response

return json_response(
{
"message": request.query["param"],
Expand All @@ -32,14 +24,24 @@ async def root(

@pytest.mark.asyncio
async def test_aiohttp(unused_tcp_port: int):
pytest.importorskip("aiohttp", reason="aiohttp not available")

from aiohttp.web_app import Application
from aiohttp.web_routedef import RouteTableDef

routes = RouteTableDef()
routes.get("/")(root)
application = Application()
application.add_routes(routes)
async with Context() as ctx, AsyncClient() as http:
ctx.add_resource("foo")
ctx.add_resource("bar", name="another")
await AIOHTTPComponent(app=application, site={'port': unused_tcp_port}).start(ctx)
response = await http.get(f"http://127.0.0.1:{unused_tcp_port}",
params={"param": "Hello World"})
await AIOHTTPComponent(app=application, site={"port": unused_tcp_port}).start(
ctx
)
response = await http.get(
f"http://127.0.0.1:{unused_tcp_port}", params={"param": "Hello World"}
)
response.raise_for_status()
assert response.json() == {
"message": "Hello World",
Expand Down
12 changes: 7 additions & 5 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import json
from typing import cast
from urllib.parse import parse_qs

import pytest
from asgiref.typing import HTTPScope, ASGIReceiveCallable, ASGISendCallable
from asgiref.typing import ASGIReceiveCallable, ASGISendCallable, HTTPScope
from asphalt.core import Context, Dependency, inject
from httpx import AsyncClient

from asphalt.core import Context, Dependency, inject
from asphalt.web.asgi import ASGIComponent


Expand All @@ -18,7 +19,7 @@ async def application(
another_resource: str = Dependency("another"),
):
assert scope["type"] == "http"
query = parse_qs(scope["query_string"])
query = parse_qs(cast(bytes, scope["query_string"]))
await receive()
await send(
{
Expand Down Expand Up @@ -50,8 +51,9 @@ async def test_asgi(unused_tcp_port: int):
ctx.add_resource("foo")
ctx.add_resource("bar", name="another")
await ASGIComponent(app=application, port=unused_tcp_port).start(ctx)
response = await http.get(f"http://127.0.0.1:{unused_tcp_port}",
params={"param": "Hello World"})
response = await http.get(
f"http://127.0.0.1:{unused_tcp_port}", params={"param": "Hello World"}
)
response.raise_for_status()
assert response.json() == {
"message": "Hello World",
Expand Down
7 changes: 4 additions & 3 deletions tests/test_fastapi.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import pytest
from asphalt.core import Context
from fastapi import FastAPI
from httpx import AsyncClient
from starlette.requests import Request
from starlette.responses import JSONResponse, Response

from asphalt.core import Context
from asphalt.web.fastapi import AsphaltDepends, FastAPIComponent

application = FastAPI()
Expand All @@ -31,8 +31,9 @@ async def test_fastapi(unused_tcp_port: int):
ctx.add_resource("foo")
ctx.add_resource("bar", name="another")
await FastAPIComponent(app=application, port=unused_tcp_port).start(ctx)
response = await http.get(f"http://127.0.0.1:{unused_tcp_port}",
params={"param": "Hello World"})
response = await http.get(
f"http://127.0.0.1:{unused_tcp_port}", params={"param": "Hello World"}
)
response.raise_for_status()
assert response.json() == {
"message": "Hello World",
Expand Down
9 changes: 4 additions & 5 deletions tests/test_starlette.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pytest
from httpx import AsyncClient
from uvicorn import Server

from asphalt.core import Context, Dependency, inject
from httpx import AsyncClient
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
Expand Down Expand Up @@ -40,8 +38,9 @@ async def test_starlette(unused_tcp_port: int):
ctx.add_resource("foo")
ctx.add_resource("bar", name="another")
await StarletteComponent(app=application, port=unused_tcp_port).start(ctx)
response = await http.get(f"http://127.0.0.1:{unused_tcp_port}",
params={"param": "Hello World"})
response = await http.get(
f"http://127.0.0.1:{unused_tcp_port}", params={"param": "Hello World"}
)
response.raise_for_status()
assert response.json() == {
"message": "Hello World",
Expand Down

0 comments on commit 638db09

Please sign in to comment.