Do not type DeclarativeContainer.__getattr__ as returning a Provider (#910)#973
Open
uttam12331 wants to merge 1 commit into
Open
Conversation
The `__getattr__(self, name: str) -> Provider[Any]` stub lived on the base `Container`, so `DeclarativeContainer` inherited a catch-all that made type checkers accept access to any attribute — silencing typos and references to providers that were never declared. Move the stub to `DynamicContainer`, whose providers really are added by name at runtime. `DeclarativeContainer` now only exposes its statically declared providers, so accessing an undeclared one is flagged by the type checker while dynamic containers keep resolving attributes to `Provider[Any]`. Closes ets-labs#910
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
Closes #910.
The container type stub declares
on the base
Container, soDeclarativeContainerinherits a catch-all__getattr__. That makes type checkers accept access to any attribute on a declarative container as aProvider, silently hiding typos and references to providers that were never declared:As noted in the issue, this only makes sense for
DynamicContainer, whose providers are added by name at runtime — not forDeclarativeContainer, whose providers are declared statically.Change
Move the
__getattr__stub from the baseContainertoDynamicContainer. Now:DeclarativeContainerexposes only its statically declared providers → accessing an undeclared attribute is flagged.DynamicContainerkeeps resolving arbitrary attributes toProvider[Any].Verified with
mypy --strict:Tests
mypy --strict tests/typingpasses (19 source files). Added Test 7 intests/typing/dynamic_container.pyasserting that dynamic attribute access still resolves toproviders.Provider[Any], so theDynamicContainerbehavior stays covered. The existingdeclarative_container.pytyping tests continue to pass because they access explicitly declared providers.