Skip to content

Commit 785d90b

Browse files
author
Andrew William Borba
committed
Improve type-hints in .pyi stub
Summary: Type-hints were too restrictive on APIs which take a binding as a parameter. They didn't account for Hashable values which can be used instead of class types, as described in the docs. Fixed some return values on the configure*() APIs because they actually return the Injector. Improved type-hint for the binding config Callable on the configure*() APIs. Made some type aliases to make it a little easier to read.
1 parent e2f04f9 commit 785d90b

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/inject.pyi

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from typing import Type, TypeVar, Callable, Optional
2-
1+
from typing import Type, TypeVar, Callable, Optional, Union, Hashable
32

43
T = TypeVar('T')
4+
Binding = Union[Type[T], Hashable]
5+
Constructor = Provider = Callable[[], T]
6+
BinderCallable = Callable[['Binder'], None]
57

68

79
class Binder(object):
@@ -10,54 +12,54 @@ class Binder(object):
1012

1113
def install(
1214
self,
13-
config: Callable[[Binder], None]
15+
config: BinderCallable
1416
) -> Binder: ...
1517

1618
def bind(
1719
self,
18-
cls: Type[T],
20+
cls: Binding,
1921
instance: T
2022
) -> Binder: ...
2123

2224
def bind_to_constructor(
2325
self,
24-
cls: Type[T],
25-
constructor: Callable
26+
cls: Binding,
27+
constructor: Constructor
2628
) -> Binder: ...
2729

2830
def bind_to_provider(
2931
self,
30-
cls: Type[T],
31-
provider: Callable
32+
cls: Binding,
33+
provider: Provider
3234
) -> Binder: ...
3335

34-
def _check_class(self, cls: Type[T]) -> None: ...
36+
def _check_class(self, cls: Binding) -> None: ...
3537

3638

3739
class Injector(object):
3840

3941
def __init__(
4042
self,
41-
config: Optional[Callable[[Binder], None]] = None,
43+
config: Optional[BinderCallable] = None,
4244
bind_in_runtime: bool = True
4345
) -> None: ...
4446

45-
def get_instance(self, cls: Type[T]) -> T: ...
47+
def get_instance(self, cls: Binding) -> T: ...
4648

4749

4850
def configure(
49-
config: Optional[Callable], bind_in_runtime: bool = True
50-
) -> None: ...
51+
config: Optional[BinderCallable], bind_in_runtime: bool = True
52+
) -> Injector: ...
5153

5254

5355
def configure_once(
54-
config: Optional[Callable], bind_in_runtime: bool = True
55-
) -> None: ...
56+
config: Optional[BinderCallable], bind_in_runtime: bool = True
57+
) -> Injector: ...
5658

5759

5860
def clear_and_configure(
59-
config: Optional[Callable], bind_in_runtime: bool = True
60-
) -> None: ...
61+
config: Optional[BinderCallable], bind_in_runtime: bool = True
62+
) -> Injector: ...
6163

6264

6365
def is_configured() -> bool: ...
@@ -66,16 +68,16 @@ def is_configured() -> bool: ...
6668
def clear() -> None: ...
6769

6870

69-
def params(**args_to_classes: Type[T]) -> Callable: ...
71+
def params(**args_to_classes: Binding) -> Callable: ...
7072

7173

7274
def autoparams(*selected_args: str) -> Callable: ...
7375

7476

75-
def instance(cls: Type[T]) -> T: ...
77+
def instance(cls: Binding) -> T: ...
7678

7779

78-
def attr(cls: Type[T]) -> T: ...
80+
def attr(cls: Binding) -> T: ...
7981

8082

8183
def get_injector() -> Optional[Injector]: ...

0 commit comments

Comments
 (0)