Skip to content

fix(python): keep file node when a class is type-hinted before it's defined (#4334) - #4343

Closed
Mpasha17 wants to merge 2 commits into
Graphify-Labs:v8from
Mpasha17:fix/4334-forward-ref-file-id
Closed

Mpasha17 wants to merge 2 commits into
Graphify-Labs:v8from
Mpasha17:fix/4334-forward-ref-file-id

Conversation

@Mpasha17

@Mpasha17 Mpasha17 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4334

What changed

In _extract_generic (graphify/extractors/engine.py), a forward reference like w: Widget before class Widget in widget.py minted a bare widget stub, which is the same id the file node gets once paths are made relative, so the file node got swallowed and Widget ended up "containing" itself and Helper. Now the stub is still emitted exactly as before, but after the walk, if the name turns out to be a class defined in this file, references get rewired to that class and the stub is dropped. Anything else (an imported Widget, or a def widget() with the same id) is left on v8's sourceless-stub path for the cross-file rewire.

How I tested it

  • New tests/test_python_forward_ref_file_id.py: the issue repro (single file + with an importer), pkg/widget.py importing Widget from .base (no dangling edges, use() links to base.Widget), and a def widget() with no class (no reference to the function).
  • Ran the issue's 2-file repro with graphify update . (fresh graphify-out) before and after:
    • before: no widget.py node, Widget contains Widget/Helper, app imports_from Widget, no Helper.make -> draw call
    • after: widget.py node is back and contains both classes, app.py imports_from the file, .make() calls .draw() shows up
  • Also checked live: pkg/widget.py + from .base import Widget and the def widget() case both give the same graph as v8.
  • ruff check clean. Full suite on py3.10/3.12/3.13/3.14: the only failures (test_dm_* and 7 test_security safe_fetch/validate_url tests) fail the same way on v8 too.

One note: in the 2-file repro the file node's id comes out as widget_py_widget rather than widget. That's because app.py's own Widget stub still collides, and v8 already does exactly the same thing even without the forward ref, so I left it alone here.

I used an AI assistant (Grok Bot) to help write this; I reviewed and tested the change myself.

…d-referenced (Graphify-Labs#4334)

A type hint like `w: Widget` before `class Widget` in widget.py minted a
bare 'widget' stub, which collides with the file node id once paths are made
relative, so the file node vanished and Widget ended up containing itself.
Point that reference at the local class id instead.

Co-Authored-By: Grok Bot
@Mpasha17
Mpasha17 requested a review from safishamsi as a code owner October 11, 2026 17:53
@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 11, 2026
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Mpasha17. 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.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 2 advisory finding(s) below merit a look before merge.

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 unsupported).

Not verified on this run: \_extract\_generic (unsupported).


Graphify review — findings

Fixes forward references to a class named after its own file (e.g. Widget in widget.py). These used to emit a cross-file stub whose id collided with the file node and swallowed it. _extract_generic now resolves such a name to the namespaced local id of the class defined later in the same file, so the file node keeps its contains edges and importers still resolve imports_from to the file.

Worth a look

  • File-stem collision always rewrites the reference to a local id, even when the name is imported from another module — graphify/extractors/engine.py:4847 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
  • Forward-ref id built from current namespace_stack instead of the class's actual scope — graphify/extractors/engine.py:4852 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 845 functions depend on the 292 functions this change touches.

Health — grade A; 10 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):

  • _extract_generic() — 19 callers, 35 callees (high)
  • extract_js() — 90 callers, 6 callees (high)
  • extract_xaml() — 19 callers, 17 callees (high)
  • extract_objc() — 25 callers, 10 callees (high)
  • extract_php() — 37 callers, 5 callees (high)
  • extract_cpp() — 36 callers, 5 callees (high)
  • extract_julia() — 19 callers, 8 callees (high)
  • extract_svelte() — 14 callers, 8 callees (high)
  • …and 2 more

Verification — 845 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: 741 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

33 of 368 test file(s) selected (9%) via static blast radius.

  • tests/test_astro_extraction.py — impact
  • tests/test_build.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_php_closures.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_indirect_call_block_scoped_shadow.py — impact
  • tests/test_indirect_dispatch.py — impact
  • tests/test_indirect_dispatch_assign_return.py — impact
  • tests/test_indirect_dispatch_getattr.py — impact
  • tests/test_js_exported_scalar_bindings.py — impact
  • tests/test_languages.py — impact
  • tests/test_multilang.py — impact
  • tests/test_php_unparsed_casts.py — impact
  • tests/test_python_forward_ref_file_id.py — impact, changed-test
  • tests/test_python_inherited_method_calls.py — impact
  • tests/test_python_underscore_resolution.py — impact
  • tests/test_rationale.py — impact
  • tests/test_ruby_resolution.py — impact
  • tests/test_scala_context_bounds.py — impact
  • tests/test_scala_given_instances.py — impact
  • tests/test_scala_self_type.py — impact
  • tests/test_scala_top_level_binding.py — impact
  • tests/test_scala_type_definition.py — impact
  • tests/test_svelte_extraction.py — impact
  • tests/test_swift_computed_properties.py — impact
  • tests/test_swift_protocol_requirements.py — impact
  • tests/test_trailing_newline_not_a_syntax_error.py — impact
  • tests/test_ts_new_expression_calls.py — impact
  • tests/test_typescript_module_extensions.py — impact
  • tests/test_vue_extraction.py — impact

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.

Docs that may be stale (advisory)

…and 10 more.

Formal verification

Could not verify: Could not verify \_extract\_generic.

The verifier did not have enough to check \_extract\_generic, 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: parameter `config` is annotated `LanguageConfig` — outside the synthesizable primitive/collection set

@Mpasha17

Copy link
Copy Markdown
Contributor Author

Checked both live with graphify update, comparing v8 against this branch:

  1. Imported Widget with no local class: v8 drops the widget.py node and other.Widget ends up containing widget.use. With this change the file node is back and use still links to other.Widget through the import, so nothing's lost or mislinked.
  2. Hint inside a nested method/function, or Widget nested as Outer.Widget: still resolves fine, and the file node and containment are right. v8 loses the file node in all of these.

So neither is a regression compared to v8. Happy to tighten it if you've got a case that comes out worse.

@safishamsi

Copy link
Copy Markdown
Member

Thanks @Mpasha17! The single-file case (#4334) is fixed correctly. One thing before this can land: the guard fires on any referenced name whose bare slug equals the file stem — including a same-named class that's imported into the file rather than defined in it. For a file in a subdirectory that does from .base import Widget and only uses Widget, the redirect returns a local qualified id for which no node is ever created (dangling edge) and it suppresses the sourceless stub the cross-file rewire relies on — a regression, and that path is untested.

Could you gate the redirect on the name actually being a local definition (e.g. only redirect when the qualified id ends up in seen_ids at end-of-walk, otherwise fall through to the sourceless stub), and add a test for the imported-same-name-in-a-subdirectory case? Happy to point at the exact spot if useful.

@smngvlkz

Copy link
Copy Markdown
Contributor

I tested this too and hit the same imported-class case. One more for the new check: if there's no Widget class but there is a def widget() in widget.py, make(w: Widget) ends up referencing the function. An id-exists check wouldn't catch it since widget() has the same id, so it might also need to check it's a class

…raphify-Labs#4334)

Keep v8's sourceless stub and resolve after the walk: rewrite to the local
id only when that id is a class defined in this file. An imported name (e.g.
pkg/widget.py doing `from .base import Widget`) or a same-id function like
`def widget()` now falls through to the cross-file rewire exactly as on v8.

Co-Authored-By: Grok Bot
@Mpasha17

Copy link
Copy Markdown
Contributor Author

Thanks for catching this! Changed it the way you suggested: ensure_named_node() now emits the same sourceless stub as v8, and after the walk it only rewires to the local id if that's a class actually defined in the file. Otherwise it's untouched, so pkg/widget.py with from .base import Widget goes through the normal cross-file rewire. Added a test for that (no dangling edges, use() lands on base.Widget). Fwiw I couldn't get a dangling edge out of that case on the previous commit locally, but the gate makes it go through v8's path either way.

Thanks @smngvlkz for the def widget() case too, reproduced it and it's fixed + tested. Full suite on 3.10/3.12/3.13/3.14 has only the failures v8 already has, and the live #4334 repro still keeps the file node.

safishamsi added a commit that referenced this pull request Oct 12, 2026
Python property-accessor decorators and `Self` (#4385, #4386), Swift
`Self` (#4387), Go generic type params (#4388), the Python forward
type-hint file-node fix (#4343), PowerShell dot-source paths (#4369),
and the C# verbatim-interpolated-string recovery (#3612) all change AST
extraction output, so warm caches must re-extract.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.88, just released to PyPI as graphifyy 0.9.88. Landed via an authorship-preserving cherry-pick (your commit keeps your name and counts on your contributor graph), which is why GitHub marks this closed rather than merged. Thanks @Mpasha17!

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.

[Bug]: Python files can disappear from the graph when a class is used before it's defined

3 participants