Skip to content
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

Fix an InjectException raised when trying to bind a class with a stringified parameter type #77

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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