Skip to content

Commit

Permalink
refactor!: add __slots__ to App and Router and hide some App attribut…
Browse files Browse the repository at this point in the history
…es (#44)
  • Loading branch information
adriangb authored Feb 3, 2022
1 parent ed3acb2 commit 3383ed9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "xpresso"
version = "0.11.0"
version = "0.11.1"
description = "A developer centric, performant Python web framework"
authors = ["Adrian Garcia Badaracco <[email protected]>"]
readme = "README.md"
Expand Down
26 changes: 19 additions & 7 deletions xpresso/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ class App:
state: State
container: BaseContainer

__slots__ = (
"_debug",
"_openapi_info",
"_openapi_servers",
"_openapi_version",
"_setup_run",
"container",
"openapi",
"router",
"state",
)

def __init__(
self,
routes: typing.Optional[typing.Sequence[BaseRoute]] = None,
Expand Down Expand Up @@ -166,13 +178,13 @@ async def lifespan_ctx(*args: typing.Any) -> typing.AsyncIterator[None]:
lifespan=lifespan_ctx,
)

self.openapi_version = openapi_version
self.openapi_info = openapi_models.Info(
self._openapi_version = openapi_version
self._openapi_info = openapi_models.Info(
title=title,
version=version,
description=description,
)
self.servers = servers
self._openapi_servers = servers
self.openapi = None

async def __call__(
Expand Down Expand Up @@ -238,9 +250,9 @@ def get_openapi(self) -> openapi_models.OpenAPI:
app_type=App, router=self.router, nodes=[self, self.router], path=""
),
container=self.container,
version=self.openapi_version,
info=self.openapi_info,
servers=self.servers,
version=self._openapi_version,
info=self._openapi_info,
servers=self._openapi_servers,
)

def _get_doc_routes(
Expand Down Expand Up @@ -273,7 +285,7 @@ async def swagger_ui_html(req: Request) -> HTMLResponse:
full_openapi_url = root_path + openapi_url # type: ignore[operator]
return get_swagger_ui_html(
openapi_url=full_openapi_url,
title=self.openapi_info.title + " - Swagger UI",
title=self._openapi_info.title + " - Swagger UI",
oauth2_redirect_url=None,
init_oauth=None,
)
Expand Down
2 changes: 1 addition & 1 deletion xpresso/routing/pathitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class _PathApp:

__slots__ = ("operations",)

def __init__(self, operations: typing.Dict[str, Operation]) -> None:
def __init__(self, operations: typing.Mapping[str, Operation]) -> None:
self.operations = operations

def __call__(
Expand Down
21 changes: 15 additions & 6 deletions xpresso/routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,24 @@ def __call__(

class Router:
routes: typing.Sequence[BaseRoute]
lifespan: typing.Optional[
typing.Callable[..., typing.AsyncContextManager[None]]
] = None
lifespan: typing.Optional[typing.Callable[..., typing.AsyncContextManager[None]]]
dependencies: typing.Sequence[Dependant]
tags: typing.Sequence[str]
responses: Responses
include_in_schema: bool
_app: _ASGIApp

__slots__ = (
"_app",
"_router",
"dependencies",
"include_in_schema",
"lifespan",
"responses",
"routes",
"tags",
)

def __init__(
self,
routes: typing.Sequence[BaseRoute],
Expand Down Expand Up @@ -75,10 +84,10 @@ def __init__(
for cls, options in typing.cast(_MiddlewareIterator, reversed(middleware)):
self._app = cls(app=self._app, **options)

async def __call__(
def __call__(
self,
scope: Scope,
receive: Receive,
send: Send,
) -> None:
await self._app(scope, receive, send) # type: ignore[arg-type,call-arg,misc]
) -> typing.Awaitable[None]:
return self._app(scope, receive, send)

0 comments on commit 3383ed9

Please sign in to comment.