Skip to content
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
2 changes: 1 addition & 1 deletion modern_di/providers/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _find_dep_provider(self, container: "Container", v: SignatureItem) -> "Abstr
return provider
for x in v.args:
provider = container.providers_registry.find_provider(x)
if provider:
if provider is not None and provider is not self:
return provider
return None

Expand Down
17 changes: 17 additions & 0 deletions tests/providers/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ def second_creator(first_factory: str) -> str:
assert app_container.resolve_provider(second_factory) == "one two"


def test_factory_self_reference_in_union_falls_through_to_default() -> None:
@dataclasses.dataclass(kw_only=True, slots=True)
class SelfRef:
x: int = 1

def make(x: int | SelfRef = 1) -> SelfRef:
return SelfRef(x=x if isinstance(x, int) else x.x)

factory = providers.Factory(creator=make)
app_container = Container()
app_container.providers_registry.add_providers(factory)

result = app_container.resolve(SelfRef)
assert isinstance(result, SelfRef)
assert result.x == 1


def test_factory_repr() -> None:
provider = providers.Factory(creator=str, scope=Scope.APP)
assert repr(provider) == "Factory(creator=<class 'str'>, scope=<Scope.APP: 1>, cached=False)"
Expand Down
Loading