Skip to content

fix(python): count a subscript annotation's value once, not as its own generic arg - #5113

Closed
rohit-jsfreaky wants to merge 1 commit into
Graphify-Labs:v8from
rohit-jsfreaky:fix/py-generic-ref-identity
Closed

rohit-jsfreaky wants to merge 1 commit into
Graphify-Labs:v8from
rohit-jsfreaky:fix/py-generic-ref-identity

Conversation

@rohit-jsfreaky

Copy link
Copy Markdown
Contributor

Closes #5110

What does this PR do?

In _python_collect_type_refs, the subscript branch compared tree-sitter nodes with is. py-tree-sitter returns a new Node object on every access, so c is value never matched and the value was walked a second time as one of its own generic arguments. Now it uses ==.

graph.json keeps one edge per node pair, so on v8 the false generic_arg reference replaced the correct one. With the fix the right context comes back:

repo edges changed v8 -> this PR
click 39 generic_arg -> 34 parameter_type, 5 return_type
flask 21 generic_arg -> 17 parameter_type, 4 return_type
requests 6 generic_arg -> parameter_type / return_type
django 0 (almost no annotations)

The edge count doesn't change; only the context of these edges does. _AST_CACHE_SCHEMA goes 42 -> 43 so cached Python extractions re-run.

Diff: engine.py +4/-1, cache.py +1/-1, one test file.

Other is comparisons between tree-sitter nodes

I checked every is between nodes in graphify/:

  • Same bug, not fixed here: engine.py, the PHP route-prefix loop (sibling is curr). curr comes from .parent and sibling from .children, so it never matches, and the loop can pick a path string from after the closure. That's a different language, so it should be a separate PR.
  • Not bugs (both sides are the same Python object, from one stack or list): engine.py 4 places, objc.py 2, solidity.py 1.

Type of change

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

Verification & Invariants

Invariant: a Python annotation's references are each emitted once with the right context. Persisted state: the AST cache is bumped (42 -> 43), so cached Python extractions re-run.

  • 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.

How was this tested?

uv run --frozen pytest tests/test_python_subscript_type_refs.py -q   # 3 passed; on v8: 3 failed
PYTHONPATH="$PWD" uv run --frozen pytest tests -q                    # 0 failed
uv run --frozen ruff check .                                         # clean
pyright graphify/extractors/engine.py graphify/cache.py              # no new errors

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments.
  • 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.

…n generic arg

`_python_collect_type_refs` walks a `subscript` annotation's value, then
skips it among the children with `c is value`. py-tree-sitter returns a
new Node object on every access, so `is` never matched and the value was
walked a second time as a generic argument. `def f(x: mod.MyBox[Foo])`
collected `MyBox` as both `type` and `generic_arg`, adding a false
`references` edge with `context: generic_arg` next to the real
`parameter_type` / `return_type` one. Compare with `==`, which
py-tree-sitter defines on node identity in the tree.

Bump _AST_CACHE_SCHEMA to 43 so cached Python extractions re-run.

Co-Authored-By: Claude <[email protected]>
@rohit-jsfreaky

Copy link
Copy Markdown
Contributor Author

Note: my open #5052 also bumps _AST_CACHE_SCHEMA 42 -> 43 (both change Python output). Whichever lands second needs 44; I'll rebase that one.

@rohit-jsfreaky

Copy link
Copy Markdown
Contributor Author

The red test jobs on the first run were one watch-timing test on Python 3.13 (tests/test_language_override_lifecycle.py, graph.json not written yet when the observer test checked). The other Python versions were cancelled by fail-fast, not failed. This PR doesn't touch watch code, and that file fails the same way on v8 under load. I closed and reopened to re-run CI.

@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: \_python\_collect\_type\_refs (vacuous: never exercised).


Graphify review — findings

Bumps _AST_CACHE_SCHEMA so cached extractions re-run under revised extractor semantics, and adjusts how the engine resolves receiver types across languages. Python and JS now check whether a locally bound or parameter name shadows a receiver, and Java, TS, Kotlin, Swift, Ruby and C# receiver typing and argument counting are updated. On the cache side, the semantic cache normalizes paths, checks stat-signature freshness, and handles edges that would dangle when saving.

No blocking issues surfaced.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 4346 functions depend on the 509 functions this change touches.

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

  • extract() — 1174 callers, 71 callees (high)
  • _rebuild_code() — 268 callers, 74 callees (high)
  • detect() — 154 callers, 19 callees (high)
  • dispatch_command() — 8 callers, 148 callees (high)
  • _extract_generic() — 20 callers, 54 callees (high)
  • extract_js() — 101 callers, 8 callees (high)
  • load_cached() — 72 callers, 10 callees (high)
  • save_semantic_cache() — 67 callers, 9 callees (high)
  • …and 2 more

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

Test selection

Test selection

93 of 542 test file(s) selected (17%) via static blast radius.

  • tests/test_4992_ts_import_types.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_binary_media_words.py — impact
  • tests/test_build.py — impact
  • tests/test_cache.py — impact
  • tests/test_cache_long_paths.py — impact
  • tests/test_cache_stale_import_target.py — impact
  • tests/test_cache_windows_case_4925.py — impact
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_code_only_scope_4923.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_csharp_extension_calls.py — impact
  • tests/test_csharp_extension_metadata.py — impact
  • tests/test_csharp_extension_resolution.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_ext_tagged_id_clash.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cache_location.py — impact
  • tests/test_extract_cli.py — impact
  • tests/test_extract_php_closures.py — impact
  • tests/test_grammar_cache_key.py — impact
  • tests/test_hook_content_freshness.py — impact
  • tests/test_ignore_file_encoding.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_incremental_mtime_collision.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_java_member_calls.py — impact
  • tests/test_js_exported_scalar_bindings.py — impact
  • tests/test_js_member_collisions.py — impact
  • tests/test_language_override_lifecycle.py — impact
  • tests/test_language_overrides.py — impact
  • tests/test_languages.py — impact
  • tests/test_linked_worktree_reconcile.py — impact
  • tests/test_llm_backends.py — impact
  • tests/test_manifest_ingest.py — impact
  • tests/test_msbuild_dir_evidence.py — impact
  • tests/test_multilang.py — impact
  • tests/test_objc_field_table_remap.py — impact
  • tests/test_out_dir_evidence.py — impact
  • tests/test_partial_cache.py — impact
  • tests/test_php_unparsed_casts.py — impact
  • … and 43 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 \_python\_collect\_type\_refs.

The verifier did not have enough to check \_python\_collect\_type\_refs, 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: not verifiable: all 234 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly AttributeError — names the real obstacle, not a sampling gap)

safishamsi added a commit that referenced this pull request Nov 10, 2026
… trio

#5113 (Python subscript generic-arg count) and #5117/#5118/#5115 (C++ decl/def
reconcile, scoped aliases + inherited receivers, template ownership + guarded
initializers) all change per-file extractor output and land in one window, so
they share a single bump to 43 chained over prior (42).

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

Copy link
Copy Markdown
Member

Landed on v8 and shipped in 0.9.106 (now on PyPI). I cherry-picked your commit with -x so it keeps your authorship. Thanks for counting a dotted Python subscript annotation's value once, @rohit-jsfreaky.

@safishamsi safishamsi closed this Nov 10, 2026
safishamsi pushed a commit that referenced this pull request Nov 11, 2026
_php_get_route_name reads a route's path (or a group's prefix) from the
string arguments before the closure, and stops at the closure's argument
with `sibling is curr`. py-tree-sitter hands back a new Node object on every
access, so `is` never matched: the scan ran past the closure and took a
string argument after it. `$app->get(function () {...}, '/late')` became a
`GET /late` route, and `$app->group(function ($g) { $g->get('/users', ...); },
'/late')` labelled the inner route `GET /late/users`. Compare with `==`,
which py-tree-sitter defines on the node's identity in the tree. Same class
of bug as #5113.

Bump _AST_CACHE_SCHEMA to 44 so cached PHP extractions re-run.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit c3cf5d3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Python mod.Box[T] annotation gets a false generic_arg edge that replaces the parameter_type one ('is' on tree-sitter nodes)

2 participants