Skip to content

Commit 3a91a1a

Browse files
authored
Merge pull request #81 from eadwinCode/starlette_upgrade
Starlette Upgrade
2 parents d062a8a + 3afbe38 commit 3a91a1a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+466
-497
lines changed

docs/basics/testing.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,19 @@ During testing, there two ways to apply or modify configuration.
246246
test_module = Test.create_test_module(
247247
controllers=[CarController,],
248248
providers=[ProviderConfig(CarRepository, use_class=CarRepository)],
249-
config_module='project_name.config.TestingConfiguration'
249+
config_module='project_name.config:TestingConfiguration'
250250
)
251251
self.controller: CarController = test_module.get(CarController)
252252
```
253+
Also, we can expose the testing config to environment for more global scope, for example:
254+
255+
```python
256+
# project_name/tests/conftest.py
257+
import os
258+
from ellar.constants import ELLAR_CONFIG_MODULE
259+
os.environ.setdefault(ELLAR_CONFIG_MODULE, 'project_name.config:TestingConfiguration')
260+
```
261+
253262
=== "Inline"
254263
This method doesn't require configuration file, we simply go ahead and define the configuration variables in a dictionary type set to `config_module`.
255264

ellar/common/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
extra_args,
1414
file,
1515
middleware,
16-
on_shutdown,
17-
on_startup,
1816
openapi_info,
1917
render,
2018
serializer_filter,
@@ -89,8 +87,6 @@
8987
"middleware",
9088
"exception_handler",
9189
"serializer_filter",
92-
"on_shutdown",
93-
"on_startup",
9490
"template_filter",
9591
"template_global",
9692
"Res",

ellar/common/decorators/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from .middleware import middleware
1212
from .modules import Module
1313
from .openapi import openapi_info
14-
from .request import on_shutdown, on_startup
1514
from .serializer import serializer_filter
1615
from .versioning import Version
1716

@@ -24,9 +23,7 @@
2423
"template_global",
2524
"file",
2625
"render",
27-
"on_startup",
2826
"exception_handler",
29-
"on_shutdown",
3027
"command",
3128
"set_metadata",
3229
"middleware",

ellar/common/decorators/request.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

ellar/constants.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ def __new__(mcls, name, bases, namespace):
6464
RESPONSE_OVERRIDE_KEY = "RESPONSE_OVERRIDE"
6565
EXCEPTION_HANDLERS_KEY = "EXCEPTION_HANDLERS"
6666

67-
ON_REQUEST_STARTUP_KEY = "ON_REQUEST_STARTUP"
68-
ON_REQUEST_SHUTDOWN_KEY = "ON_REQUEST_SHUTDOWN"
69-
7067
TEMPLATE_GLOBAL_KEY = "TEMPLATE_GLOBAL_FILTERS"
7168
TEMPLATE_FILTER_KEY = "TEMPLATE_FILTERS"
7269

ellar/core/conf/app_settings_models.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,15 @@
1111
from ellar.serializer import Serializer, SerializerFilter
1212
from ellar.types import ASGIApp, TReceive, TScope, TSend
1313

14-
from .mixins import (
15-
ConfigDefaultTypesMixin,
16-
TEventHandler,
17-
TExceptionHandler,
18-
TMiddleware,
19-
TVersioning,
20-
)
14+
from .mixins import ConfigDefaultTypesMixin, TExceptionHandler, TMiddleware, TVersioning
2115

2216
if t.TYPE_CHECKING: # pragma: no cover
2317
from ellar.core.main import App
2418

2519

26-
async def _not_found(scope: TScope, receive: TReceive, send: TSend) -> None:
20+
async def _not_found(
21+
scope: TScope, receive: TReceive, send: TSend
22+
) -> None: # pragma: no cover
2723
if scope["type"] == "websocket":
2824
websocket_close = WebSocketClose()
2925
await websocket_close(scope, receive, send)
@@ -113,9 +109,6 @@ class Config:
113109
# logging Level
114110
LOG_LEVEL: t.Optional[log_levels] = log_levels.info
115111

116-
ON_REQUEST_STARTUP: t.List[TEventHandler] = []
117-
ON_REQUEST_SHUTDOWN: t.List[TEventHandler] = []
118-
119112
TEMPLATE_FILTERS: t.Dict[str, t.Callable[..., t.Any]] = {}
120113
TEMPLATE_GLOBAL_FILTERS: t.Dict[str, t.Callable[..., t.Any]] = {}
121114

ellar/core/conf/mixins.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from ellar.core.exceptions.interfaces import IExceptionHandler
99
from ellar.core.middleware import Middleware
1010
from ellar.core.versioning import BaseAPIVersioning
11-
from ellar.events import EventHandler
1211

1312
if t.TYPE_CHECKING: # pragma: no cover
1413
from ellar.core import App
@@ -17,7 +16,6 @@
1716
"ConfigDefaultTypesMixin",
1817
"TVersioning",
1918
"TMiddleware",
20-
"TEventHandler",
2119
"TExceptionHandler",
2220
]
2321

@@ -70,18 +68,18 @@ def validate(cls: t.Type["Middleware"], v: t.Any) -> t.Any:
7068
return v
7169

7270

73-
class TEventHandler(EventHandler):
74-
@classmethod
75-
def __get_validators__(
76-
cls: t.Type["TEventHandler"],
77-
) -> t.Iterable[t.Callable[..., t.Any]]:
78-
yield cls.validate
79-
80-
@classmethod
81-
def validate(cls: t.Type["EventHandler"], v: t.Any) -> t.Any:
82-
if not isinstance(v, EventHandler):
83-
raise ValueError(f"Expected EventHandler, received: {type(v)}")
84-
return v
71+
# class TEventHandler(EventHandler):
72+
# @classmethod
73+
# def __get_validators__(
74+
# cls: t.Type["TEventHandler"],
75+
# ) -> t.Iterable[t.Callable[..., t.Any]]:
76+
# yield cls.validate
77+
#
78+
# @classmethod
79+
# def validate(cls: t.Type["EventHandler"], v: t.Any) -> t.Any:
80+
# if not isinstance(v, EventHandler):
81+
# raise ValueError(f"Expected EventHandler, received: {type(v)}")
82+
# return v
8583

8684

8785
class ConfigDefaultTypesMixin:
@@ -126,12 +124,6 @@ class ConfigDefaultTypesMixin:
126124
# defines other custom json encoders
127125
SERIALIZER_CUSTOM_ENCODER: t.Dict[t.Any, t.Callable[[t.Any], t.Any]]
128126

129-
# will be set automatically when @on_startup is found in a Module class
130-
ON_REQUEST_STARTUP: t.List[TEventHandler]
131-
132-
# will be set automatically when @on_shutdown is found in a Module class
133-
ON_REQUEST_SHUTDOWN: t.List[TEventHandler]
134-
135127
# will be set automatically when @template_filter is found in a Module class
136128
TEMPLATE_FILTERS: t.Dict[str, t.Callable[..., t.Any]]
137129

ellar/core/factory.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,9 @@ def _get_config_kwargs() -> t.Dict:
137137

138138
cls._build_modules(app_module=module, injector=injector, config=config)
139139

140-
shutdown_event = config.ON_REQUEST_STARTUP
141-
startup_event = config.ON_REQUEST_SHUTDOWN
142-
143140
app = App(
144141
config=config,
145142
injector=injector,
146-
on_shutdown_event_handlers=shutdown_event if shutdown_event else None,
147-
on_startup_event_handlers=startup_event if startup_event else None,
148143
lifespan=config.DEFAULT_LIFESPAN_HANDLER,
149144
global_guards=global_guards,
150145
)

ellar/core/main.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from ellar.core.templating import AppTemplating, Environment
3030
from ellar.core.versioning import BaseAPIVersioning, VersioningSchemes
3131
from ellar.di.injector import EllarInjector
32-
from ellar.events import EventHandler, RouterEventManager
3332
from ellar.logger import logger
3433
from ellar.types import ASGIApp, T, TReceive, TScope, TSend
3534

@@ -41,8 +40,6 @@ def __init__(
4140
self,
4241
config: "Config",
4342
injector: EllarInjector,
44-
on_startup_event_handlers: t.Optional[t.Sequence[EventHandler]] = None,
45-
on_shutdown_event_handlers: t.Optional[t.Sequence[EventHandler]] = None,
4643
lifespan: t.Optional[t.Callable[["App"], t.AsyncContextManager]] = None,
4744
global_guards: t.List[
4845
t.Union[t.Type[GuardCanActivate], GuardCanActivate]
@@ -53,12 +50,6 @@ def __init__(
5350
injector, EllarInjector
5451
), "injector must instance of EllarInjector"
5552

56-
# The lifespan context function is a newer style that replaces
57-
# on_startup / on_shutdown handlers. Use one or the other, not both.
58-
assert lifespan is None or (
59-
on_startup_event_handlers is None and on_shutdown_event_handlers is None
60-
), "Use either 'lifespan' or 'on_startup'/'on_shutdown', not both."
61-
6253
self._config = config
6354
self._injector: EllarInjector = injector
6455

@@ -67,15 +58,6 @@ def __init__(
6758

6859
self._user_middleware = list(t.cast(list, self.config.MIDDLEWARE))
6960

70-
self.on_startup = RouterEventManager(
71-
[] if on_startup_event_handlers is None else list(on_startup_event_handlers)
72-
)
73-
self.on_shutdown = RouterEventManager(
74-
[]
75-
if on_shutdown_event_handlers is None
76-
else list(on_shutdown_event_handlers)
77-
)
78-
7961
self._static_app: t.Optional[ASGIApp] = None
8062

8163
self.state = State()
@@ -85,12 +67,6 @@ def __init__(
8567
self.router = ApplicationRouter(
8668
routes=self._get_module_routes(),
8769
redirect_slashes=self.config.REDIRECT_SLASHES,
88-
on_startup=[self.on_startup.async_run]
89-
if self.config.DEFAULT_LIFESPAN_HANDLER is None
90-
else None,
91-
on_shutdown=[self.on_shutdown.async_run]
92-
if self.config.DEFAULT_LIFESPAN_HANDLER is None
93-
else None,
9470
default=self.config.DEFAULT_NOT_FOUND_HANDLER, # type: ignore
9571
lifespan=self.config.DEFAULT_LIFESPAN_HANDLER,
9672
)

ellar/core/modules/builder.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
EXCEPTION_HANDLERS_KEY,
55
MIDDLEWARE_HANDLERS_KEY,
66
MODULE_FIELDS,
7-
ON_REQUEST_SHUTDOWN_KEY,
8-
ON_REQUEST_STARTUP_KEY,
97
TEMPLATE_FILTER_KEY,
108
TEMPLATE_GLOBAL_KEY,
119
)
12-
from ellar.events import EventHandler
1310
from ellar.reflect import reflect
1411

1512
from ..exceptions.callable_exceptions import CallableExceptionHandler
@@ -34,8 +31,6 @@ def __init__(self, cls: t.Union[t.Type["ModuleBase"], "ModuleBaseMeta"]) -> None
3431
{
3532
EXCEPTION_HANDLERS_KEY: self.exception_config,
3633
MIDDLEWARE_HANDLERS_KEY: self.middleware_config,
37-
ON_REQUEST_SHUTDOWN_KEY: self.on_request_shut_down_config,
38-
ON_REQUEST_STARTUP_KEY: self.on_request_startup_config,
3934
TEMPLATE_GLOBAL_KEY: self.template_global_config,
4035
TEMPLATE_FILTER_KEY: self.template_filter_config,
4136
},
@@ -57,26 +52,6 @@ def middleware_config(self, middleware: "MiddlewareSchema") -> None:
5752
self._cls,
5853
)
5954

60-
def on_request_shut_down_config(self, on_shutdown_event: EventHandler) -> None:
61-
on_shutdown_event.handler = module_callable_factory(
62-
on_shutdown_event.handler, self._cls
63-
)
64-
reflect.define_metadata(
65-
ON_REQUEST_SHUTDOWN_KEY,
66-
[on_shutdown_event],
67-
self._cls,
68-
)
69-
70-
def on_request_startup_config(self, on_startup_event: EventHandler) -> None:
71-
on_startup_event.handler = module_callable_factory(
72-
on_startup_event.handler, self._cls
73-
)
74-
reflect.define_metadata(
75-
ON_REQUEST_STARTUP_KEY,
76-
[on_startup_event],
77-
self._cls,
78-
)
79-
8055
def template_filter_config(self, template_filter: "TemplateFunctionData") -> None:
8156
reflect.define_metadata(
8257
TEMPLATE_FILTER_KEY,

0 commit comments

Comments
 (0)