Removed compatibility factory for types when Optional[T] factory is defined - #140
Open
ranjanprasad96 wants to merge 2 commits into
Open
Removed compatibility factory for types when Optional[T] factory is defined#140ranjanprasad96 wants to merge 2 commits into
ranjanprasad96 wants to merge 2 commits into
Conversation
Contributor
Author
|
@maldoinc Any updates on this PR? |
maldoinc
reviewed
Jun 9, 2026
maldoinc
left a comment
Owner
There was a problem hiding this comment.
Looks good, just a few minor items
|
|
||
| return await res if compiled_factory.is_async else res # type:ignore[no-any-return] | ||
|
|
||
| if (optional_klass := self._optional_compat_klass(klass, qualifier)) is not None: |
Owner
There was a problem hiding this comment.
This is deprecated behavior so whenever this branch is hit the deprecation warning should be raised.
|
|
||
| return compiled_factory.factory(self) # type:ignore[no-any-return] | ||
|
|
||
| if (optional_klass := self._optional_compat_klass(klass, qualifier)) is not None: |
Owner
There was a problem hiding this comment.
This is deprecated behavior so whenever this branch is hit the deprecation warning should be raised.
Comment on lines
53
to
54
| is_optional_type: bool | ||
| raw_type: type |
Owner
There was a problem hiding this comment.
These are now unused and can be removed.
Owner
There was a problem hiding this comment.
Let's leave the existing tests in tact to keep asserting the behavior, we can add any new ones as necessary
| def _register_mapping_collections(self) -> None: | ||
| for klass, qualifiers in dict(self.impls).items(): | ||
| # Only real registration keys should contribute to collection members. | ||
| # Synthetic aliases like raw optional-compat or Mapping[Hashable, T] keys must not participate here. |
Owner
There was a problem hiding this comment.
comment is stale, can be updated.
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]registered 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
To keep
container.get(T)working when a factory returnedOptional[T], Wireup registered an alias factory (compat_fn) under the raw typeT. That aliasdepended on the real
Optional[T]factory — but it built that dependency from the type alone, dropping the qualifier.So for a qualified factory, the alias looked up
(Optional[T], qualifier=None)while the real factory was registered under(Optional[T], qualifier). The lookupmissed, and registry validation reported it as a spurious self-dependency on
T.The alias was also too broad: living under the raw type
T, it leaked into injected resolution as well ascontainer.get— but this compatibility should applyto
container.getonly.Fix
Remove the compatibility alias factory entirely.
Optional[T]now registers a single factory under its normalizedT | Nonekey (qualifier preserved, like any other registration), so the qualifier can never be dropped.The
container.get(T)convenience is handled instead as a fallback on unknown types, scoped togetalone:Behavior changes
of the alias).
Tests
container.get(T, qualifier=...) resolves the correctly qualified instance. Verified it fails without the fix and passes with it.
All unit tests, mypy --strict, and ruff pass.