From 5bd7ba4794884a77c91a4ddcf6d459747622eb40 Mon Sep 17 00:00:00 2001 From: Ahmad Nofal Date: Sat, 9 Sep 2023 17:59:16 +0400 Subject: [PATCH] lint mypy --- ninja/schema.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/ninja/schema.py b/ninja/schema.py index 83267614b..ab9c7423b 100644 --- a/ninja/schema.py +++ b/ninja/schema.py @@ -21,7 +21,7 @@ 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 @@ -29,6 +29,7 @@ def resolve_initials(self, obj): 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 @@ -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 @@ -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) @@ -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: