Skip to content

fix: type propogation through provided #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dependency_injector/providers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Provider(Generic[T]):
@property
def provider(self) -> Provider: ...
@property
def provided(self) -> ProvidedInstance: ...
def provided(self) -> T: ...
def enable_async_mode(self) -> None: ...
def disable_async_mode(self) -> None: ...
def reset_async_mode(self) -> None: ...
Expand Down
5 changes: 1 addition & 4 deletions tests/typing/callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ def create(cls) -> Animal:

# Test 5: to check the provided instance interface
provider5 = providers.Callable(Animal)
provided5: providers.ProvidedInstance = provider5.provided
attr_getter5: providers.AttributeGetter = provider5.provided.attr
item_getter5: providers.ItemGetter = provider5.provided["item"]
method_caller: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
provided5: Animal = provider5.provided
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not exactly correct. The attribute provider5.provided returns a provider, not an object of Animal. I can't recall why it was important, but the current change will introduce a functional change in the typing approach.

To make a correct typing test with the .provided attribute we would need to do something like that:

provided5: Animal = provider5.provided()

At least, that would be what the library actually does.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rmk135 updated the tests with .provided()

cc @philipbjorge thanks for flagging


# Test 6: to check the DelegatedCallable
provider6 = providers.DelegatedCallable(Cat)
Expand Down
2 changes: 1 addition & 1 deletion tests/typing/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
a1=providers.Factory(object),
a2=providers.Factory(object),
)
provided5: providers.ProvidedInstance = provider5.provided
provided5: dict[Any, Any] = provider5.provided


# Test 6: to check the return type with await
Expand Down
5 changes: 1 addition & 4 deletions tests/typing/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ def create(cls) -> Animal:

# Test 5: to check the provided instance interface
provider5 = providers.Factory(Animal)
provided5: providers.ProvidedInstance = provider5.provided
attr_getter5: providers.AttributeGetter = provider5.provided.attr
item_getter5: providers.ItemGetter = provider5.provided["item"]
method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
provided5: Animal = provider5.provided

# Test 6: to check the DelegatedFactory
provider6 = providers.DelegatedFactory(Cat)
Expand Down
5 changes: 1 addition & 4 deletions tests/typing/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
providers.Factory(object),
providers.Factory(object),
)
provided3: providers.ProvidedInstance = provider3.provided
attr_getter3: providers.AttributeGetter = provider3.provided.attr
item_getter3: providers.ItemGetter = provider3.provided["item"]
method_caller3: providers.MethodCaller = provider3.provided.method.call(123, arg=324)
provided3: List[Any] = provider3.provided

# Test 4: to check the return type with await
provider4 = providers.List(
Expand Down
5 changes: 1 addition & 4 deletions tests/typing/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

# Test 2: to check the provided instance interface
provider2 = providers.Object(int)
provided2: providers.ProvidedInstance = provider2.provided
attr_getter2: providers.AttributeGetter = provider2.provided.attr
item_getter2: providers.ItemGetter = provider2.provided["item"]
method_caller2: providers.MethodCaller = provider2.provided.method.call(123, arg=324)
provided2: Type[int] = provider2.provided

# Test 3: to check the return type with await
provider3 = providers.Object(int(3))
Expand Down
2 changes: 1 addition & 1 deletion tests/typing/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Test 1: to check .provided attribute
provider1: providers.Provider[int] = providers.Object(1)
provided: providers.ProvidedInstance = provider1.provided
provided: int = provider1.provided

# Test 2: to check async mode API
provider2: providers.Provider = providers.Provider()
Expand Down
5 changes: 1 addition & 4 deletions tests/typing/singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ def create(cls) -> Animal:

# Test 5: to check the provided instance interface
provider5 = providers.Singleton(Animal)
provided5: providers.ProvidedInstance = provider5.provided
attr_getter5: providers.AttributeGetter = provider5.provided.attr
item_getter5: providers.ItemGetter = provider5.provided["item"]
method_caller5: providers.MethodCaller = provider5.provided.method.call(123, arg=324)
provided5: Animal = provider5.provided

# Test 6: to check the DelegatedSingleton
provider6 = providers.DelegatedSingleton(Cat)
Expand Down