Skip to content

Python: Fix resolution of relative imports from namespace packages#21459

Draft
tausbn wants to merge 3 commits intomainfrom
tausbn/python-fix-missing-relative-imports
Draft

Python: Fix resolution of relative imports from namespace packages#21459
tausbn wants to merge 3 commits intomainfrom
tausbn/python-fix-missing-relative-imports

Conversation

@tausbn
Copy link
Contributor

@tausbn tausbn commented Mar 11, 2026

Fixes an issue that was causing us to not resolve relative imports from namespace packages correctly (thankfully a relatively uncommon occurrence).

Should be reviewed commit-by-commit. The first commit adds a test that demonstrates the issue (indirectly, by observing a lack of data-flow), and the second fixes the issue and updates the test to reflect this.

tausbn added 2 commits March 11, 2026 23:03
The fix may look a bit obscure, so here's what's going on.

When we see `from . import helper`, we create an `ImportExpr` with level
equal to 1 (corresponding to the number of dots). To resolve such
imports, we compute the name of the enclosing package, as part of
`ImportExpr.qualifiedTopName()`. For this form of import expression, it
is equivalent to `this.getEnclosingModule().getPackageName()`. But
`qualifiedTopName` requires that `valid_module_name` holds for its
result, and this was _not_ the case for namespace packages.

To fix this, we extend `valid_module_name` to include the module names
of _any_ folder, not just regular package (which are the ones where
there's a `__init__.py` in the folder). Note that this doesn't simply
include all folders -- only the ones that result in valid module names
in Python.
@tausbn tausbn force-pushed the tausbn/python-fix-missing-relative-imports branch from a57c994 to d2e760f Compare March 11, 2026 23:06
@tausbn tausbn changed the title Python: Fix missing relative imports Python: Fix resolution of relative imports from namespace packages Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant