Repository navigation
fix(python): resolve imports across PEP 420 namespace packages under src/ - #4929
Conversation
…src/ In a monorepo of namespace distributions (pkgsql/src/airflow/..., pkgpresto/src/airflow/..., no __init__.py anywhere), the package-root pass skipped every file because it found no initializer, so an absolute import across distributions dangled. The importing class lost its base, its inherited self.method() calls were dropped, and imported functions fell back to INFERRED name matches. When no initializer is found, take the nearest src/ ancestor as the import root. Modules directly under src/ and stdlib-named namespaces are left alone, and the existing guard still leaves a module claimed by two src roots unresolved.
|
Thanks for the pull request, @nothariharan. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic. |
There was a problem hiding this comment.
Graphify reviewed this change.
No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.
Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 not verified).
Formal verification. PR-changed functions: 0/1 verified (0 proven, 0 may-equivalent, 0 distinguished) · 1 not verified (1 vacuous).
Not verified on this run: \_repoint\_python\_package\_imports (vacuous: never exercised).
Graphify review — findings
Resolves absolute Python imports across PEP 420 namespace packages under a src/ root, so a monorepo of distributions sharing a namespace (e.g. airflow.sql.hooks.sql) keeps its cross-distribution import edges and the inherited self.method() calls that depend on them. The new _namespace_src_module_parts derives the module path from the nearest src directory when the file sits at least one level below it, and _repoint_python_package_imports uses it when no __init__.py is found above the file. Loose modules directly under src/ (like src/helpers.py) and namespaces named like stdlib modules (like src/logging/...) are left unresolved, since a bare import of those is as likely third-party or stdlib. If the same namespace module exists under two src/ roots, the import stays ambiguous and no call is bound to either copy.
No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.
Analysis details — impact, health, verification
Impact & health
Graphify review
Impact — 3508 functions depend on the 380 functions this change touches.
Health — this change adds coupling hotspots:
- worse:
extract()— 1091 callers, 64 callees - worse:
_repoint_python_package_imports()— 1 callers, 7 callees
Verification — 3508 functions in the blast radius were not formally verified this run (proofs are advisory here).
Gate & verification
graphify gate
PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.
Advisory (not blocking):
- verification_scope: 3246 function(s) in the blast radius were not formally verified this run
Test selection
Test selection
234 of 487 test file(s) selected (48%) via static blast radius.
tests/test_4874_jvm_resolution.py— impacttests/test_affected_overrides.py— impacttests/test_astro_extraction.py— impacttests/test_astro_import_ids.py— impacttests/test_blade_extractor.py— impacttests/test_build.py— impacttests/test_builtin_global_type_refs.py— impacttests/test_c_include_resolution.py— impacttests/test_cache.py— impacttests/test_case_only_decl_ids_4904.py— impacttests/test_case_sensitive_resolution.py— impacttests/test_cjs_module_extension.py— impacttests/test_cobol_extractor.py— impacttests/test_confidence_score_output.py— impacttests/test_cpp_method_declarations.py— impacttests/test_cpp_nested_and_cli.py— impacttests/test_cpp_objc_cross_file_calls.py— impacttests/test_cpp_template_type_params.py— impacttests/test_cpp_type_alias.py— impacttests/test_cross_extension_reexport_self_cycle.py— impacttests/test_cross_language_call_resolution.py— impacttests/test_cross_repo_external_call_guards.py— impacttests/test_cross_repo_member_calls.py— impacttests/test_csharp_call_site_generic_args.py— impacttests/test_csharp_delegate_declarations.py— impacttests/test_csharp_enum_members.py— impacttests/test_csharp_field_generic_args.py— impacttests/test_csharp_foreach_loop_variable.py— impacttests/test_csharp_generic_callsites.py— impacttests/test_csharp_interface_dispatch.py— impacttests/test_csharp_member_calls.py— impacttests/test_csharp_member_chain_calls.py— impacttests/test_csharp_member_nodes.py— impacttests/test_csharp_method_owner_labels.py— impacttests/test_csharp_object_creation.py— impacttests/test_csharp_overloads.py— impacttests/test_csharp_partial_classes.py— impacttests/test_csharp_tuple_type_refs.py— impacttests/test_csharp_type_resolution.py— impacttests/test_csharp_underscore_collisions.py— impacttests/test_csharp_var_call_return_type.py— impacttests/test_csharp_verbatim_interpolation.py— impacttests/test_definition_file_portability.py— impacttests/test_detect.py— impacttests/test_dotnet.py— impacttests/test_duplicate_annotation_edges.py— impacttests/test_edge_also_seen.py— impacttests/test_ejs_extractor.py— impacttests/test_elixir_defdelegate.py— impacttests/test_elixir_import_resolution.py— impact- … and 184 more
Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.
Formal verification
Could not verify: Could not verify \_repoint\_python\_package\_imports.
The verifier did not have enough to check \_repoint\_python\_package\_imports, so it is saying so rather than guessing. No false assurance is the whole point.
Guarantee: No guarantee either way, this is an honest abstention, not a pass.
Note: Reason: no capturable inputs from the test suite; property tier: non-vacuity: domain too small (only 2 distinct inputs exercised, need 3) — 'no divergence' would be near-vacuous
· 1 grounded finding(s) anchored inline below; 1 more finding(s) on lines outside this diff (see the check run).
| return after | ||
|
|
||
|
|
||
| def _repoint_python_package_imports(paths, all_nodes, all_edges, root) -> None: |
There was a problem hiding this comment.
_repoint_python_package_imports()
fans out to 7 callees (efferent coupling).
Grounded coupling-delta finding (deterministic), not an LLM guess.
… fixes #4933 (python bare-call), #4931 (go method-value via field) and #4932 (go var-init type refs) were merged on GitHub but each changes AST-cached output and deferred its schema bump; one combined 33->34 bump so cached Python/Go extractions re-run. #4929 (namespace packages) is a post-cache pass and needs none. Opens the unreleased 0.9.101 changelog. Co-Authored-By: Claude Opus 4.8 <[email protected]>
What does this PR do?
On v8, a Python monorepo of PEP 420 namespace distributions loses every import that crosses from one distribution to another. Each distribution has its own
src/, and no directory has an__init__.py.On v8 (
f5155bad), this gives:imports_fromdangles atairflow_sql_hooks_sqlinstead of reachingsql.py.PrestoHookgets no resolved base, so the inheritedself.strip_sql_string()call has nocallsedge.free_fn()is only anINFERREDname match.Add
__init__.pyto the target's directories and both calls becomeEXTRACTED. So this is a namespace-package gap, not a missing feature.Why it happens
_repoint_python_package_importsmaps a dotted module id (airflow_sql_hooks_sql) to the real file node. To find the module path, it walks up the contiguous run of__init__.pydirectories above the file. A namespace package has none, so the walk stops at once (levels == 0) and the file is skipped (# not inside a package (namespace pkg / loose module))._resolve_python_module_pathdoesn't recover it either: it probes the scan root and the importing file's own ancestors, and another distribution'ssrc/is neither.The change
When no
__init__.pyis found, the nearestsrc/ancestor is taken as the import root.pkgsql/src/airflow/sql/hooks/sql.pythen aliases toairflow.sql.hooks.sql, the same module path Python would import it by. Three guards:src/roots still stays unresolved, through the existing single-claimant guard (test_ambiguous_namespace_submodule_does_not_bind_callsand a new test cover it).src/(src/helpers.py) is not aliased, since a bareimport helpersmay name a third-party package.src/logging/...) is not aliased (Import builder resolves bare to a same-stem local module (captures stdlib) #4261).Files with an
__init__.pychain take the old path unchanged.Related: #2174 is a broader rework of Python import resolution in monorepos, including namespace packages. This is a small, separate fix for the one case above. #2280 and #3272 are different import forms.
Type of change
Verification & Invariants
The invariant: a Python absolute import whose module exists in the corpus, at exactly one path, is linked to that file whatever the package layout. Ambiguous module paths are never guessed. Nothing persisted changes shape; graphs built from these layouts gain the missing
imports_fromandcallsedges.src/directory are still not aliased.How was this tested?
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (No skill sources changed.)