Skip to content

fix(python): resolve imports across PEP 420 namespace packages under src/ - #4929

Merged
safishamsi merged 1 commit into
Graphify-Labs:v8from
nothariharan:fix/python-pep420-src-namespace
Nov 1, 2026
Merged

safishamsi merged 1 commit into
Graphify-Labs:v8from
nothariharan:fix/python-pep420-src-namespace

Conversation

@nothariharan

Copy link
Copy Markdown
Contributor

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.

pkgsql/src/airflow/sql/hooks/sql.py          class DbApiHook: def strip_sql_string(self, sql) ...
                                             def free_fn() ...
pkgpresto/src/airflow/presto/hooks/presto.py from airflow.sql.hooks.sql import DbApiHook, free_fn
                                             class PrestoHook(DbApiHook):
                                                 def get_records(self, sql):
                                                     free_fn()
                                                     return self.strip_sql_string(sql)

On v8 (f5155bad), this gives:

  • imports_from dangles at airflow_sql_hooks_sql instead of reaching sql.py.
  • PrestoHook gets no resolved base, so the inherited self.strip_sql_string() call has no calls edge.
  • free_fn() is only an INFERRED name match.

Add __init__.py to the target's directories and both calls become EXTRACTED. So this is a namespace-package gap, not a missing feature.

Why it happens

_repoint_python_package_imports maps 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__.py directories 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_path doesn't recover it either: it probes the scan root and the importing file's own ancestors, and another distribution's src/ is neither.

The change

When no __init__.py is found, the nearest src/ ancestor is taken as the import root. pkgsql/src/airflow/sql/hooks/sql.py then aliases to airflow.sql.hooks.sql, the same module path Python would import it by. Three guards:

  • Ambiguity: a module path claimed by two src/ roots still stays unresolved, through the existing single-claimant guard (test_ambiguous_namespace_submodule_does_not_bind_calls and a new test cover it).
  • Loose modules: a module directly under src/ (src/helpers.py) is not aliased, since a bare import helpers may name a third-party package.
  • Stdlib names: a namespace named like a stdlib module (src/logging/...) is not aliased (Import builder resolves bare to a same-stem local module (captures stdlib) #4261).

Files with an __init__.py chain 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

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

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_from and calls edges.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly. Namespace packages that are not under a src/ directory are still not aliased.

How was this tested?

uv run --frozen pytest tests/test_src_layout_import_resolution.py -q
  12 passed (3 new)
New headline test with the extract.py change reverted:
  FAILED ... AssertionError: {('.get_records()', 'free_fn()', 'INFERRED')}
Full suite on Linux (WSL), isolated HOME:
  uv sync --all-extras --frozen && uv run --frozen pytest tests/ -q --tb=short
  9364 passed, 28 skipped, 1 xfailed, 1 failed:
  test_install_references.py::test_install_skill_references_ignores_precreated_predictable_tmp_symlink_to_dir
  It fails only in the full run. Run alone, it passes on both this branch and f5155bad (it reuses a predictable /tmp path).
uv run --frozen python -m tools.skillgen --check   ok
uv run --frozen bandit -r graphify -ll             same findings as f5155bad (4 high, 8 medium), none in the changed lines

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (No skill sources changed.)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

…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.
@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 1, 2026
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

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.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 — impact
  • tests/test_affected_overrides.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_blade_extractor.py — impact
  • tests/test_build.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_c_include_resolution.py — impact
  • tests/test_cache.py — impact
  • tests/test_case_only_decl_ids_4904.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cobol_extractor.py — impact
  • tests/test_confidence_score_output.py — impact
  • tests/test_cpp_method_declarations.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cpp_template_type_params.py — impact
  • tests/test_cpp_type_alias.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_language_call_resolution.py — impact
  • tests/test_cross_repo_external_call_guards.py — impact
  • tests/test_cross_repo_member_calls.py — impact
  • tests/test_csharp_call_site_generic_args.py — impact
  • tests/test_csharp_delegate_declarations.py — impact
  • tests/test_csharp_enum_members.py — impact
  • tests/test_csharp_field_generic_args.py — impact
  • tests/test_csharp_foreach_loop_variable.py — impact
  • tests/test_csharp_generic_callsites.py — impact
  • tests/test_csharp_interface_dispatch.py — impact
  • tests/test_csharp_member_calls.py — impact
  • tests/test_csharp_member_chain_calls.py — impact
  • tests/test_csharp_member_nodes.py — impact
  • tests/test_csharp_method_owner_labels.py — impact
  • tests/test_csharp_object_creation.py — impact
  • tests/test_csharp_overloads.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_csharp_tuple_type_refs.py — impact
  • tests/test_csharp_type_resolution.py — impact
  • tests/test_csharp_underscore_collisions.py — impact
  • tests/test_csharp_var_call_return_type.py — impact
  • tests/test_csharp_verbatim_interpolation.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_duplicate_annotation_edges.py — impact
  • tests/test_edge_also_seen.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_elixir_defdelegate.py — impact
  • tests/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).

Comment thread graphify/extract.py
return after


def _repoint_python_package_imports(paths, all_nodes, all_edges, root) -> None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Health regression — _repoint_python_package_imports()

fans out to 7 callees (efferent coupling).

Grounded coupling-delta finding (deterministic), not an LLM guess.

@nothariharan nothariharan reopened this Nov 1, 2026
@nothariharan nothariharan reopened this Nov 1, 2026
@safishamsi
safishamsi merged commit 2fcbdc0 into Graphify-Labs:v8 Nov 1, 2026
21 of 26 checks passed
safishamsi added a commit that referenced this pull request Nov 1, 2026
… 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants