Skip to content

Commit

Permalink
Merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Jul 9, 2023
2 parents 7fcd917 + 4133dfd commit ab2552a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 17 deletions.
7 changes: 6 additions & 1 deletion sanic_ext/extensions/injection/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
from sanic.exceptions import ServerError

from sanic_ext.exceptions import InitError
from sanic_ext.utils.typing import is_attrs, is_optional, is_pydantic, is_msgspec
from sanic_ext.utils.typing import (
is_attrs,
is_msgspec,
is_optional,
is_pydantic,
)

if TYPE_CHECKING:
from .registry import ConstantRegistry, InjectionRegistry
Expand Down
22 changes: 16 additions & 6 deletions sanic_ext/extensions/openapi/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

from sanic_routing.patterns import alpha, ext, nonemptystr, parse_date, slug

from sanic_ext.utils.typing import is_attrs, is_generic, is_pydantic, is_msgspec
from sanic_ext.utils.typing import (
UnionType,
is_attrs,
is_generic,
is_msgspec,
is_pydantic,
)

try:
import attrs
Expand All @@ -28,7 +34,8 @@

try:
import msgspec
from msgspec.inspect import type_info as msgspec_type_info, Metadata as MsgspecMetadata
from msgspec.inspect import Metadata as MsgspecMetadata
from msgspec.inspect import type_info as msgspec_type_info

MsgspecMetadata: Any = MsgspecMetadata
NODEFAULT: Any = msgspec.NODEFAULT
Expand All @@ -40,13 +47,14 @@ class MsgspecAdapter(msgspec.Struct):
metadata: dict

except ImportError:

def msgspec_type_info(struct):
pass

class MsgspecAdapter:
pass

MsgspecMetadata = object()
MsgspecMetadata = object()
NODEFAULT = object()
UNSET = object()

Expand Down Expand Up @@ -124,7 +132,7 @@ def make(value, **kwargs):
_type = type(value)
origin = get_origin(value)
args = get_args(value)
if origin is Union:
if origin in (Union, UnionType):
if type(None) in args:
kwargs["nullable"] = True

Expand Down Expand Up @@ -332,8 +340,10 @@ def make(cls, value: Any, **kwargs):
fields = [
MsgspecAdapter(
name=f.name,
default=MISSING if f.default in (UNSET, NODEFAULT) else f.default,
metadata=getattr(f.type, 'extra', {})
default=MISSING
if f.default in (UNSET, NODEFAULT)
else f.default,
metadata=getattr(f.type, "extra", {}),
)
for f in msgspec_type_info(value).fields
]
Expand Down
12 changes: 10 additions & 2 deletions sanic_ext/extras/validation/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
get_origin,
)

from sanic_ext.utils.typing import UnionType, is_generic, is_optional, is_msgspec
from sanic_ext.utils.typing import (
UnionType,
is_generic,
is_msgspec,
is_optional,
)

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

Expand All @@ -32,6 +37,7 @@

try:
import msgspec

MSGSPEC = True
except ImportError:
MSGSPEC = False
Expand Down Expand Up @@ -179,7 +185,9 @@ def check_data(model, data, schema, allow_multiple=False, allow_coerce=False):

if MSGSPEC and is_msgspec(model):
try:
return msgspec.from_builtins(hydration_values, model, str_values=True, str_keys=True)
return msgspec.from_builtins(
hydration_values, model, str_values=True, str_keys=True
)
except msgspec.ValidationError as e:
raise TypeError(e)
else:
Expand Down
5 changes: 4 additions & 1 deletion sanic_ext/extras/validation/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
try:
from msgspec.inspect import type_info as msgspec_type_info
except ModuleNotFoundError:

def msgspec_type_info(val):
pass

Expand All @@ -42,7 +43,9 @@ def make_schema(agg, item):
if is_generic(item) and (args := get_args(item)):
for arg in args:
make_schema(agg, arg)
elif item.__name__ not in agg and (is_dataclass(item) or is_attrs(item) or is_msgspec(item)):
elif item.__name__ not in agg and (
is_dataclass(item) or is_attrs(item) or is_msgspec(item)
):
if is_dataclass(item):
fields = item.__dataclass_fields__
elif is_msgspec(item):
Expand Down
1 change: 1 addition & 0 deletions sanic_ext/utils/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def is_pydantic(model):
issubclass(model, BaseModel) or hasattr(model, "__pydantic_model__")
)


def is_attrs(model):
return ATTRS and (hasattr(model, "__attrs_attrs__"))

Expand Down
15 changes: 11 additions & 4 deletions tests/extensions/openapi/test_model_fields.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from dataclasses import dataclass, field
from typing import List, Annotated
from typing import Annotated, List
from uuid import UUID

import attrs
import pytest
from msgspec import Struct, Meta
from msgspec import Meta, Struct
from pydantic import BaseModel, Field
from pydantic.dataclasses import dataclass as pydataclass

Expand Down Expand Up @@ -50,8 +50,15 @@ class FooPydanticDataclass:

class FooStruct(Struct):
links: List[UUID]
priority: Annotated[int, Meta(extra={"openapi": {"exclusiveMinimum": 1, "exclusiveMaximum": 10}})]
ident: Annotated[str, Meta(extra={"openapi": {"example": "ABC123"}})] = "XXXX"
priority: Annotated[
int,
Meta(
extra={"openapi": {"exclusiveMinimum": 1, "exclusiveMaximum": 10}}
),
]
ident: Annotated[
str, Meta(extra={"openapi": {"example": "ABC123"}})
] = "XXXX"


@pytest.mark.parametrize(
Expand Down
2 changes: 2 additions & 0 deletions tests/extensions/openapi/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def test_dict_values_nested():
]
if sys.version_info >= (3, 9):
params.append(({"foo": list[str]}, True))
if sys.version_info >= (3, 10):
params.append(({"foo": str | None}, True))


@pytest.mark.parametrize("item,expected", params)
Expand Down
3 changes: 1 addition & 2 deletions tests/extra/test_validation_msgspec.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import sys

from msgspec import Struct
from typing import List, Optional

import pytest
from msgspec import Struct
from sanic import json
from sanic.views import HTTPMethodView

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ python =
extras = test
deps =
git+https://github.com/sanic-org/sanic.git#egg=sanic
pydantic
pydantic<2.0.0
attrs
msgspec
; sanic21.6: sanic==21.6
Expand Down

0 comments on commit ab2552a

Please sign in to comment.