python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Pyright: @injected providers throw type errors

Open jack-michaud opened this issue 3 years ago • 2 comments

When using Pyright for typechecking, injected provider arguments raise type errors.

For example, in the flask miniapp (examples/miniapps/flask/), the full output from running pyright githubnavigator/views.py from the root of the flask example app is below:

Searching for source files
Found 1 source file
/home/****/Code/ets-labs/python-dependency-injector/examples/miniapps/flask/githubnavigator/views.py
  12:49 - error: Expected class type but received "Configuration" (reportGeneralTypeIssues)
  12:49 - error: Expected no type arguments (reportGeneralTypeIssues)
  12:41 - error: Expression of type "Type[Provide[Configuration]]" cannot be assigned to parameter of type "SearchService"
    "Type[ClassGetItemMeta]" is incompatible with "Type[SearchService]" (reportGeneralTypeIssues)
  13:38 - error: Expected class type but received "Configuration" (reportGeneralTypeIssues)
  13:38 - error: Expected class type but received "ConfigurationOption" (reportGeneralTypeIssues)
  13:38 - error: Expected class type but received "ConfigurationOption" (reportGeneralTypeIssues)
  13:38 - error: Expected no type arguments (reportGeneralTypeIssues)
  13:30 - error: Expression of type "Type[Provide[ConfigurationOption]]" cannot be assigned to parameter of type "str"
    "Type[ClassGetItemMeta]" is incompatible with "Type[str]" (reportGeneralTypeIssues)
  14:38 - error: Expected class type but received "TypedConfigurationOption[int]" (reportGeneralTypeIssues)
  14:38 - error: Expected no type arguments (reportGeneralTypeIssues)
  14:30 - error: Expression of type "Type[Provide[TypedConfigurationOption[int]]]" cannot be assigned to parameter of type "int"
    "Type[ClassGetItemMeta]" is incompatible with "Type[int]" (reportGeneralTypeIssues)
11 errors, 0 warnings, 0 infos
Completed in 1.186sec

Edit: Pre-emptively published.

jack-michaud avatar Apr 25 '21 05:04 jack-michaud

Hi @jack-michaud. I never tested Dependency Injector with Pyright. Thanks for bringing up the issue. I don’t know yet if there is a way to get these errors fixed.

rmk135 avatar Apr 26 '21 02:04 rmk135

same with mypy

class Container(containers.DeclarativeContainer):
    metric_settings = providers.Configuration(pydantic_settings=[MetricSettings()])

class PlatformMetric():

    def __init__(self) -> None:

        container = Container()
        self.config = container.metric_settings
        rotation_name = self.config.rotation_name.as_(str) #(variable) rotation_name: TypedConfigurationOption[str]

rotation_name has type TypedConfigurationOption[str] but mypy expects string

How can i suppress this error ?

mancioshell avatar Sep 09 '22 13:09 mancioshell