Skip to content

Commit

Permalink
lint mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
nofalx committed Sep 9, 2023
1 parent 5474d2c commit 5bd7ba4
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions ninja/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ def resolve_initials(self, obj):
"""
import warnings
from typing import Any, Callable, Dict, Type, TypeVar, Union, no_type_check
from typing import Any, Callable, Dict, TypeVar, Union, no_type_check

import pydantic
from django.db.models import Manager, QuerySet
from django.db.models.fields.files import FieldFile
from django.template import Variable, VariableDoesNotExist
from pydantic import BaseModel, Field, ValidationInfo, model_validator, validator
from pydantic._internal._model_construction import ModelMetaclass
from pydantic.functional_validators import ModelWrapValidatorHandler
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue

from ninja.signature.utils import get_args_names, has_kwargs
Expand All @@ -45,7 +46,7 @@ def resolve_initials(self, obj):
class DjangoGetter:
__slots__ = ("_obj", "_schema_cls", "_context")

def __init__(self, obj: Any, schema_cls: "Schema", context: Any = None):
def __init__(self, obj: Any, schema_cls: type[S], context: Any = None):
self._obj = obj
self._schema_cls = schema_cls
self._context = context
Expand Down Expand Up @@ -199,9 +200,10 @@ class Config:
from_attributes = True # aka orm_mode

@model_validator(mode="wrap")
@classmethod
def _run_root_validator(
cls, values: Any, handler: Callable, info: ValidationInfo
) -> Any:
cls, values: Any, handler: ModelWrapValidatorHandler[S], info: ValidationInfo
) -> S:
# We dont perform 'before' validations if an validating through 'model_validate'
through_model_validate = (
info and info.context and info.context.get("through_model_validate", False)
Expand All @@ -216,22 +218,22 @@ def _run_root_validator(
return handler(values)

@classmethod
def from_orm(cls: Type[S], obj: Any) -> S:
def from_orm(cls: type[S], obj: Any) -> S:
return cls.model_validate(obj)

@classmethod
def model_validate(
cls: type[BaseModel],
cls: type[S],
obj: Any,
*args,
*,
strict: bool | None = None,
from_attributes: bool | None = None,
context: dict[str, Any] | None = None,
) -> BaseModel:
) -> S:
context = context or {}
context["through_model_validate"] = True
return super().model_validate(
obj, *args, strict=strict, from_attributes=from_attributes, context=context
obj, strict=strict, from_attributes=from_attributes, context=context
)

def dict(self, *a: Any, **kw: Any) -> DictStrAny:
Expand Down

0 comments on commit 5bd7ba4

Please sign in to comment.