Support pkg_resources.declare_namespace for legacy namespace packages#22987
Merged
Support pkg_resources.declare_namespace for legacy namespace packages#22987
pkg_resources.declare_namespace for legacy namespace packages#22987Conversation
Extends the legacy namespace package detection to recognize the
`__import__('pkg_resources').declare_namespace(__name__)` pattern,
in addition to the existing `pkgutil.extend_path` patterns.
This allows ty to properly resolve imports across namespace packages
that use the pkg_resources approach, which was common before PEP 420.
Fixes astral-sh/ty#2679
https://claude.ai/code/session_01GRtteNwvNMMj5uPrj3bh1F
Typing conformance resultsNo changes detected ✅ |
|
pkg_resources.declare_namespace for legacy namespace packages
| return; | ||
| }; | ||
|
|
||
| if maybe_import.id() != "__import__" { |
Member
There was a problem hiding this comment.
Does this do the right thing if __import__ is shadowed or aliased?
Member
There was a problem hiding this comment.
probably not, but I think that's a pre-existing issue with this visitor, e.g. in check_pkgutil_extend_path. And very hard to resolve, because we don't have access to type inference at this early stage of analysis
Contributor
Author
There was a problem hiding this comment.
Yeah, it's a good callout, but this is something where we just can't be more precise; and in practice seems unlikely to be a problem.
Member
There was a problem hiding this comment.
No worries, just here to cause problems.
charliermarsh
approved these changes
Jan 30, 2026
AlexWaygood
approved these changes
Jan 30, 2026
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 astral-sh/ty#2679
Add support for detecting
__import__('pkg_resources').declare_namespace(__name__)as a legacy namespace package marker, in addition to the existingpkgutil.extend_pathdetection.This change extends the
LegacyNamespacePackageVisitorto recognize both patterns:__path__ = pkgutil.extend_path(__path__, __name__)(existing)__import__('pkg_resources').declare_namespace(__name__)(new)Test Plan
Added test cases in
legacy_namespace.mdverifying that namespace packages are correctly resolved when using thepkg_resourcespattern, and that resolution fails appropriately when the pattern is malformed.