Skip to content

[mypy] "Only concrete class can be given" fails with Dependency provider and abc #497

Open
@zerlok

Description

@zerlok

I'm trying to use Dependency provider according to the documentation, and the code works well, but mypy returns an error Only concrete class can be given where "Type[Animal]" is expected. I found this mypy issue, but I'm not sure is that problem related to me. Should I comment to the mypy issue and wait for it to be solved in mypy?

import abc

from dependency_injector.containers import DeclarativeContainer
from dependency_injector.providers import Dependency, Factory


class Animal(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def say(self) -> str:
        raise NotImplementedError


class Dog(Animal):
    def say(self) -> str:
        return "bark"


class Container(DeclarativeContainer):
    # mypy error: Only concrete class can be given where "Type[Animal]" is expected
    animal_dependency = Dependency(Animal)

    # no mypy errors
    animal_factory = Factory(Animal)


def main() -> None:
    container = Container()

    container.animal_dependency.override(Factory(Dog))
    container.animal_factory.override(Factory(Dog))

    print(container.animal_dependency().say())
    print(container.animal_factory().say())


if __name__ == "__main__":
    main()

project dependencies

dependency-injector==4.36.0
mypy==0.910
mypy-extensions==0.4.3
six==1.16.0
toml==0.10.2
typing-extensions==3.10.0.0

mypy settings in project.toml

[tool.mypy]
python_version = "3.8"

disallow_any_unimported = true
disallow_any_decorated = true
disallow_any_generics = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
disallow_untyped_decorators = true
no_implicit_optional = true
strict_optional = true

warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_return_any = true
warn_unreachable = true

strict_equality = true
implicit_reexport = false

show_error_context = true
show_column_numbers = true
show_error_codes = true
pretty = true

warn_unused_configs = true

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions