python-injector / python-injector/injector

Binding to interface style class Protocols

Open
#177 6 comments 1 reaction 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

Separated out from #168 - specifically to deal with binding to a class level Protocol usage (i.e normal methods defined in the protocol rather than the special __call__() usage.

This use case for Protocol is about as close as I've found in python to a java interface, thus also feels like it would be a pretty desirable use case to support in an injection framework. In fact, if you ignore the MyPy warnings, this use case seems to work with the injector code on master. (based on a very quick unit test!)

With usage of @runtime_checkable on the Protocol class, you can use isinstance() on it, which might ease some of the implementation pain?

I think with some sensible use of @overload on the binder.bind method, it might be possible to isolate these use cases and allow some type checking that things aren't entirely wrong, if your concerns are around pushing a load of checking to run-time?

If only the class level Protocol is being supported at this stage, we should ensure that any attempt to bind to a protocol with a __call__ method fails at runtime.

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

Start at the binder.bind entry point and inspect how the injector currently handles Protocol classes. Reproduce the issue with a small unit test based on the class-level Protocol use case, including @runtime_checkable and a Protocol with call. Done means class-level Protocol binding is supported with appropriate type-checking behavior, while unsupported call bindings fail at runtime.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.