Preserve qualifier on optional factory compat alias - #139
Merged
maldoinc merged 3 commits intoJun 5, 2026
Merged
Conversation
maldoinc
reviewed
Jun 4, 2026
| # 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)] |
Owner
There was a problem hiding this comment.
no qualifier is equivalent to qualifier = None so you can simplify this.
Owner
|
@ranjanprasad96this is now in 2.11.1 and 2.10.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thatcontainer.get(T)still resolves to theOptional[T]instance. That alias's syntheticraw_type_instanceparameter was annotated with the plain normalized type (Optional[T]) without carrying thequalifier.
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 factorywas registered under
(Optional[T], qualifier). The unqualified lookup missed during registry validation, surfacing as an apparent self-dependency onT.Fix
Preserve the qualifier on the compat alias's parameter annotation, matching the idiom already used for collection factories in the same file:
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.