Skip to content

Commit

Permalink
Merge branch 'main' into issue-214
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Dec 12, 2023
2 parents 5d28f12 + 4b7af45 commit 309da87
Show file tree
Hide file tree
Showing 30 changed files with 103 additions and 38 deletions.
23 changes: 20 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
pretty:
black sanic_ext tests
isort sanic_ext tests
PYTEST = pytest -rs sanic_ext tests -vv --cache-clear --flake8

.PHONY: test
test:
${PYTEST}

.PHONY: test-cov
test-cov:
${PYTEST} --cov sanic_ext

.PHONY: fix
fix:
ruff check sanic_ext --fix

.PHONY: format
format:
ruff format sanic_ext

.PHONY: pretty
pretty: fix format
37 changes: 28 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,32 @@
requires = ["setuptools<60.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.black]
[tool.ruff]
target-version = "py38"
line-length = 79
target-version = ['py38', 'py39', 'py310']

[tool.isort]
profile = "black"
src_paths = ["sanic_ext", "tests"]
line_length = 79
multi_line_output = 3
include_trailing_comma = true

[tool.ruff.lint]
select = [
"E", # pycodestyle
"F", # pyflakes
"I", # isort
"W", # pycodestyle warnings
]
ignore = [
"E203",
]

[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

[tool.ruff.isort]
known-first-party = ["sanic_ext"]
known-third-party = ["pytest"]
lines-after-imports = 2
lines-between-types = 1
1 change: 1 addition & 0 deletions sanic_ext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sanic_ext.extras.serializer.decorator import serializer
from sanic_ext.extras.validation.decorator import validate


__version__ = version("sanic-ext")

__all__ = [
Expand Down
1 change: 1 addition & 0 deletions sanic_ext/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from sanic_ext.utils.string import camel_to_snake
from sanic_ext.utils.version import get_version


try:
from jinja2 import Environment

Expand Down
6 changes: 6 additions & 0 deletions sanic_ext/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os

from typing import Any, Dict, List, Optional, Sequence, Union

from sanic import Sanic
Expand All @@ -9,6 +10,9 @@
from sanic.signals import Event


PRIORITY = 100_000_000


class Config(SanicConfig):
def __init__(
self,
Expand Down Expand Up @@ -36,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 @@ -93,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
1 change: 1 addition & 0 deletions sanic_ext/extensions/health/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from sanic.application.constants import ServerStage
from sanic.log import logger


if TYPE_CHECKING:
from sanic import Sanic

Expand Down
4 changes: 3 additions & 1 deletion sanic_ext/extensions/http/cors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re

from dataclasses import dataclass
from datetime import timedelta
from types import SimpleNamespace
Expand All @@ -9,6 +10,7 @@
from sanic.helpers import Default, _default
from sanic.log import logger


WILDCARD_PATTERN = re.compile(r".*")
ORIGIN_HEADER = "access-control-allow-origin"
ALLOW_HEADERS_HEADER = "access-control-allow-headers"
Expand Down Expand Up @@ -299,7 +301,7 @@ def _get_allow_origins(app: Sanic) -> Tuple[re.Pattern, ...]:


def _parse_allow_origins(
value: Union[str, re.Pattern, List[Union[str, re.Pattern]]]
value: Union[str, re.Pattern, List[Union[str, re.Pattern]]],
) -> Tuple[re.Pattern, ...]:
origins: Optional[
Union[List[str], List[re.Pattern], List[Union[str, re.Pattern]]]
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
1 change: 1 addition & 0 deletions sanic_ext/extensions/injection/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
is_pydantic,
)


if TYPE_CHECKING:
from .registry import ConstantRegistry, InjectionRegistry

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
1 change: 1 addition & 0 deletions sanic_ext/extensions/logging/logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging

from collections import defaultdict
from logging import LogRecord
from logging.handlers import QueueHandler
Expand Down
1 change: 1 addition & 0 deletions sanic_ext/extensions/openapi/blueprint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect

from functools import lru_cache, partial
from os.path import abspath, dirname, realpath

Expand Down
7 changes: 1 addition & 6 deletions sanic_ext/extensions/openapi/builders.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
from __future__ import annotations

"""
Builders for the oas3 object types
These are completely internal, so can be refactored if desired without concern
for breaking user experience
"""
from collections import defaultdict
from typing import TYPE_CHECKING, Optional, Sequence, Union, cast

Expand Down Expand Up @@ -39,6 +33,7 @@
Tag,
)


if TYPE_CHECKING:
from sanic import Sanic

Expand Down
1 change: 1 addition & 0 deletions sanic_ext/extensions/openapi/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ..base import Extension
from .blueprint import blueprint_factory


if TYPE_CHECKING:
from sanic_ext import Extend

Expand Down
1 change: 1 addition & 0 deletions sanic_ext/extensions/openapi/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from sanic_ext.extras.validation.setup import do_validation, generate_schema
from sanic_ext.utils.extraction import extract_request


__all__ = (
"definitions",
"body",
Expand Down
3 changes: 3 additions & 0 deletions sanic_ext/extensions/openapi/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import uuid

from dataclasses import MISSING, is_dataclass
from datetime import date, datetime, time
from enum import Enum
Expand All @@ -25,6 +26,7 @@
is_pydantic,
)


try:
import attrs

Expand All @@ -34,6 +36,7 @@

try:
import msgspec

from msgspec.inspect import Metadata as MsgspecMetadata
from msgspec.inspect import type_info as msgspec_type_info

Expand Down
1 change: 1 addition & 0 deletions sanic_ext/extensions/templating/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
TemplateResponse,
)


if TYPE_CHECKING:
from sanic_ext import Config

Expand Down
2 changes: 2 additions & 0 deletions sanic_ext/extensions/templating/extension.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os

from collections import abc
from pathlib import Path
from typing import TYPE_CHECKING, Sequence, Union
Expand All @@ -16,6 +17,7 @@

from ..base import Extension


if TYPE_CHECKING:
from sanic_ext import Extend

Expand Down
1 change: 1 addition & 0 deletions sanic_ext/extensions/templating/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from sanic_ext.exceptions import ExtensionNotFound


if TYPE_CHECKING:
from jinja2 import Environment

Expand Down
1 change: 1 addition & 0 deletions sanic_ext/extras/serializer/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from sanic import response


T = TypeVar("T")


Expand Down
1 change: 1 addition & 0 deletions sanic_ext/extras/validation/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
is_optional,
)


MISSING: Tuple[Any, ...] = (_HAS_DEFAULT_FACTORY,)

try:
Expand Down
1 change: 1 addition & 0 deletions sanic_ext/extras/validation/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .setup import do_validation, generate_schema


T = TypeVar("T")


Expand Down
5 changes: 3 additions & 2 deletions sanic_ext/extras/validation/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import types

from dataclasses import MISSING, Field, is_dataclass
from inspect import isclass, signature
from typing import (
Expand All @@ -17,6 +18,7 @@

from .check import Hint


try:
UnionType = types.UnionType # type: ignore
except AttributeError:
Expand Down Expand Up @@ -88,8 +90,7 @@ def parse_hint(hint, field: Optional[Union[Field, Attribute]] = None):

if field and (
(
isinstance(field, Field)
and field.default_factory is not MISSING # type: ignore
isinstance(field, Field) and field.default_factory is not MISSING # type: ignore
)
or (isinstance(field, Attribute) and field.default is not NOTHING)
):
Expand Down
1 change: 1 addition & 0 deletions sanic_ext/extras/validation/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .check import check_data
from .clean import clean_data


try:
from pydantic import ValidationError as PydanticValidationError

Expand Down
2 changes: 1 addition & 1 deletion sanic_ext/utils/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def remove_nulls(dictionary, deep=True):
Removes all null values from a dictionary.
"""
return {
k: remove_nulls(v, deep) if deep and type(v) is dict else v
k: remove_nulls(v, deep) if deep and isinstance(v, dict) else v
for k, v in dictionary.items()
if v is not None
}
Expand Down
1 change: 1 addition & 0 deletions sanic_ext/utils/string.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re


CAMEL_TO_SNAKE_PATTERNS = (
re.compile(r"(.)([A-Z][a-z]+)"),
re.compile(r"([a-z0-9])([A-Z])"),
Expand Down
Loading

0 comments on commit 309da87

Please sign in to comment.