Open Spoonrad opened 2 months ago
I don't think it's currently possible. I believe this happens because of the overloaded inject.instance()
Injectable = Union[object, Any]
# ....
@overload
def instance(cls: Hashable) -> Injectable: ...
And I suppose most things are considered "hashable":
In [1]: from abc import ABC, abstractmethod
In [2]: class ICache(ABC):
...:
...: @abstractmethod
...: def check(self) -> bool:
...: ...
...:
In [3]: from collections.abc import Hashable
In [4]: issubclass(ICache, Hashable)
Out[4]: True
My understanding is that inject
allows to bind to any "hashable" for convenience but maybe there should be a more specific type for binding to an arbitrary object/value?
Doesn't look too difficult to fix given how broad the type spec currently is. Something like this should work:
InjectableTypeVar = TypeVar("InjectableTypeVar")
@overload
def instance(cls: type[InjectableTypeVar]) -> InjectableTypeVar:
...
How can I fix typing errors when using this library?
Example:
If I specify the type like this:
cache: ICache
I get an assignment error instead. How can I resolve this please?