Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy error when getting abstract classes #143

Open
davidparsson opened this issue Apr 22, 2020 · 4 comments
Open

Mypy error when getting abstract classes #143

davidparsson opened this issue Apr 22, 2020 · 4 comments

Comments

@davidparsson
Copy link
Collaborator

davidparsson commented Apr 22, 2020

Using Injector to bind a concrete class to an abstract interface, and then getting an instance of that interface causes a mypy error:

class MyABC(ABC): pass
class MyClass(MyABC): pass

injector = Injector()
injector.binder.bind(MyABC, to=MyClass)  # error: Only concrete class can be given where "Type[MyABC]" is expected
injector.get(MyABC)  # error: Only concrete class can be given where "Type[MyABC]" is expected

I hope that this can be resolved by fixing type hints (although it's certainly possible to configure Injector so that the type hints will be incorrect).

@jstasiak
Copy link
Collaborator

This is unfortunate. I think this is MyPy/typing territory though as I don't believe we have any other tools to express this right now. See python/mypy#4717 for a discussion on this.

@davidparsson
Copy link
Collaborator Author

Got it! Let's hope for an upstream resolution.

Thanks for a timely response, as always! 🏅

@davidparsson
Copy link
Collaborator Author

davidparsson commented Jun 22, 2020

Newer Injector versions with more type hints causes the bind() call to fail with the same error, so I've updated the example to reflect this.

@davidparsson
Copy link
Collaborator Author

As of v0.991, mypy has a dedicated error code for this. With type-abstract this error can at least suppressed. A maintainer indicated that this might be as good at it gets.

See the release notes for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants