Skip to content

jinja2.Environment().filters[name] have unexpected type instead of Any #3263

@Tatsh

Description

@Tatsh

Summary

I see really strange output for just a single line for code like this (used in testing):

env = jinja2.Environment(extensions=[...])
func = env.filters['func']
result = func('something')  # this line

func should just be treated as Any here, which is what mypy and Pyright do, unless you want to add support for Jinja2 semantics and find what func should be.

error[missing-argument]: No argument provided for required parameter `linecount`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[V](value: Iterable[V], linecount: int, fill_with: V | None = None) -> Iterator[list[V]]` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

And there's way more for just this 1 line:

Details
error[missing-argument]: No argument provided for required parameter `seq`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[V](environment: Environment, seq: Reversible[V]) -> V | Undefined` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[missing-argument]: No argument provided for required parameter `value`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[V](environment: Environment, value: Iterable[V], case_sensitive: bool = False, attribute: str | int | None = None) -> V | Undefined` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[missing-argument]: No argument provided for required parameter `value`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[V](environment: Environment, value: Iterable[V], reverse: bool = False, case_sensitive: bool = False, attribute: str | int | None = None) -> list[V]` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[missing-argument]: No argument provided for required parameter `seq`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[V](context: Context, seq: Sequence[V]) -> V | Undefined` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[missing-argument]: No arguments provided for required parameters `s`, `old`, `new`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(eval_ctx: EvalContext, s: str, old: str, new: str, count: int | None = None) -> str` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[missing-argument]: No argument provided for required parameter `s`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(env: Environment, s: str, length: int = 255, killwords: bool = False, end: str = "...", leeway: int | None = None) -> str` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[missing-argument]: No argument provided for required parameter `value`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(eval_ctx: EvalContext, value: str, trim_url_limit: int | None = None, nofollow: bool = False, target: str | None = None, rel: str | None = None, extra_schemes: Iterable[str] | None = None) -> str` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[missing-argument]: No argument provided for required parameter `s`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(environment: Environment, s: str, width: int = 79, break_long_words: bool = True, wrapstring: str | None = None, break_on_hyphens: bool = True) -> str` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[missing-argument]: No argument provided for required parameter `d`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(eval_ctx: EvalContext, d: Mapping[str, Any], autospace: bool = True) -> str` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[missing-argument]: No argument provided for required parameter `value`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(eval_ctx: EvalContext, value: Any, indent: int | None = None) -> Markup` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[missing-argument]: No arguments provided for required parameters `obj`, `name`
   --> tests/test_extensions.py:146:14
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(environment: Environment, obj: Any, name: str) -> Undefined | Any` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `missing-argument` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `Environment`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(environment: Environment, obj: Any, name: str) -> Undefined | Any` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `Mapping[Unknown, Unknown]`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[K, V](value: Mapping[K, V], case_sensitive: bool = False, by: Literal["key", "value"] = "key", reverse: bool = False) -> list[tuple[K, V]]` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `Environment`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[V](environment: Environment, seq: Reversible[V]) -> V | Undefined` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `Mapping[Unknown, Unknown] | Undefined`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[K, V](value: Mapping[K, V] | Undefined) -> Iterator[tuple[K, V]]` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `Environment`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[V](environment: Environment, value: Iterable[V], case_sensitive: bool = False, attribute: str | int | None = None) -> V | Undefined` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `Environment`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[V](environment: Environment, value: Iterable[V], reverse: bool = False, case_sensitive: bool = False, attribute: str | int | None = None) -> list[V]` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `Context`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[V](context: Context, seq: Sequence[V]) -> V | Undefined` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `EvalContext`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(eval_ctx: EvalContext, s: str, old: str, new: str, count: int | None = None) -> str` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `int | float`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(value: int | float, precision: int = 0, method: Literal["common", "ceil", "floor"] = "common") -> int | float` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `Environment`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(env: Environment, s: str, length: int = 255, killwords: bool = False, end: str = "...", leeway: int | None = None) -> str` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `EvalContext`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(eval_ctx: EvalContext, value: str, trim_url_limit: int | None = None, nofollow: bool = False, target: str | None = None, rel: str | None = None, extra_schemes: Iterable[str] | None = None) -> str` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `Environment`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(environment: Environment, s: str, width: int = 79, break_long_words: bool = True, wrapstring: str | None = None, break_on_hyphens: bool = True) -> str` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `EvalContext`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(eval_ctx: EvalContext, d: Mapping[str, Any], autospace: bool = True) -> str` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `EvalContext`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `(eval_ctx: EvalContext, value: Any, indent: int | None = None) -> Markup` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

error[invalid-argument-type]: Argument is incorrect
   --> tests/test_extensions.py:146:21
    |
144 |     env = jinja2.Environment(extensions=[ShellExtension], autoescape=True)
145 |     indent = env.filters['shell_indent']
146 |     result = indent('echo hello\necho world')
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^ Expected `SupportsAbs[Unknown]`, found `Literal["echo hello\necho world"]`
147 |     assert result == '    echo hello\n    echo world'
    |
info: Union variant `[_T](x: SupportsAbs[_T], /) -> _T` is incompatible with this call site
info: Attempted to call union type `Unknown | ([_T](x: SupportsAbs[_T], /) -> _T) | ((environment: Environment, obj: Any, name: str) -> Undefined | Any) | ... omitted 29 union elements`
info: rule `invalid-argument-type` is enabled by default

Version

ty 0.0.29

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions