Skip to content

Preserve qualifier on optional factory compat alias - #139

Merged
maldoinc merged 3 commits into
maldoinc:masterfrom
ranjanprasad96:feature/preserve_qualifier_optional_factory_master
Jun 5, 2026
Merged

Preserve qualifier on optional factory compat alias#139
maldoinc merged 3 commits into
maldoinc:masterfrom
ranjanprasad96:feature/preserve_qualifier_optional_factory_master

Conversation

@ranjanprasad96

@ranjanprasad96 ranjanprasad96 commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #138 — factories returning Optional[T] with a qualifier failed at container creation with a spurious self-dependency error:

WireupError: Parameter 'raw_type_instance' of <class 'AuthContext'>
has an unknown dependency on <class 'AuthContext'>.

Root cause

When a factory returns Optional[T], Wireup registers a backwards-compatibility alias factory (compat_fn) so that container.get(T) still resolves to the
Optional[T] instance. That alias's synthetic raw_type_instance parameter was annotated with the plain normalized type (Optional[T]) without carrying the
qualifier.

As a result, when the original factory was registered with a qualifier, the alias declared a dependency on (Optional[T], qualifier=None), while the real factory
was registered under (Optional[T], qualifier). The unqualified lookup missed during registry validation, surfacing as an apparent self-dependency on T.

Fix

Preserve the qualifier on the compat alias's parameter annotation, matching the idiom already used for collection factories in the same file:

raw_type_annotation = klass if qualifier is None else Annotated[klass, InjectableQualifier(qualifier)]

When a qualifier is present, the alias now depends on the correctly qualified Optional[T] factory.

Tests

Added test_optional_factory_with_qualifier, which registers an Optional[T] factory with a qualifier alongside a plain T factory and asserts both resolve. Verified it fails without the fix and passes with it.

Comment thread wireup/ioc/registry.py Outdated
# The alias factory depends on the normalized Optional[T] instance. When the
# original factory was registered with a qualifier, carry it over so the
# dependency resolves to the qualified factory instead of an unqualified one.
raw_type_annotation = klass if qualifier is None else Annotated[klass, InjectableQualifier(qualifier)]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

no qualifier is equivalent to qualifier = None so you can simplify this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks

@maldoinc
maldoinc merged commit 0521fa5 into maldoinc:master Jun 5, 2026
8 checks passed
@maldoinc

maldoinc commented Jun 5, 2026

Copy link
Copy Markdown
Owner

@ranjanprasad96this is now in 2.11.1 and 2.10.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot use qualifiers on factory with Optional Types

2 participants