Skip to content

Commit

Permalink
Merge pull request #180 from python-ellar/starlette_0_37_support
Browse files Browse the repository at this point in the history
Starlette 0.37.1 support
  • Loading branch information
eadwinCode authored Feb 14, 2024
2 parents 7091d1f + a5a8616 commit 448ae74
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 22 deletions.
8 changes: 3 additions & 5 deletions ellar/app/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from ellar.core.modules import ModuleRefBase
from ellar.di import EllarInjector, ProviderConfig
from ellar.reflect import reflect
from ellar.threading import run_as_async
from ellar.utils import get_unique_type
from starlette.routing import BaseRoute, Host, Mount

Expand Down Expand Up @@ -119,8 +118,7 @@ def _build_modules(
return routes

@classmethod
@run_as_async
async def _create_app(
def _create_app(
cls,
module: t.Type[t.Union[ModuleBase, t.Any]],
global_guards: t.Optional[
Expand Down Expand Up @@ -206,7 +204,7 @@ def create_app(
)
app_factory_module = get_unique_type()
module(app_factory_module)
return cls._create_app( # type:ignore[no-any-return]
return cls._create_app(
module=app_factory_module,
config_module=config_module,
global_guards=global_guards,
Expand All @@ -221,6 +219,6 @@ def create_from_app_module(
] = None,
config_module: t.Union[str, t.Dict, None] = None,
) -> App:
return cls._create_app( # type:ignore[no-any-return]
return cls._create_app(
module, config_module=config_module, global_guards=global_guards
)
2 changes: 1 addition & 1 deletion ellar/common/exceptions/callable_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def catch(
args = tuple(list(self.func_args) + [ctx, exc])
if self.is_async:
return await self.callable_exception_handler(*args) # type:ignore[misc]
return await run_in_threadpool(self.callable_exception_handler, *args)
return await run_in_threadpool(self.callable_exception_handler, *args) # type:ignore[arg-type]

def __eq__(self, other: t.Any) -> bool:
if isinstance(other, CallableExceptionHandler):
Expand Down
10 changes: 6 additions & 4 deletions ellar/core/middleware/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@


class EllarMiddleware(Middleware, IEllarMiddleware):
@t.no_type_check
def __init__(self, cls: t.Type[T], **options: t.Any) -> None:
super().__init__(cls, **options)
injectable()(self.cls)
self.options = build_init_kwargs(self.cls, self.options)
self.kwargs = build_init_kwargs(self.cls, self.kwargs)

def __call__(self, app: ASGIApp, injector: EllarInjector) -> T: # type:ignore[type-var]
self.options.update(app=app)
return injector.create_object(self.cls, additional_kwargs=self.options)
@t.no_type_check
def __call__(self, app: ASGIApp, injector: EllarInjector) -> T:
self.kwargs.update(app=app)
return injector.create_object(self.cls, additional_kwargs=self.kwargs)
6 changes: 3 additions & 3 deletions ellar/core/routing/file_mount.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os.path
import typing as t

from ellar.common.staticfiles import StaticFiles
from ellar.common.types import ASGIApp
from ellar.core.staticfiles import StaticFiles
from ellar.utils.importer import get_main_directory_by_stack
from starlette.middleware import Middleware
from starlette.routing import BaseRoute, Mount
Expand Down Expand Up @@ -37,8 +37,8 @@ def __init__(

def _combine_app_with_middleware(self, app: ASGIApp) -> ASGIApp:
if self._middleware is not None:
for cls, options in reversed(self._middleware):
app = cls(app=app, **options)
for cls, _, kwargs in reversed(self._middleware):
app = cls(app=app, **kwargs)
return app

@property
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion ellar/testing/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_test_client(
base_url: str = "http://testserver",
raise_server_exceptions: bool = True,
root_path: str = "",
backend: str = "asyncio",
backend: t.Literal["asyncio", "trio"] = "asyncio",
backend_options: t.Optional[t.Dict[str, t.Any]] = None,
**kwargs: t.Any,
) -> TestClient:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ classifiers = [

dependencies = [
"injector == 0.21.0",
"starlette == 0.31.1",
"starlette == 0.37.1",
"pydantic >=2.5.1,<3.0.0",
"jinja2",
# CLI
Expand Down Expand Up @@ -81,7 +81,7 @@ test = [
"types-redis ==4.6.0.20240106",
"types-dataclasses ==0.6.6",
"python-socketio",
"uvicorn[standard] == 0.25.0",
"uvicorn[standard] == 0.27.1",
"aiohttp == 3.9.3",
"argon2-cffi == 23.1.0"
]
Expand Down
4 changes: 1 addition & 3 deletions tests/test_auth/test_session/test_session_client_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ def test_session_cookie_sub_path():
)
test_module.override_provider(SessionStrategy, use_class=SessionClientStrategy)

client_second_app = test_module.get_test_client(
base_url="http://testserver/second_app"
)
client_second_app = test_module.get_test_client(root_path="/second_app")
client = test_module.get_test_client(base_url="http://testserver/")

response = client_second_app.post("/second_app/", json={"some": "data"})
Expand Down
2 changes: 1 addition & 1 deletion tests/test_modules/test_module_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ async def middleware_func(cls, context, call_next):
config_middleware = config[MIDDLEWARE_HANDLERS_KEY]

assert isinstance(config_middleware, list)
assert "middleware_func" == get_name(config_middleware[0].options["dispatch"])
assert "middleware_func" == get_name(config_middleware[0].kwargs["dispatch"])


def test_module_template_ref_get_all_routers():
Expand Down
3 changes: 2 additions & 1 deletion tests/test_routing/test_route_endpoint_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ def get_requests_case_1(

@router.get("/others")
def get_requests_case_2(
request: StarletteRequest,
session: Inject[dict, Inject.Key("Session")],
host: Inject[str, Inject.Key("Host")],
config: Inject[Config],
):
assert isinstance(config, Config) # True
assert host == "testclient"
assert host is None # Starlette TestClient client info is None
assert isinstance(session, dict) and len(session) == 0
return True

Expand Down
2 changes: 1 addition & 1 deletion tests/test_staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import anyio
import pytest
from ellar.app import AppFactory
from ellar.common.staticfiles import StaticFiles
from ellar.core.staticfiles import StaticFiles
from ellar.testing import TestClient
from starlette.exceptions import HTTPException
from starlette.routing import Mount
Expand Down

0 comments on commit 448ae74

Please sign in to comment.