python-injector / python-injector/injector

Mypy error when getting abstract classes

Open
#143 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
94
PR merge metrics
No merged PRs in 30d

Description

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).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the mypy errors in the examples using binder.bind and injector.get, then inspect the type hints for those entry points. The work is done when the abstract-interface binding and retrieval examples type-check without errors while preserving the intended Injector API.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.