]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
fix mypy strict findings 1793/head
authorDavid Lord <davidism@gmail.com>
Fri, 20 Jan 2023 01:49:35 +0000 (17:49 -0800)
committerDavid Lord <davidism@gmail.com>
Fri, 20 Jan 2023 02:17:03 +0000 (18:17 -0800)
src/jinja2/async_utils.py
src/jinja2/environment.py
src/jinja2/filters.py
src/jinja2/lexer.py
src/jinja2/loaders.py
src/jinja2/nodes.py
src/jinja2/parser.py
src/jinja2/runtime.py
src/jinja2/sandbox.py
src/jinja2/tests.py
src/jinja2/utils.py

index 715d70119bba3f02350827da184cedbd08f65185..e65219e497b0f101fa552752d7b56dc364357e4c 100644 (file)
@@ -47,7 +47,7 @@ def async_variant(normal_func):  # type: ignore
         if need_eval_context:
             wrapper = pass_eval_context(wrapper)
 
-        wrapper.jinja_async_variant = True
+        wrapper.jinja_async_variant = True  # type: ignore[attr-defined]
         return wrapper
 
     return decorator
index 88b26662ff2e242776d1a2860acbdb54790f6710..29a1b4e409dadca47caaaeb15315949eb04cbe04 100644 (file)
@@ -79,7 +79,7 @@ def get_spontaneous_environment(cls: t.Type[_env_bound], *args: t.Any) -> _env_b
 
 def create_cache(
     size: int,
-) -> t.Optional[t.MutableMapping[t.Tuple[weakref.ref, str], "Template"]]:
+) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[BaseLoader]", str], "Template"]]:
     """Return the cache class for the given size."""
     if size == 0:
         return None
@@ -91,8 +91,10 @@ def create_cache(
 
 
 def copy_cache(
-    cache: t.Optional[t.MutableMapping],
-) -> t.Optional[t.MutableMapping[t.Tuple[weakref.ref, str], "Template"]]:
+    cache: t.Optional[
+        t.MutableMapping[t.Tuple["weakref.ref[BaseLoader]", str], "Template"]
+    ],
+) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[BaseLoader]", str], "Template"]]:
     """Create an empty copy of the given cache."""
     if cache is None:
         return None
@@ -814,7 +816,7 @@ class Environment:
 
     def compile_templates(
         self,
-        target: t.Union[str, os.PathLike],
+        target: t.Union[str, "os.PathLike[str]"],
         extensions: t.Optional[t.Collection[str]] = None,
         filter_func: t.Optional[t.Callable[[str], bool]] = None,
         zip: t.Optional[str] = "deflated",
@@ -1588,7 +1590,7 @@ class TemplateStream:
 
     def dump(
         self,
-        fp: t.Union[str, t.IO],
+        fp: t.Union[str, t.IO[t.Any]],
         encoding: t.Optional[str] = None,
         errors: t.Optional[str] = "strict",
     ) -> None:
index ed07c4c0e2ae1b6203b3468cda8a303ecf3d7832..f4479ff3db41204d33a3795fe1cd2b5b52aeb7a3 100644 (file)
@@ -538,7 +538,7 @@ def do_default(
 @pass_eval_context
 def sync_do_join(
     eval_ctx: "EvalContext",
-    value: t.Iterable,
+    value: t.Iterable[t.Any],
     d: str = "",
     attribute: t.Optional[t.Union[str, int]] = None,
 ) -> str:
@@ -596,7 +596,7 @@ def sync_do_join(
 @async_variant(sync_do_join)  # type: ignore
 async def do_join(
     eval_ctx: "EvalContext",
-    value: t.Union[t.AsyncIterable, t.Iterable],
+    value: t.Union[t.AsyncIterable[t.Any], t.Iterable[t.Any]],
     d: str = "",
     attribute: t.Optional[t.Union[str, int]] = None,
 ) -> str:
@@ -1146,7 +1146,7 @@ def do_round(
 
 class _GroupTuple(t.NamedTuple):
     grouper: t.Any
-    list: t.List
+    list: t.List[t.Any]
 
     # Use the regular tuple repr to hide this subclass if users print
     # out the value during debugging.
@@ -1402,26 +1402,30 @@ def do_attr(
 
 @typing.overload
 def sync_do_map(
-    context: "Context", value: t.Iterable, name: str, *args: t.Any, **kwargs: t.Any
-) -> t.Iterable:
+    context: "Context",
+    value: t.Iterable[t.Any],
+    name: str,
+    *args: t.Any,
+    **kwargs: t.Any,
+) -> t.Iterable[t.Any]:
     ...
 
 
 @typing.overload
 def sync_do_map(
     context: "Context",
-    value: t.Iterable,
+    value: t.Iterable[t.Any],
     *,
     attribute: str = ...,
     default: t.Optional[t.Any] = None,
-) -> t.Iterable:
+) -> t.Iterable[t.Any]:
     ...
 
 
 @pass_context
 def sync_do_map(
-    context: "Context", value: t.Iterable, *args: t.Any, **kwargs: t.Any
-) -> t.Iterable:
+    context: "Context", value: t.Iterable[t.Any], *args: t.Any, **kwargs: t.Any
+) -> t.Iterable[t.Any]:
     """Applies a filter on a sequence of objects or looks up an attribute.
     This is useful when dealing with lists of objects but you are really
     only interested in a certain value of it.
@@ -1471,32 +1475,32 @@ def sync_do_map(
 @typing.overload
 def do_map(
     context: "Context",
-    value: t.Union[t.AsyncIterable, t.Iterable],
+    value: t.Union[t.AsyncIterable[t.Any], t.Iterable[t.Any]],
     name: str,
     *args: t.Any,
     **kwargs: t.Any,
-) -> t.Iterable:
+) -> t.Iterable[t.Any]:
     ...
 
 
 @typing.overload
 def do_map(
     context: "Context",
-    value: t.Union[t.AsyncIterable, t.Iterable],
+    value: t.Union[t.AsyncIterable[t.Any], t.Iterable[t.Any]],
     *,
     attribute: str = ...,
     default: t.Optional[t.Any] = None,
-) -> t.Iterable:
+) -> t.Iterable[t.Any]:
     ...
 
 
 @async_variant(sync_do_map)  # type: ignore
 async def do_map(
     context: "Context",
-    value: t.Union[t.AsyncIterable, t.Iterable],
+    value: t.Union[t.AsyncIterable[t.Any], t.Iterable[t.Any]],
     *args: t.Any,
     **kwargs: t.Any,
-) -> t.AsyncIterable:
+) -> t.AsyncIterable[t.Any]:
     if value:
         func = prepare_map(context, args, kwargs)
 
@@ -1689,7 +1693,7 @@ def do_tojson(
 
 
 def prepare_map(
-    context: "Context", args: t.Tuple, kwargs: t.Dict[str, t.Any]
+    context: "Context", args: t.Tuple[t.Any, ...], kwargs: t.Dict[str, t.Any]
 ) -> t.Callable[[t.Any], t.Any]:
     if not args and "attribute" in kwargs:
         attribute = kwargs.pop("attribute")
@@ -1718,7 +1722,7 @@ def prepare_map(
 
 def prepare_select_or_reject(
     context: "Context",
-    args: t.Tuple,
+    args: t.Tuple[t.Any, ...],
     kwargs: t.Dict[str, t.Any],
     modfunc: t.Callable[[t.Any], t.Any],
     lookup_attr: bool,
@@ -1753,7 +1757,7 @@ def prepare_select_or_reject(
 def select_or_reject(
     context: "Context",
     value: "t.Iterable[V]",
-    args: t.Tuple,
+    args: t.Tuple[t.Any, ...],
     kwargs: t.Dict[str, t.Any],
     modfunc: t.Callable[[t.Any], t.Any],
     lookup_attr: bool,
@@ -1769,7 +1773,7 @@ def select_or_reject(
 async def async_select_or_reject(
     context: "Context",
     value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
-    args: t.Tuple,
+    args: t.Tuple[t.Any, ...],
     kwargs: t.Dict[str, t.Any],
     modfunc: t.Callable[[t.Any], t.Any],
     lookup_attr: bool,
index aff7e9f993792e1ced39c93fc0d39dcb5bdd5fde..16ca73e1f94bb8e1c1ec3294101132f732462824 100644 (file)
@@ -447,7 +447,7 @@ def get_lexer(environment: "Environment") -> "Lexer":
     return lexer
 
 
-class OptionalLStrip(tuple):
+class OptionalLStrip(tuple):  # type: ignore[type-arg]
     """A special tuple for marking a point in the state that can have
     lstrip applied.
     """
index 57b55823087332ffbd4d76e738a35e6c64550d5a..9b479be4ee7398a56b22d02a26eedf6c3ce774e3 100644 (file)
@@ -177,7 +177,9 @@ class FileSystemLoader(BaseLoader):
 
     def __init__(
         self,
-        searchpath: t.Union[str, os.PathLike, t.Sequence[t.Union[str, os.PathLike]]],
+        searchpath: t.Union[
+            str, "os.PathLike[str]", t.Sequence[t.Union[str, "os.PathLike[str]"]]
+        ],
         encoding: str = "utf-8",
         followlinks: bool = False,
     ) -> None:
@@ -598,7 +600,10 @@ class ModuleLoader(BaseLoader):
     has_source_access = False
 
     def __init__(
-        self, path: t.Union[str, os.PathLike, t.Sequence[t.Union[str, os.PathLike]]]
+        self,
+        path: t.Union[
+            str, "os.PathLike[str]", t.Sequence[t.Union[str, "os.PathLike[str]"]]
+        ],
     ) -> None:
         package_name = f"_jinja2_module_templates_{id(self):x}"
 
index b2f88d9d9c19a2cb5d03b0158c743c6b947a29ea..00365ed835cae7ccc3acc87eadb4ab928466bc8c 100644 (file)
@@ -56,7 +56,7 @@ class NodeType(type):
 
     def __new__(mcs, name, bases, d):  # type: ignore
         for attr in "fields", "attributes":
-            storage = []
+            storage: t.List[t.Any] = []
             storage.extend(getattr(bases[0] if bases else object, attr, ()))
             storage.extend(d.get(attr, ()))
             assert len(bases) <= 1, "multiple inheritance not allowed"
index cefce2dfa1d2a4171838b0d0135af8ea3ff7d62c..fb4fd0d111eee6f594bcdeedfe7dcba70cd71654 100644 (file)
@@ -859,7 +859,14 @@ class Parser:
 
         return nodes.Slice(lineno=lineno, *args)
 
-    def parse_call_args(self) -> t.Tuple:
+    def parse_call_args(
+        self,
+    ) -> t.Tuple[
+        t.List[nodes.Expr],
+        t.List[nodes.Keyword],
+        t.Union[nodes.Expr, None],
+        t.Union[nodes.Expr, None],
+    ]:
         token = self.stream.expect("lparen")
         args = []
         kwargs = []
@@ -950,7 +957,7 @@ class Parser:
             next(self.stream)
             name += "." + self.stream.expect("name").value
         dyn_args = dyn_kwargs = None
-        kwargs = []
+        kwargs: t.List[nodes.Keyword] = []
         if self.stream.current.type == "lparen":
             args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()
         elif self.stream.current.type in {
index 93e21b45bd4bc82c7eec85f9ffba817e66df6774..c64999d9dc88842dd0dffb8bb1c379db10baf595 100644 (file)
@@ -259,7 +259,10 @@ class Context:
 
     @internalcode
     def call(
-        __self, __obj: t.Callable, *args: t.Any, **kwargs: t.Any  # noqa: B902
+        __self,  # noqa: B902
+        __obj: t.Callable[..., t.Any],
+        *args: t.Any,
+        **kwargs: t.Any,
     ) -> t.Union[t.Any, "Undefined"]:
         """Call the callable with the arguments and keyword arguments
         provided but inject the active context or environment as first
index 06d74148eccea79d1f5a0ca2fb76ecc246f87d62..153f42ec27b39a1843130e4aa8416a746f14815e 100644 (file)
@@ -37,7 +37,7 @@ UNSAFE_COROUTINE_ATTRIBUTES = {"cr_frame", "cr_code"}
 #: unsafe attributes on async generators
 UNSAFE_ASYNC_GENERATOR_ATTRIBUTES = {"ag_code", "ag_frame"}
 
-_mutable_spec: t.Tuple[t.Tuple[t.Type, t.FrozenSet[str]], ...] = (
+_mutable_spec: t.Tuple[t.Tuple[t.Type[t.Any], t.FrozenSet[str]], ...] = (
     (
         abc.MutableSet,
         frozenset(
@@ -80,7 +80,7 @@ _mutable_spec: t.Tuple[t.Tuple[t.Type, t.FrozenSet[str]], ...] = (
 )
 
 
-def inspect_format_method(callable: t.Callable) -> t.Optional[str]:
+def inspect_format_method(callable: t.Callable[..., t.Any]) -> t.Optional[str]:
     if not isinstance(
         callable, (types.MethodType, types.BuiltinMethodType)
     ) or callable.__name__ not in ("format", "format_map"):
@@ -350,7 +350,7 @@ class SandboxedEnvironment(Environment):
         s: str,
         args: t.Tuple[t.Any, ...],
         kwargs: t.Dict[str, t.Any],
-        format_func: t.Optional[t.Callable] = None,
+        format_func: t.Optional[t.Callable[..., t.Any]] = None,
     ) -> str:
         """If a format call is detected, then this is routed through this
         method so that our safety sandbox can be used for it.
index a467cf08b54879ee734617611aef72ed946d4566..0d29f9475aecbd91daf8b996427a627c093f07e3 100644 (file)
@@ -204,7 +204,7 @@ def test_escaped(value: t.Any) -> bool:
     return hasattr(value, "__html__")
 
 
-def test_in(value: t.Any, seq: t.Container) -> bool:
+def test_in(value: t.Any, seq: t.Container[t.Any]) -> bool:
     """Check if value is in seq.
 
     .. versionadded:: 2.10
index 9b5f5a50eb6773c4085f8572a45b3fa351367565..852d08294d686d42c8443b3efa9d52caf5bc1b60 100644 (file)
@@ -152,7 +152,7 @@ def import_string(import_name: str, silent: bool = False) -> t.Any:
             raise
 
 
-def open_if_exists(filename: str, mode: str = "rb") -> t.Optional[t.IO]:
+def open_if_exists(filename: str, mode: str = "rb") -> t.Optional[t.IO[t.Any]]:
     """Returns a file descriptor for the filename if that file exists,
     otherwise ``None``.
     """
@@ -450,7 +450,9 @@ class LRUCache:
         self.__dict__.update(d)
         self._postinit()
 
-    def __getnewargs__(self) -> t.Tuple:
+    def __getnewargs__(
+        self,
+    ) -> t.Tuple[int,]:
         return (self.capacity,)
 
     def copy(self) -> "LRUCache":