Skip to content

Commit

Permalink
Fix an InjectException raised when trying to bind a class with a stri…
Browse files Browse the repository at this point in the history
…ngified parameter type
  • Loading branch information
Aegdesil committed Jan 9, 2024
1 parent 2ddec9b commit 5125c10
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Opyoid follows [semver guidelines](https://semver.org) for versioning.

## Unreleased
## 2.0.2
### Fixes
- Fixed an InjectException raised when trying to bind a class with a stringified parameter type

## 2.0.1
### Fixes
- Fixed MultiBindings not using the correct provider when having multiple ItemBindings to Providers
Expand Down
1 change: 1 addition & 0 deletions opyoid/provider_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def get_provider(self, target: Target[InjectedT]) -> Optional[Provider[InjectedT
)
)
if len(possible_target_types) == 1:
target.type = possible_target_types[0]
# noinspection PyTypeChecker
frozen_target = FrozenTarget(possible_target_types[0], target.named)
elif possible_target_types:
Expand Down
18 changes: 18 additions & 0 deletions tests_e2e/test_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,24 @@ def __init__(self, my_param: MyClass):
self.assertIsInstance(other_parent.my_param, MyClass)
self.assertIs(parent.my_param, other_parent.my_param)

def test_injection_with_string_type_cache_2(self):
class MyParentClass:
def __init__(self, my_param: "MyClass"):
self.my_param = my_param

class MyOtherParentClass:
def __init__(self, my_param: MyClass):
self.my_param = my_param

injector = self.get_injector(MyOtherParentClass, MyParentClass, MyClass)
parent = injector.inject(MyParentClass)
self.assertIsInstance(parent, MyParentClass)
self.assertIsInstance(parent.my_param, MyClass)
other_parent = injector.inject(MyOtherParentClass)
self.assertIsInstance(other_parent, MyOtherParentClass)
self.assertIsInstance(other_parent.my_param, MyClass)
self.assertIs(parent.my_param, other_parent.my_param)

def test_private_module_multi_bind(self):
class DependencyClass:
pass
Expand Down

0 comments on commit 5125c10

Please sign in to comment.