Why does PyLance show reportArgumentType?

135 Views Asked by At

I have the following Python code which works fine (version 3.11). Why does PyLance complain about this?

import jinja2
templateEnv = jinja2.Environment(loader=jinja2.BaseLoader)
template = templateEnv.from_string(my_string)

PyLance v2024.2.2 in VS Code shows this error:

Argument of type "type[BaseLoader]" cannot be assigned to parameter "loader" of type "BaseLoader | None" in function "__init__"
  Type "type[BaseLoader]" cannot be assigned to type "BaseLoader | None"
    "type[type]" is incompatible with "type[BaseLoader]"
    "type[type]" is incompatible with "type[None]"
https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportArgumentType
1

There are 1 best solutions below

0
Marcin Orlowski On

PyLance expects an instance of BaseLoader classs or its subclass for loader argument, but in your code you pass the class itself and not instance of the class.

If you don't need any specific loading behavior that requires subclassing BaseLoader, you can simply instantiate it directly as follows:

import jinja2

templateEnv = jinja2.Environment(loader=jinja2.BaseLoader())
template = templateEnv.from_string(my_string)