Skip to content

Commit

Permalink
Switched from black to ruff-format
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Jan 8, 2024
1 parent 03ff24b commit b1ebdb9
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 63 deletions.
10 changes: 3 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
rev: v0.1.11
hooks:
- id: ruff
args: [--fix, --show-fixes]

- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
rev: v1.8.0
hooks:
- id: mypy
additional_dependencies:
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ testpaths = ["tests"]

[tool.ruff]
line-length = 99

[tool.ruff.lint]
select = [
"E", "F", "W", # default flake-8
"I", # isort
Expand Down
15 changes: 4 additions & 11 deletions src/asphalt/web/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@


@middleware
async def asphalt_middleware(
request: Request, handler: Callable[..., Awaitable]
) -> Response:
async def asphalt_middleware(request: Request, handler: Callable[..., Awaitable]) -> Response:
async with Context() as ctx:
ctx.add_resource(request, types=[Request])
return await handler(request)
Expand All @@ -44,9 +42,7 @@ def __init__(
app: Application | str | None = None,
host: str = "127.0.0.1",
port: int = 8000,
middlewares: Sequence[
Callable[..., Coroutine[Any, Any, Any]] | dict[str, Any]
] = (),
middlewares: Sequence[Callable[..., Coroutine[Any, Any, Any]] | dict[str, Any]] = (),
) -> None:
super().__init__(components)

Expand Down Expand Up @@ -85,8 +81,7 @@ def add_middleware(
self.app.middlewares.append(middleware)
else:
raise TypeError(
f"middleware must be either a coroutine function or a dict, not "
f"{middleware!r}"
f"middleware must be either a coroutine function or a dict, not " f"{middleware!r}"
)

async def start(self, ctx: Context) -> None:
Expand All @@ -95,9 +90,7 @@ async def start(self, ctx: Context) -> None:
await self.start_server(ctx)

@context_teardown
async def start_server(
self, ctx: Context
) -> AsyncGenerator[None, Exception | None]:
async def start_server(self, ctx: Context) -> AsyncGenerator[None, Exception | None]:
"""
Start the HTTP server.
Expand Down
12 changes: 3 additions & 9 deletions src/asphalt/web/asgi3.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ def __init__(
def setup_asphalt_middleware(self, app: T_Application) -> ASGI3Application:
return AsphaltMiddleware(app)

def add_middleware(
self, middleware: Callable[..., ASGI3Application] | dict[str, Any]
) -> None:
def add_middleware(self, middleware: Callable[..., ASGI3Application] | dict[str, Any]) -> None:
"""
Add middleware to the application.
Expand All @@ -108,9 +106,7 @@ def add_middleware(
elif callable(middleware):
self.app = middleware(self.app)
else:
raise TypeError(
f"middleware must be either a callable or a dict, not {middleware!r}"
)
raise TypeError(f"middleware must be either a callable or a dict, not {middleware!r}")

async def start(self, ctx: Context) -> None:
types = [ASGI3Application]
Expand All @@ -122,9 +118,7 @@ async def start(self, ctx: Context) -> None:
await self.start_server(ctx)

@context_teardown
async def start_server(
self, ctx: Context
) -> AsyncGenerator[None, Exception | None]:
async def start_server(self, ctx: Context) -> AsyncGenerator[None, Exception | None]:
"""
Start the HTTP server.
Expand Down
8 changes: 2 additions & 6 deletions src/asphalt/web/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ def __init__(
def setup_asphalt_middleware(self, app: FastAPI) -> ASGI3Application:
return AsphaltMiddleware(app)

def add_middleware(
self, middleware: Callable[..., ASGI3Application] | dict[str, Any]
) -> None:
def add_middleware(self, middleware: Callable[..., ASGI3Application] | dict[str, Any]) -> None:
"""
Add a middleware to the application.
Expand All @@ -108,9 +106,7 @@ def add_middleware(
elif callable(middleware):
self.app.add_middleware(middleware)
else:
raise TypeError(
f"middleware must be either a callable or a dict, not {middleware!r}"
)
raise TypeError(f"middleware must be either a callable or a dict, not {middleware!r}")

async def start_server(self, ctx: Context) -> None:
# Convert Asphalt dependencies into FastAPI dependencies
Expand Down
4 changes: 1 addition & 3 deletions src/asphalt/web/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ def __init__(
config_.setdefault("debug", __debug__)
config_["logging_config"] = None
app = Litestar(**config_)
super().__init__(
components, app=app, middlewares=middlewares, host=host, port=port
)
super().__init__(components, app=app, middlewares=middlewares, host=host, port=port)

for item in route_handlers:
if isinstance(item, str):
Expand Down
12 changes: 3 additions & 9 deletions src/asphalt/web/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:

await super().__call__(scope, receive, send)

async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
current_context().add_resource(request)
return await call_next(request)

Expand Down Expand Up @@ -68,9 +66,7 @@ def __init__(
def setup_asphalt_middleware(self, app: Starlette) -> ASGI3Application:
return AsphaltMiddleware(app)

def add_middleware(
self, middleware: Callable[..., ASGI3Application] | dict[str, Any]
) -> None:
def add_middleware(self, middleware: Callable[..., ASGI3Application] | dict[str, Any]) -> None:
"""
Add a middleware to the application.
Expand All @@ -94,6 +90,4 @@ def add_middleware(
elif callable(middleware):
self.app.add_middleware(middleware)
else:
raise TypeError(
f"middleware must be either a callable or a dict, not {middleware!r}"
)
raise TypeError(f"middleware must be either a callable or a dict, not {middleware!r}")
12 changes: 6 additions & 6 deletions tests/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ async def start(self, ctx: Context) -> None:
async with Context() as ctx, AsyncClient() as http:
ctx.add_resource("foo")
ctx.add_resource("bar", name="another")
await AIOHTTPComponent(
components=components, app=application, port=unused_tcp_port
).start(ctx)
await AIOHTTPComponent(components=components, app=application, port=unused_tcp_port).start(
ctx
)

# Ensure that the application got added as a resource
ctx.require_resource(Application)
Expand Down Expand Up @@ -117,9 +117,9 @@ async def start(self, ctx: Context) -> None:
async with Context() as ctx:
ctx.add_resource("foo")
ctx.add_resource("bar", name="another")
await AIOHTTPComponent(
components=components, app=application, port=unused_tcp_port
).start(ctx)
await AIOHTTPComponent(components=components, app=application, port=unused_tcp_port).start(
ctx
)

# Ensure that the application got added as a resource
ctx.require_resource(Application)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_asgi3.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ async def test_middleware(unused_tcp_port: int, method: str):
async with Context() as ctx, AsyncClient() as http:
ctx.add_resource("foo")
ctx.add_resource("bar", name="another")
await ASGIComponent(
app=application, port=unused_tcp_port, middlewares=middlewares
).start(ctx)
await ASGIComponent(app=application, port=unused_tcp_port, middlewares=middlewares).start(
ctx
)

# Ensure that the application got added as a resource
ctx.require_resource(ASGI3Application)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ async def start(self, ctx: Context, app: FastAPI = resource()) -> None:
async with Context() as ctx, AsyncClient() as http:
ctx.add_resource("foo")
ctx.add_resource("bar", name="another")
await FastAPIComponent(
components=components, app=application, port=unused_tcp_port
).start(ctx)
await FastAPIComponent(components=components, app=application, port=unused_tcp_port).start(
ctx
)

# Ensure that the application got added as a resource
asgi_app = ctx.require_resource(ASGI3Application)
Expand Down Expand Up @@ -108,9 +108,9 @@ async def start(self, ctx: Context, app: FastAPI = resource()) -> None:
async with Context() as ctx:
ctx.add_resource("foo")
ctx.add_resource("bar", name="another")
await FastAPIComponent(
components=components, app=application, port=unused_tcp_port
).start(ctx)
await FastAPIComponent(components=components, app=application, port=unused_tcp_port).start(
ctx
)

# Ensure that the application got added as a resource
asgi_app = ctx.require_resource(ASGI3Application)
Expand Down
4 changes: 1 addition & 3 deletions tests/test_litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ async def test_dependency_injection(unused_tcp_port: int) -> None:
"another_resource": AsphaltProvide(str, "another"),
},
)
async def root(
request: Request, my_resource: str, another_resource: str
) -> Dict[str, Any]: # noqa: UP006
async def root(request: Request, my_resource: str, another_resource: str) -> Dict[str, Any]: # noqa: UP006
my_resource = require_resource(str)
another_resource = require_resource(str, "another")
require_resource(HTTPScope)
Expand Down

0 comments on commit b1ebdb9

Please sign in to comment.