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

Allow non hashable instances to be exposed in private modules #4

Merged
merged 1 commit into from
Oct 13, 2020
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
@@ -1,5 +1,9 @@
# Changelog
## Unreleased
## 0.6.2
### Fixes
- Non hashable instance bindings can be exposed in private modules

## 0.6.1
### Fixes
- Fixed a bug preventing injection when using strings as type hints
Expand Down
2 changes: 1 addition & 1 deletion opyoid/bindings/abstract_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def install(self, module: "AbstractModule") -> None:
module.configure_once()
for binding in module.binding_registry.get_bindings_by_target().values():
if isinstance(module, PrivateModule):
if not module.is_exposed(binding):
if not module.is_exposed(binding.target):
continue
binding = RegisteredBinding(
binding.raw_binding,
Expand Down
10 changes: 6 additions & 4 deletions opyoid/bindings/private_module.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
from typing import Set

from opyoid.frozen_target import FrozenTarget
from opyoid.typings import InjectedT
from .abstract_module import AbstractModule
from .registered_binding import RegisteredBinding


class PrivateModule(AbstractModule):
def __init__(self):
AbstractModule.__init__(self)
self._exposed_bindings: Set[RegisteredBinding] = set()
self._exposed_bindings: Set[FrozenTarget] = set()

def configure(self) -> None:
raise NotImplementedError

def is_exposed(self, binding: RegisteredBinding) -> bool:
return binding in self._exposed_bindings
def is_exposed(self, target: FrozenTarget[InjectedT]) -> bool:
return target in self._exposed_bindings

def expose(self, *bindings: RegisteredBinding) -> None:
for binding in bindings:
self._exposed_bindings.add(binding)
self._exposed_bindings.add(binding.target)