Skip to content

Commit

Permalink
Typing fixes (#208)
Browse files Browse the repository at this point in the history
Explicit optional type hints

This is the default in recent `mypy` versions. It is fully backwards
compatible, but the previous typing did not work with explicit
optional type hints.

Declare classes as generic

To fix the following `mypy` error:

> A function returning TypeVar should receive at least one argument
> containing the same TypeVar [type-var]
  • Loading branch information
davidparsson committed Dec 9, 2022
1 parent 93fe2e8 commit 3942351
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions injector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def get(self, injector: 'Injector') -> T:
raise NotImplementedError # pragma: no cover


class ClassProvider(Provider):
class ClassProvider(Provider, Generic[T]):
"""Provides instances from a given class, created using an Injector."""

def __init__(self, cls: Type[T]) -> None:
Expand All @@ -264,7 +264,7 @@ def get(self, injector: 'Injector') -> T:
return injector.create_object(self._cls)


class CallableProvider(Provider):
class CallableProvider(Provider, Generic[T]):
"""Provides something using a callable.
The callable is called every time new value is requested from the provider.
Expand Down Expand Up @@ -305,7 +305,7 @@ def __repr__(self) -> str:
return '%s(%r)' % (type(self).__name__, self._callable)


class InstanceProvider(Provider):
class InstanceProvider(Provider, Generic[T]):
"""Provide a specific instance.
::
Expand Down Expand Up @@ -392,7 +392,9 @@ class Binder:
_bindings: Dict[type, Binding]

@private
def __init__(self, injector: 'Injector', auto_bind: bool = True, parent: 'Binder' = None) -> None:
def __init__(
self, injector: 'Injector', auto_bind: bool = True, parent: Optional['Binder'] = None
) -> None:
"""Create a new Binder.
:param injector: Injector we are binding for.
Expand Down Expand Up @@ -460,7 +462,7 @@ def multibind(
self,
interface: Type[List[T]],
to: Union[List[T], Callable[..., List[T]], Provider[List[T]]],
scope: Union[Type['Scope'], 'ScopeDecorator'] = None,
scope: Union[Type['Scope'], 'ScopeDecorator', None] = None,
) -> None: # pragma: no cover
pass

Expand All @@ -469,12 +471,12 @@ def multibind(
self,
interface: Type[Dict[K, V]],
to: Union[Dict[K, V], Callable[..., Dict[K, V]], Provider[Dict[K, V]]],
scope: Union[Type['Scope'], 'ScopeDecorator'] = None,
scope: Union[Type['Scope'], 'ScopeDecorator', None] = None,
) -> None: # pragma: no cover
pass

def multibind(
self, interface: type, to: Any, scope: Union['ScopeDecorator', Type['Scope']] = None
self, interface: type, to: Any, scope: Union['ScopeDecorator', Type['Scope'], None] = None
) -> None:
"""Creates or extends a multi-binding.
Expand Down Expand Up @@ -555,7 +557,7 @@ def configure(self, binder):
instance(self)

def create_binding(
self, interface: type, to: Any = None, scope: Union['ScopeDecorator', Type['Scope']] = None
self, interface: type, to: Any = None, scope: Union['ScopeDecorator', Type['Scope'], None] = None
) -> Binding:
provider = self.provider_for(interface, to)
scope = scope or getattr(to or interface, '__scope__', NoScope)
Expand Down Expand Up @@ -864,9 +866,9 @@ class Injector:

def __init__(
self,
modules: Union[_InstallableModuleType, Iterable[_InstallableModuleType]] = None,
modules: Union[_InstallableModuleType, Iterable[_InstallableModuleType], None] = None,
auto_bind: bool = True,
parent: 'Injector' = None,
parent: Optional['Injector'] = None,
) -> None:
# Stack of keys currently being injected. Used to detect circular
# dependencies.
Expand Down Expand Up @@ -896,7 +898,7 @@ def __init__(
def _log_prefix(self) -> str:
return '>' * (len(self._stack) + 1) + ' '

def get(self, interface: Type[T], scope: Union[ScopeDecorator, Type[Scope]] = None) -> T:
def get(self, interface: Type[T], scope: Union[ScopeDecorator, Type[Scope], None] = None) -> T:
"""Get an instance of the given interface.
.. note::
Expand Down

0 comments on commit 3942351

Please sign in to comment.