Skip to content

Commit

Permalink
ci(typing): make mypy happy-ish
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobCoffee committed Nov 17, 2023
1 parent 8ad618b commit e653101
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
3 changes: 2 additions & 1 deletion litestar/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from litestar.routes import ASGIRoute, HTTPRoute, WebSocketRoute
from litestar.static_files.base import StaticFiles
from litestar.stores.registry import StoreRegistry
from litestar.template import TemplateEngineProtocol
from litestar.types import Empty, TypeDecodersSequence
from litestar.types.internal_types import PathParameterDefinition
from litestar.utils import deprecated, ensure_async_callable, join_paths, unique
Expand Down Expand Up @@ -216,7 +217,7 @@ def __init__(
static_files_config: Sequence[StaticFilesConfig] | None = None,
stores: StoreRegistry | dict[str, Store] | None = None,
tags: Sequence[str] | None = None,
template_config: TemplateConfig | None = None,
template_config: TemplateConfig[TemplateEngineProtocol] | None = None,
type_encoders: TypeEncodersMap | None = None,
type_decoders: TypeDecodersSequence | None = None,
websocket_class: type[WebSocket] | None = None,
Expand Down
3 changes: 2 additions & 1 deletion litestar/config/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from litestar.config.response_cache import ResponseCacheConfig
from litestar.datastructures import State
from litestar.events.emitter import SimpleEventEmitter
from litestar.template import TemplateEngineProtocol
from litestar.types.empty import Empty

if TYPE_CHECKING:
Expand Down Expand Up @@ -196,7 +197,7 @@ class AppConfig:
"""
tags: list[str] = field(default_factory=list)
"""A list of string tags that will be appended to the schema of all route handlers under the application."""
template_config: TemplateConfig | None = field(default=None)
template_config: TemplateConfig[TemplateEngineProtocol] | None = field(default=None)
"""An instance of :class:`TemplateConfig <.template.TemplateConfig>`."""
type_encoders: TypeEncodersMap | None = field(default=None)
"""A mapping of types to callables that transform them into types supported for serialization."""
Expand Down
12 changes: 2 additions & 10 deletions litestar/response/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ def to_asgi_response(
)

def _render_from_string(self, template_str: str, request: Request) -> bytes:
"""Render the template from a string.
Args:
template_str: A string representing the template.
request: A :class:`Request <.connection.Request>` instance.
Returns:
Rendered content as bytes.
"""
"""Render the template from a string."""
context = self.create_template_context(request)
return request.app.template_engine.render_string(template_str, context).encode(self.encoding) # type: ignore[no-any-return]
return request.app.template_engine.render_string(template_str, context).encode(self.encoding)
5 changes: 4 additions & 1 deletion tests/unit/test_template/test_template.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import sys
from pathlib import Path
from typing import TYPE_CHECKING, Optional, Type, Union
Expand Down Expand Up @@ -32,8 +34,9 @@ def invalid_path() -> Template:
def test_engine_passed_to_callback(tmp_path: "Path") -> None:
received_engine: Optional[JinjaTemplateEngine] = None

def callback(engine: JinjaTemplateEngine) -> None:
def callback(engine: TemplateEngineProtocol) -> None:
nonlocal received_engine
assert isinstance(engine, JinjaTemplateEngine), "Engine must be a JinjaTemplateEngine"
received_engine = engine

app = Litestar(
Expand Down

0 comments on commit e653101

Please sign in to comment.