Skip to content

fix(extract): walk Scala type_definition right-hand sides (#2049) - #4037

Closed
Faisal-Fayaz wants to merge 2 commits into
Graphify-Labs:v8from
Faisal-Fayaz:fix/scala-type-definition-refs
Closed

Faisal-Fayaz wants to merge 2 commits into
Graphify-Labs:v8from
Faisal-Fayaz:fix/scala-type-definition-refs

Conversation

@Faisal-Fayaz

Copy link
Copy Markdown
Contributor

Stacked on #4036. This branch is #4036 plus one commit. The reviewable unit is
the top commit (879dfd3); the commit below it is #4036 verbatim and is not
part of this change. This PR targets v8 only because GitHub cannot base a PR on
a fork branch. If you would rather review the combined diff, the net effect of
this commit alone is: widen one dispatch tuple by one node type, and add two node
types to one walker whitelist.

Problem

A type alias (type Alias = List[Int]), an opaque type, and a match type all declare a dependency on the types they name — but the type-reference dispatch only fired for val_definition/var_definition, so these produced zero references edges of any kind, in any context. That is the asymmetry with val/var, whose annotation is walked:

trait Repo:
  type Alias = List[Int]
  opaque type Id = Long
  type Elem[X] = X match
    case String => Char

match_type needed a second, independent change: the type-reference walker's own recursion whitelist did not know match_type/type_case_clause, so even a correctly dispatched call on a match type's right-hand side would still resolve nothing.

Implementation

Two small changes:

  1. Widen the dispatch tuple to include type_definition. I verified the RHS field name against tree-sitter-scala 0.26.0 before writing the fix — all three forms carry it under the same type field the val/var path already reads, so the existing walk applies verbatim with no new handler:

    type_definition  fields=[(type_identifier, name), (generic_type, type)]   # type Alias = List[Int]
    type_definition  fields=[(type_identifier, name), (type_identifier, type)]  # opaque type Id = Long
    type_definition  fields=[(type_identifier, name), (type_parameters, ...), (match_type, type)]
    
  2. Add match_type/type_case_clause to the walker whitelist, recursing over named children exactly as compound_type is already handled. A case clause names two real types — the one it discriminates on and the one it yields — and both are collected.

File-scope aliases inherit the anchor fallback from #4036; without it, an alias at file scope is still dropped by the separate missing-container defect that PR fixes.

Scope

Edges only. The alias itself still gets no node of its own. That is a separate, already-accepted tradeoff shared with val/given bindings, and I deliberately left it alone rather than widening this PR. No new node type, no config change.

Verification

uv run pytest tests/test_scala_type_definition.py -q     # 10 passed
uv run pytest tests/ -q                                   # 28 failed, 6154 passed, 101 skipped
uv run ruff check .                                       # All checks passed!
uv run pyright                                            # 635 errors
  • 8 of the 10 new tests fail on the pre-fix tree (confirmed by stashing the engine.py change and re-running).
  • The 28 failures are byte-identical to the clean-tree baseline (28 failed, 6136 passed), diffed test-id-by-test-id. They are missing tree-sitter grammars for erlang/R/solidity/vbnet plus ollama retry tests; none touch Scala.
  • pyright is at parity: 635 errors before and after — pre-existing debt in this repo.

Tests cover all three alias forms in-trait and at file scope, the generic_type argument split, the match_type result and matched type, that the shared val context split is preserved rather than flattened, EXTRACTED tagging with a source location, and the self-edge guard.

🤖 Generated with Claude Code

Faisal-Fayaz and others added 2 commits October 3, 2026 19:21
…-Labs#2054)

Scala 3 allows `val`/`var` to be written directly at file scope with no
enclosing object/class/trait. The `val_definition`/`var_definition`
type-reference dispatch was gated on `and parent_class_nid`, so a file-scope
binding produced no node and no edge of any kind, while the byte-identical
definition nested one level inside any container resolved correctly. The
missing container was the only difference: both positions parse to the same
node type carrying the same `type` field.

This is the Scala instance of a generic defect in the walker's default
recursion path that had already caused and been fixed once for Python
(Graphify-Labs#1050) -- a position that legitimately has no container must fall back to
the file node rather than be skipped. The fix reuses the `owner_nid =
parent_class_nid or file_nid` idiom already present in the property branch
above, and moves the self-reference guard onto the same anchor so a
file-scope binding cannot mint an edge from the file node to itself.

Behaviour is unchanged for class-nested bindings; a regression test pins
that case explicitly.

Verified:
- uv run pytest tests/test_scala_top_level_binding.py -q  (8 passed)
- 6 of the 8 new tests fail on the pre-fix tree, 2 pass either way
  (the in-class control, and the self-edge guard which is trivially empty
  before the fix and meaningful after it)
- uv run pytest tests/ -q -> 28 failed, 6144 passed, 101 skipped; the 28
  failures are byte-identical to the clean-tree baseline (missing
  tree-sitter grammars for erlang/R/solidity/vbnet, plus ollama retry
  tests) and none touch Scala
- uv run ruff check . -> clean
- uv run pyright -> 635 errors, identical to the clean-tree baseline
  (pre-existing debt; this change adds none)

Co-Authored-By: Claude <[email protected]>
…abs#2049)

A type alias (`type Alias = List[Int]`), an `opaque type`, and a `match type`
all declare a dependency on the types they name, but the type-reference
dispatch only fired for `val_definition`/`var_definition`, so these produced
zero `references` edges in any context -- in contrast to a `val`/`var`, whose
annotation is walked.

`match_type` needed a second, independent change: the type-reference walker's
recursion whitelist did not know `match_type`/`type_case_clause`, so even a
correctly dispatched call on a match type's right-hand side would have
resolved nothing. Both are now handled, recursing over named children exactly
as `compound_type` already is.

Verified field names against tree-sitter-scala 0.26.0 before writing the fix:
`type_definition` carries its RHS under the `type` field for all three forms
(`generic_type`, `type_identifier`, `match_type`), so the existing
`child_by_field_name("type")` walk applies verbatim and no new handler is
needed. File-scope aliases inherit the Graphify-Labs#2054 anchor fallback.

Scope: only the edges are added. The alias still gets no node of its own, which
is a separate already-accepted tradeoff shared with `val`/`given` bindings.

Verified:
- uv run pytest tests/test_scala_type_definition.py -q  (10 passed)
- 8 of the 10 new tests fail on the pre-fix tree
- uv run pytest tests/ -q -> 28 failed, 6154 passed, 101 skipped; the 28
  failures are byte-identical to the clean-tree baseline and none touch Scala
- uv run ruff check . -> clean
- uv run pyright -> 635 errors, identical to the clean-tree baseline

Co-Authored-By: Claude <[email protected]>
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown

Thanks for the pull request, @Faisal-Fayaz. 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 1 advisory finding(s) below merit a look before merge.

Formal verification. PR-changed functions: 0/2 verified (0 proven, 0 may-equivalent, 0 distinguished) · 2 not verified (1 vacuous, 1 unsupported).

Not verified on this run: \_extract\_generic (unsupported), \_scala\_collect\_type\_refs (vacuous: never exercised).


Graphify review — findings

Fixes Scala type-reference extraction so file-scope val/var bindings (Scala 3's wrapper-less style) emit references edges owned by the file node instead of vanishing from the graph. type_definition now gets the same treatment, so type aliases, opaque types and match types link to the types on their right-hand side, though the alias itself still gets no node. _scala_collect_type_refs now recurses into match_type/type_case_clause, so both the matched and the yielded type in each case are picked up.

Worth a look

  • Scala type alias type parameters are emitted as spurious reference targets — graphify/extractors/engine.py:5249 · 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 — 754 functions depend on the 274 functions this change touches.

Health — this change adds coupling hotspots:

  • new: _extract_generic() — 18 callers, 30 callees
  • new: extract_js() — 87 callers, 4 callees
  • new: extract_xaml() — 19 callers, 17 callees
  • new: extract_objc() — 27 callers, 9 callees
  • new: extract_julia() — 19 callers, 7 callees
  • new: extract_cpp() — 31 callers, 3 callees
  • new: extract_vue() — 10 callers, 7 callees
  • new: walk() — 1 callers, 67 callees
  • …and 9 more — each is listed as a finding

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

Test selection

Test selection

27 of 320 test file(s) selected (8%) 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_python_underscore_resolution.py — impact
  • tests/test_rationale.py — impact
  • tests/test_ruby_resolution.py — impact
  • tests/test_scala_self_type.py — impact
  • tests/test_scala_top_level_binding.py — impact, changed-test
  • tests/test_scala_type_definition.py — impact, changed-test
  • 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)

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

Could not verify: Could not verify \_scala\_collect\_type\_refs.

The verifier did not have enough to check \_scala\_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 200 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)

· 17 more finding(s) on lines outside this diff (see the check run).

@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.75 (live on PyPI as graphifyy==0.9.75). Landed on v8 via an authorship-preserving cherry-pick, so your original commit authorship is kept. Thanks @Faisal-Fayaz for Scala type_definition RHS walking 🙏

Closing as shipped.

@safishamsi safishamsi closed this Oct 4, 2026
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.

2 participants