Skip to content

Commit

Permalink
Pydantic V2 - coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalik committed Jun 30, 2023
1 parent 6d2fa90 commit 9c5a9e7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions ninja/orm/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def create_schema(

if custom_fields:
for fld_name, python_type, field_info in custom_fields:
if not isinstance(field_info, FieldInfo):
field_info = Field(field_info)
# if not isinstance(field_info, FieldInfo):
# field_info = Field(field_info)
definitions[fld_name] = (python_type, field_info)

if name in self.schema_names:
Expand Down
4 changes: 3 additions & 1 deletion ninja/orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ def _validate(cls, v: Any, _):


@no_type_check
def get_schema_field(field: DjangoField, *, depth: int = 0, optional: bool = False) -> Tuple:
def get_schema_field(
field: DjangoField, *, depth: int = 0, optional: bool = False
) -> Tuple:
"Returns pydantic field from django's model field"
alias = None
default = ...
Expand Down
37 changes: 19 additions & 18 deletions ninja/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def __init__(self, obj: Any, schema_cls: "Type[Schema]"):
self._schema_cls = schema_cls

def __getattr__(self, key: str) -> Any:
if key.startswith("__pydantic"):
return getattr(self._obj, key)
# if key.startswith("__pydantic"):
# return getattr(self._obj, key)

resolver = self._schema_cls._ninja_resolvers.get(key)
if resolver:
Expand Down Expand Up @@ -118,22 +118,23 @@ def __init__(self, func: Union[Callable, staticmethod]):
def __call__(self, getter: DjangoGetter) -> Any:
if self._static:
return self._func(getter._obj)
return self._func(self._fake_instance(getter), getter._obj)

def _fake_instance(self, getter: DjangoGetter) -> "Schema":
"""
Generate a partial schema instance that can be used as the ``self``
attribute of resolver functions.
"""

class PartialSchema(Schema):
def __getattr__(self, key: str) -> Any:
value = getattr(getter, key)
field = getter._schema_cls.model_fields[key]
value = field.validate(value, values={}, loc=key, cls=None)[0]
return value

return PartialSchema()
assert False, "Non static resolves are not supported yet" # pragma: no cover
# return self._func(self._fake_instance(getter), getter._obj)

# def _fake_instance(self, getter: DjangoGetter) -> "Schema":
# """
# Generate a partial schema instance that can be used as the ``self``
# attribute of resolver functions.
# """

# class PartialSchema(Schema):
# def __getattr__(self, key: str) -> Any:
# value = getattr(getter, key)
# field = getter._schema_cls.model_fields[key]
# value = field.validate(value, values={}, loc=key, cls=None)[0]
# return value

# return PartialSchema()


class ResolverMetaclass(ModelMetaclass):
Expand Down
5 changes: 3 additions & 2 deletions ninja/signature/details.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ def detect_collection_fields(
annotation_or_field, "outer_type_", annotation_or_field
)

if hasattr(annotation_or_field, "annotation"):
annotation_or_field = annotation_or_field.annotation
# if hasattr(annotation_or_field, "annotation"):
annotation_or_field = annotation_or_field.annotation

if is_collection_type(annotation_or_field):
result.append(path[-1])
return result
2 changes: 1 addition & 1 deletion ninja/signature/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# from pydantic.typing import ForwardRef, evaluate_forwardref # type: ignore


if sys.version_info < (3, 9):
if sys.version_info < (3, 9): # pragma: nocover

def evaluate_forwardref(type_: ForwardRef, globalns: Any, localns: Any) -> Any:
return type_._evaluate(globalns, localns)
Expand Down
5 changes: 4 additions & 1 deletion tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ def test_with_boss_schema():
}


@pytest.mark.skipif(True, reason="Lets deal with this later")
SKIP_NON_STATIC_RESOLVES = True


@pytest.mark.skipif(SKIP_NON_STATIC_RESOLVES, reason="Lets deal with this later")
def test_with_initials_schema():
user = User()
schema = UserWithInitialsSchema.from_orm(user)
Expand Down

0 comments on commit 9c5a9e7

Please sign in to comment.