ivankorobkov / python-inject

Python dependency injection
Apache License 2.0
672 stars 77 forks source link

Type-hints don't account for hashable keys #32

Closed andrewborba10 closed 5 years ago

andrewborba10 commented 5 years ago

Type-hints for any API which accepts a dependency binding defines it as "Type[T]" when in fact this can be a non-type value that is simply Hashable. Taken from docs:

It is possible to use any hashable object as a binding key.

Here is my suggested alternative:

T = TypeVar('T')
Binding = Union[Type[T], Hashable]
Constructor = Provider = Callable[[], T]

I confirmed that my IDE understands that when you pass a non-type, it falls to "Hashable" and drops away type-hints from there. As it stands, I suspect that if you try to use Hashable values in conjunction with a Python type-checker, then the checker would give failures when it should be allowed.

andrewborba10 commented 5 years ago

To add to this, it looks like configure*() APIs should have their return value be Injector