Skip to content

Commit 0ee5eb4

Browse files
committed
satisfy formatter, linter, and strict mypy
1 parent 20477c6 commit 0ee5eb4

21 files changed

+151
-113
lines changed

src/jinja2/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
non-XML syntax that supports inline expressions and an optional
33
sandboxed environment.
44
"""
5+
56
from .bccache import BytecodeCache as BytecodeCache
67
from .bccache import FileSystemBytecodeCache as FileSystemBytecodeCache
78
from .bccache import MemcachedBytecodeCache as MemcachedBytecodeCache

src/jinja2/async_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def wrapper(*args, **kwargs): # type: ignore
4747
if need_eval_context:
4848
wrapper = pass_eval_context(wrapper)
4949

50-
wrapper.jinja_async_variant = True
50+
wrapper.jinja_async_variant = True # type: ignore[attr-defined]
5151
return wrapper
5252

5353
return decorator

src/jinja2/bccache.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Situations where this is useful are often forking web applications that
66
are initialized on the first request.
77
"""
8+
89
import errno
910
import fnmatch
1011
import marshal
@@ -20,14 +21,15 @@
2021

2122
if t.TYPE_CHECKING:
2223
import typing_extensions as te
24+
2325
from .environment import Environment
2426

2527
class _MemcachedClient(te.Protocol):
26-
def get(self, key: str) -> bytes:
27-
...
28+
def get(self, key: str) -> bytes: ...
2829

29-
def set(self, key: str, value: bytes, timeout: t.Optional[int] = None) -> None:
30-
...
30+
def set(
31+
self, key: str, value: bytes, timeout: t.Optional[int] = None
32+
) -> None: ...
3133

3234

3335
bc_version = 5

src/jinja2/compiler.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Compiles nodes from the parser into Python code."""
2+
23
import typing as t
34
from contextlib import contextmanager
45
from functools import update_wrapper
@@ -24,6 +25,7 @@
2425

2526
if t.TYPE_CHECKING:
2627
import typing_extensions as te
28+
2729
from .environment import Environment
2830

2931
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
@@ -60,8 +62,7 @@ def _make_binop(op: str) -> t.Callable[["CodeGenerator", nodes.BinExpr, "Frame"]
6062
@optimizeconst
6163
def visitor(self: "CodeGenerator", node: nodes.BinExpr, frame: Frame) -> None:
6264
if (
63-
self.environment.sandboxed
64-
and op in self.environment.intercepted_binops # type: ignore
65+
self.environment.sandboxed and op in self.environment.intercepted_binops # type: ignore
6566
):
6667
self.write(f"environment.call_binop(context, {op!r}, ")
6768
self.visit(node.left, frame)
@@ -84,8 +85,7 @@ def _make_unop(
8485
@optimizeconst
8586
def visitor(self: "CodeGenerator", node: nodes.UnaryExpr, frame: Frame) -> None:
8687
if (
87-
self.environment.sandboxed
88-
and op in self.environment.intercepted_unops # type: ignore
88+
self.environment.sandboxed and op in self.environment.intercepted_unops # type: ignore
8989
):
9090
self.write(f"environment.call_unop(context, {op!r}, ")
9191
self.visit(node.node, frame)
@@ -133,7 +133,7 @@ def has_safe_repr(value: t.Any) -> bool:
133133
if type(value) in {tuple, list, set, frozenset}:
134134
return all(has_safe_repr(v) for v in value)
135135

136-
if type(value) is dict:
136+
if type(value) is dict: # noqa E721
137137
return all(has_safe_repr(k) and has_safe_repr(v) for k, v in value.items())
138138

139139
return False
@@ -551,10 +551,13 @@ def pull_dependencies(self, nodes: t.Iterable[nodes.Node]) -> None:
551551
for node in nodes:
552552
visitor.visit(node)
553553

554-
for id_map, names, dependency in (self.filters, visitor.filters, "filters"), (
555-
self.tests,
556-
visitor.tests,
557-
"tests",
554+
for id_map, names, dependency in (
555+
(self.filters, visitor.filters, "filters"),
556+
(
557+
self.tests,
558+
visitor.tests,
559+
"tests",
560+
),
558561
):
559562
for name in sorted(names):
560563
if name not in id_map:
@@ -829,7 +832,8 @@ def visit_Template(
829832
assert frame is None, "no root frame allowed"
830833
eval_ctx = EvalContext(self.environment, self.name)
831834

832-
from .runtime import exported, async_exported
835+
from .runtime import async_exported
836+
from .runtime import exported
833837

834838
if self.environment.is_async:
835839
exported_names = sorted(exported + async_exported)

src/jinja2/environment.py

+28-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Classes for managing templates and their runtime and compile time
22
options.
33
"""
4+
45
import os
56
import typing
67
import typing as t
@@ -20,10 +21,10 @@
2021
from .defaults import BLOCK_START_STRING
2122
from .defaults import COMMENT_END_STRING
2223
from .defaults import COMMENT_START_STRING
23-
from .defaults import DEFAULT_FILTERS
24+
from .defaults import DEFAULT_FILTERS # type: ignore[attr-defined]
2425
from .defaults import DEFAULT_NAMESPACE
2526
from .defaults import DEFAULT_POLICIES
26-
from .defaults import DEFAULT_TESTS
27+
from .defaults import DEFAULT_TESTS # type: ignore[attr-defined]
2728
from .defaults import KEEP_TRAILING_NEWLINE
2829
from .defaults import LINE_COMMENT_PREFIX
2930
from .defaults import LINE_STATEMENT_PREFIX
@@ -55,6 +56,7 @@
5556

5657
if t.TYPE_CHECKING:
5758
import typing_extensions as te
59+
5860
from .bccache import BytecodeCache
5961
from .ext import Extension
6062
from .loaders import BaseLoader
@@ -79,7 +81,7 @@ def get_spontaneous_environment(cls: t.Type[_env_bound], *args: t.Any) -> _env_b
7981

8082
def create_cache(
8183
size: int,
82-
) -> t.Optional[t.MutableMapping[t.Tuple[weakref.ref, str], "Template"]]:
84+
) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[t.Any]", str], "Template"]]:
8385
"""Return the cache class for the given size."""
8486
if size == 0:
8587
return None
@@ -91,13 +93,13 @@ def create_cache(
9193

9294

9395
def copy_cache(
94-
cache: t.Optional[t.MutableMapping],
95-
) -> t.Optional[t.MutableMapping[t.Tuple[weakref.ref, str], "Template"]]:
96+
cache: t.Optional[t.MutableMapping[t.Any, t.Any]],
97+
) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[t.Any]", str], "Template"]]:
9698
"""Create an empty copy of the given cache."""
9799
if cache is None:
98100
return None
99101

100-
if type(cache) is dict:
102+
if type(cache) is dict: # noqa E721
101103
return {}
102104

103105
return LRUCache(cache.capacity) # type: ignore
@@ -670,7 +672,7 @@ def _tokenize(
670672
stream = ext.filter_stream(stream) # type: ignore
671673

672674
if not isinstance(stream, TokenStream):
673-
stream = TokenStream(stream, name, filename) # type: ignore
675+
stream = TokenStream(stream, name, filename)
674676

675677
return stream
676678

@@ -711,8 +713,7 @@ def compile( # type: ignore
711713
filename: t.Optional[str] = None,
712714
raw: "te.Literal[False]" = False,
713715
defer_init: bool = False,
714-
) -> CodeType:
715-
...
716+
) -> CodeType: ...
716717

717718
@typing.overload
718719
def compile(
@@ -722,8 +723,7 @@ def compile(
722723
filename: t.Optional[str] = None,
723724
raw: "te.Literal[True]" = ...,
724725
defer_init: bool = False,
725-
) -> str:
726-
...
726+
) -> str: ...
727727

728728
@internalcode
729729
def compile(
@@ -814,7 +814,7 @@ def compile_expression(
814814

815815
def compile_templates(
816816
self,
817-
target: t.Union[str, os.PathLike],
817+
target: t.Union[str, "os.PathLike[str]"],
818818
extensions: t.Optional[t.Collection[str]] = None,
819819
filter_func: t.Optional[t.Callable[[str], bool]] = None,
820820
zip: t.Optional[str] = "deflated",
@@ -858,7 +858,10 @@ def write_file(filename: str, data: str) -> None:
858858
f.write(data.encode("utf8"))
859859

860860
if zip is not None:
861-
from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED, ZIP_STORED
861+
from zipfile import ZIP_DEFLATED
862+
from zipfile import ZIP_STORED
863+
from zipfile import ZipFile
864+
from zipfile import ZipInfo
862865

863866
zip_file = ZipFile(
864867
target, "w", dict(deflated=ZIP_DEFLATED, stored=ZIP_STORED)[zip]
@@ -1417,7 +1420,9 @@ async def make_module_async(
14171420
"""
14181421
ctx = self.new_context(vars, shared, locals)
14191422
return TemplateModule(
1420-
self, ctx, [x async for x in self.root_render_func(ctx)] # type: ignore
1423+
self,
1424+
ctx,
1425+
[x async for x in self.root_render_func(ctx)], # type: ignore
14211426
)
14221427

14231428
@internalcode
@@ -1588,7 +1593,7 @@ def __init__(self, gen: t.Iterator[str]) -> None:
15881593

15891594
def dump(
15901595
self,
1591-
fp: t.Union[str, t.IO],
1596+
fp: t.Union[str, t.IO[bytes]],
15921597
encoding: t.Optional[str] = None,
15931598
errors: t.Optional[str] = "strict",
15941599
) -> None:
@@ -1606,22 +1611,25 @@ def dump(
16061611
if encoding is None:
16071612
encoding = "utf-8"
16081613

1609-
fp = open(fp, "wb")
1614+
real_fp: t.IO[bytes] = open(fp, "wb")
16101615
close = True
1616+
else:
1617+
real_fp = fp
1618+
16111619
try:
16121620
if encoding is not None:
16131621
iterable = (x.encode(encoding, errors) for x in self) # type: ignore
16141622
else:
16151623
iterable = self # type: ignore
16161624

1617-
if hasattr(fp, "writelines"):
1618-
fp.writelines(iterable)
1625+
if hasattr(real_fp, "writelines"):
1626+
real_fp.writelines(iterable)
16191627
else:
16201628
for item in iterable:
1621-
fp.write(item)
1629+
real_fp.write(item)
16221630
finally:
16231631
if close:
1624-
fp.close()
1632+
real_fp.close()
16251633

16261634
def disable_buffering(self) -> None:
16271635
"""Disable the output buffering."""

src/jinja2/ext.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Extension API for adding custom tags and behavior."""
2+
23
import pprint
34
import re
45
import typing as t
@@ -18,23 +19,23 @@
1819

1920
if t.TYPE_CHECKING:
2021
import typing_extensions as te
22+
2123
from .lexer import Token
2224
from .lexer import TokenStream
2325
from .parser import Parser
2426

2527
class _TranslationsBasic(te.Protocol):
26-
def gettext(self, message: str) -> str:
27-
...
28+
def gettext(self, message: str) -> str: ...
2829

2930
def ngettext(self, singular: str, plural: str, n: int) -> str:
3031
pass
3132

3233
class _TranslationsContext(_TranslationsBasic):
33-
def pgettext(self, context: str, message: str) -> str:
34-
...
34+
def pgettext(self, context: str, message: str) -> str: ...
3535

36-
def npgettext(self, context: str, singular: str, plural: str, n: int) -> str:
37-
...
36+
def npgettext(
37+
self, context: str, singular: str, plural: str, n: int
38+
) -> str: ...
3839

3940
_SupportedTranslations = t.Union[_TranslationsBasic, _TranslationsContext]
4041

@@ -218,7 +219,7 @@ def pgettext(
218219

219220

220221
def _make_new_npgettext(
221-
func: t.Callable[[str, str, str, int], str]
222+
func: t.Callable[[str, str, str, int], str],
222223
) -> t.Callable[..., str]:
223224
@pass_context
224225
def npgettext(
@@ -294,14 +295,14 @@ def _install_null(self, newstyle: t.Optional[bool] = None) -> None:
294295
pgettext = translations.pgettext
295296
else:
296297

297-
def pgettext(c: str, s: str) -> str:
298+
def pgettext(c: str, s: str) -> str: # type: ignore[misc]
298299
return s
299300

300301
if hasattr(translations, "npgettext"):
301302
npgettext = translations.npgettext
302303
else:
303304

304-
def npgettext(c: str, s: str, p: str, n: int) -> str:
305+
def npgettext(c: str, s: str, p: str, n: int) -> str: # type: ignore[misc]
305306
return s if n == 1 else p
306307

307308
self._install_callables(

0 commit comments

Comments
 (0)