Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix late dependency registration #228

Merged
merged 4 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sanic_ext/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from sanic.signals import Event


PRIORITY = 100_000_000


class Config(SanicConfig):
def __init__(
self,
Expand Down Expand Up @@ -37,6 +40,7 @@ def __init__(
http_auto_options: bool = True,
http_auto_trace: bool = False,
injection_signal: Union[str, Event] = Event.HTTP_ROUTING_AFTER,
injection_priority: int = PRIORITY,
injection_load_custom_constants: bool = False,
logging: bool = False,
logging_queue_max_size: int = 4096,
Expand Down Expand Up @@ -94,6 +98,7 @@ def __init__(
self.HTTP_AUTO_OPTIONS = http_auto_options
self.HTTP_AUTO_TRACE = http_auto_trace
self.INJECTION_SIGNAL = injection_signal
self.INJECTION_PRIORITY = injection_priority
self.INJECTION_LOAD_CUSTOM_CONSTANTS = injection_load_custom_constants
self.LOGGING = logging
self.LOGGING_QUEUE_MAX_SIZE = logging_queue_max_size
Expand Down
7 changes: 4 additions & 3 deletions sanic_ext/extensions/http/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from sanic.exceptions import SanicException
from sanic.response import empty, raw

from ...utils.route import clean_route_name
from ..openapi import openapi
from sanic_ext.config import PRIORITY
from sanic_ext.extensions.openapi import openapi
from sanic_ext.utils.route import clean_route_name


def add_http_methods(
Expand Down Expand Up @@ -66,7 +67,7 @@ async def trace_handler(request):
)
return raw(message, content_type="message/http")

@app.before_server_start
@app.before_server_start(priority=PRIORITY + 1)
def _add_handlers(app, _):
nonlocal auto_head
nonlocal auto_options
Expand Down
8 changes: 5 additions & 3 deletions sanic_ext/extensions/injection/injector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sanic import Sanic
from sanic.constants import HTTP_METHODS

from sanic_ext.config import PRIORITY
from sanic_ext.extensions.injection.constructor import gather_args

from .registry import ConstantRegistry, InjectionRegistry, SignatureRegistry
Expand All @@ -21,7 +22,7 @@ def add_injection(
app, injection_registry, constant_registry
)

@app.after_server_start
@app.listener("before_server_start", priority=PRIORITY)
async def finalize_injections(app: Sanic, _):
router_converters = set(
allowed[0] for allowed in app.router.regex_types.values()
Expand All @@ -37,8 +38,9 @@ async def finalize_injections(app: Sanic, _):
injection_registry.finalize(app, constant_registry, router_types)

injection_signal = app.ext.config.INJECTION_SIGNAL
injection_priority = app.ext.config.INJECTION_PRIORITY

@app.signal(injection_signal)
@app.signal(injection_signal, priority=injection_priority)
async def inject_kwargs(request, **_):
nonlocal signature_registry

Expand Down Expand Up @@ -72,7 +74,7 @@ def _setup_signature_registry(
) -> SignatureRegistry:
registry = SignatureRegistry()

@app.after_server_start
@app.listener("before_server_start", priority=PRIORITY)
async def setup_signatures(app, _):
nonlocal registry

Expand Down
3 changes: 2 additions & 1 deletion tests/extensions/injection/test_injection_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_add_injection_uses_signal_config():

app.signal = Mock(return_value=Mock())
app.ext.config.INJECTION_SIGNAL = "random_string"
app.ext.config.INJECTION_PRIORITY = 99999
add_injection(app, Mock(), Mock())

app.signal.assert_called_once_with("random_string")
app.signal.assert_called_once_with("random_string", priority=99999)
Loading