From 8ed8e1d0eda956aa0cdc6e216270be3ebcfd4b4a Mon Sep 17 00:00:00 2001 From: David Lord Date: Thu, 19 Jan 2023 17:49:35 -0800 Subject: fix mypy strict findings --- src/jinja2/async_utils.py | 2 +- src/jinja2/environment.py | 12 +++++++----- src/jinja2/filters.py | 42 +++++++++++++++++++++++------------------- src/jinja2/lexer.py | 2 +- src/jinja2/loaders.py | 9 +++++++-- src/jinja2/nodes.py | 2 +- src/jinja2/parser.py | 11 +++++++++-- src/jinja2/runtime.py | 5 ++++- src/jinja2/sandbox.py | 6 +++--- src/jinja2/tests.py | 2 +- src/jinja2/utils.py | 6 ++++-- 11 files changed, 61 insertions(+), 38 deletions(-) diff --git a/src/jinja2/async_utils.py b/src/jinja2/async_utils.py index 715d701..e65219e 100644 --- a/src/jinja2/async_utils.py +++ b/src/jinja2/async_utils.py @@ -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 diff --git a/src/jinja2/environment.py b/src/jinja2/environment.py index 88b2666..29a1b4e 100644 --- a/src/jinja2/environment.py +++ b/src/jinja2/environment.py @@ -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: diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index ed07c4c..f4479ff 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -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, diff --git a/src/jinja2/lexer.py b/src/jinja2/lexer.py index aff7e9f..16ca73e 100644 --- a/src/jinja2/lexer.py +++ b/src/jinja2/lexer.py @@ -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. """ diff --git a/src/jinja2/loaders.py b/src/jinja2/loaders.py index 57b5582..9b479be 100644 --- a/src/jinja2/loaders.py +++ b/src/jinja2/loaders.py @@ -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}" diff --git a/src/jinja2/nodes.py b/src/jinja2/nodes.py index b2f88d9..00365ed 100644 --- a/src/jinja2/nodes.py +++ b/src/jinja2/nodes.py @@ -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" diff --git a/src/jinja2/parser.py b/src/jinja2/parser.py index cefce2d..fb4fd0d 100644 --- a/src/jinja2/parser.py +++ b/src/jinja2/parser.py @@ -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 { diff --git a/src/jinja2/runtime.py b/src/jinja2/runtime.py index 93e21b4..c64999d 100644 --- a/src/jinja2/runtime.py +++ b/src/jinja2/runtime.py @@ -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 diff --git a/src/jinja2/sandbox.py b/src/jinja2/sandbox.py index 06d7414..153f42e 100644 --- a/src/jinja2/sandbox.py +++ b/src/jinja2/sandbox.py @@ -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. diff --git a/src/jinja2/tests.py b/src/jinja2/tests.py index a467cf0..0d29f94 100644 --- a/src/jinja2/tests.py +++ b/src/jinja2/tests.py @@ -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 diff --git a/src/jinja2/utils.py b/src/jinja2/utils.py index 9b5f5a5..852d082 100644 --- a/src/jinja2/utils.py +++ b/src/jinja2/utils.py @@ -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": -- cgit v1.2.1