From 62930fcb892f30343194db06304dae502fd29bd5 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 26 Nov 2024 16:27:44 +0100 Subject: [PATCH] fix mypy typings / use encoders without check because now instances are ensured (#441) - fix mypy complaining because of only instances and use speedier version (it is now ensured that response.encoders are instances) - reorder check for more speed --- esmerald/responses/base.py | 3 +-- esmerald/responses/encoders.py | 3 +-- esmerald/routing/_internal.py | 3 ++- pyproject.toml | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/esmerald/responses/base.py b/esmerald/responses/base.py index 7cdda69d..be61f569 100644 --- a/esmerald/responses/base.py +++ b/esmerald/responses/base.py @@ -1,5 +1,4 @@ from functools import partial -from inspect import isclass from typing import ( TYPE_CHECKING, Any, @@ -190,7 +189,7 @@ def make_response(self, content: Any) -> Union[bytes, str]: encoders = ( ( ( - *(encoder() if isclass(encoder) else encoder for encoder in self.encoders), + *self.encoders, *LILYA_ENCODER_TYPES.get(), ) ) diff --git a/esmerald/responses/encoders.py b/esmerald/responses/encoders.py index ebc14f1f..47bf1f1a 100644 --- a/esmerald/responses/encoders.py +++ b/esmerald/responses/encoders.py @@ -1,5 +1,4 @@ from functools import partial -from inspect import isclass from typing import Any, cast import orjson @@ -24,7 +23,7 @@ def make_response(self, content: Any) -> bytes: encoders = ( ( ( - *(encoder() if isclass(encoder) else encoder for encoder in self.encoders), + *self.encoders, *LILYA_ENCODER_TYPES.get(), ) ) diff --git a/esmerald/routing/_internal.py b/esmerald/routing/_internal.py index d5cfe988..a4e6c07a 100644 --- a/esmerald/routing/_internal.py +++ b/esmerald/routing/_internal.py @@ -62,8 +62,9 @@ def convert_annotation_to_pydantic_model(field_annotation: Any) -> Any: if ( not isinstance(field_annotation, BaseModel) - and any(encoder.is_type(field_annotation) for encoder in LILYA_ENCODER_TYPES.get()) + # call before encoder check, because this test is faster and inspect.isclass(field_annotation) + and any(encoder.is_type(field_annotation) for encoder in LILYA_ENCODER_TYPES.get()) ): field_definitions: Dict[str, Any] = {} diff --git a/pyproject.toml b/pyproject.toml index 774a4640..cc7e3a55 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,7 +41,7 @@ dependencies = [ "email-validator >=2.2.0,<3.0.0", "itsdangerous>=2.1.2,<3.0.0", "jinja2>=3.1.2,<4.0.0", - "lilya>=0.11.1", + "lilya>=0.11.2", "loguru>=0.7.0,<0.8.0", "pydantic>=2.9.1,<3.0.0", "pydantic-settings>=2.0.0,<3.0.0",