Skip to content

Commit c0424f2

Browse files
committed
perf: Make template existence test (for locale templates) faster
1 parent 624974a commit c0424f2

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎src/mkdocstrings_handlers/python/_internal/handler.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ def update_env(self, config: Any) -> None: # noqa: ARG002
353353
self.env.filters["as_modules_section"] = rendering.do_as_modules_section
354354
self.env.filters["backlink_tree"] = rendering.do_backlink_tree
355355
self.env.globals["AutorefsHook"] = rendering.AutorefsHook
356-
self.env.tests["existing_template"] = lambda template_name: template_name in self.env.list_templates()
356+
self.env.tests["existing_template"] = rendering._test_existing_template # type: ignore[assignment]
357357

358358
def get_aliases(self, identifier: str) -> tuple[str, ...]:
359359
"""Get the aliases for the given identifier.

‎src/mkdocstrings_handlers/python/_internal/rendering.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from collections import defaultdict
1111
from contextlib import suppress
1212
from dataclasses import replace
13-
from functools import lru_cache
13+
from functools import cache, lru_cache
1414
from pathlib import Path
1515
from re import Pattern
1616
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Literal, TypeVar
@@ -32,7 +32,7 @@
3232
Object,
3333
TypeAlias,
3434
)
35-
from jinja2 import pass_context
35+
from jinja2 import Environment, pass_context, pass_environment
3636
from markupsafe import Markup
3737
from mkdocs_autorefs import AutorefsHookInterface, Backlink, BacklinkCrumb
3838
from mkdocstrings import get_logger
@@ -898,3 +898,9 @@ def do_backlink_tree(backlinks: list[Backlink]) -> Tree[BacklinkCrumb]:
898898
A tree of backlinks.
899899
"""
900900
return _compact_tree(_tree(backlink.crumbs for backlink in backlinks))
901+
902+
903+
@cache
904+
@pass_environment
905+
def _test_existing_template(env: Environment, path: str) -> bool:
906+
return path in env.list_templates()

0 commit comments

Comments
 (0)