fix(extract): emit inherits edge for Python parameterized bases#1815
Open
Synvoya wants to merge 1 commit into
Open
fix(extract): emit inherits edge for Python parameterized bases#1815Synvoya wants to merge 1 commit into
Synvoya wants to merge 1 commit into
Conversation
The Python heritage handler in _extract_generic only matched bare `identifier` bases in a class's `superclasses` list. A parameterized base -- `class Repo(Base[int])`, `class C(Generic[T])` -- parses as a `subscript` node whose `value` field is the actual base type and whose `[...]` holds only type parameters. That node type fell through the identifier-only check, so every generic base silently lost its `inherits` edge. Unwrap a `subscript` base to its `value` before the identifier check, so parameterized bases point their heritage edge at the base type. Bare-identifier bases are unaffected. Qualified (`attribute`) bases remain handled by the cross-file symbol resolver and are out of scope here. Regression test in tests/test_languages.py fails before, passes after.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
A Python class whose base is parameterized loses its heritage edge.
class Repo(Base[int])andclass Handler(Generic[T])emit noinheritsedge at all, so every generic base —Generic[T],Sequence[T],Mapping[K, V],Repository[User],list[str]— isdropped from the graph.
Root cause
The Python branch of
_extract_generic(graphify/extractors/engine.py)iterates the
superclasseslist and only handles bases whose node type isidentifier:A parameterized base does not parse as an
identifier. tree-sitter-pythonwraps it in a
subscriptnode whosevaluefield is the real base type andwhose
[...]holds only the type parameters:The
subscriptnode never matched theidentifiercheck, so the base fellthrough and the edge was silently dropped.
Minimal repro
extract_pythonbefore this change yields only the bare-identifier edges;every
subscriptbase is missing.Fix
Unwrap a
subscriptbase to itsvaluefield before the identifier check:class C(Base)) are unaffected.class C(A, B[T])) nowkeep both edges.
attribute) bases such aspkg.mod.Baseare intentionally leftout of scope here — those are the domain of the cross-file symbol resolver
(issue AST extractor misses cross-file inheritance and produces ghost-duplicate nodes for cross-file type references #1186 / PR Fix cross-file inheritance and ghost type references in AST extractor #1231), which resolves an imported base name back to its
canonical cross-file definition. This change is confined to node-type
coverage in the immediate AST-traversal path and does not touch that
pipeline.
Test
tests/test_languages.py::test_python_parameterized_base_inherits_edgeasserts
Repo -> BaseandMulti -> Base(subscript bases) plusMulti -> Mixin(bare-identifier regression guard).inherits == {('Multi', 'Mixin')}; both subscriptbases dropped.
Full
tests/test_languages.pysuite: 315 passed, 13 skipped (unchanged;skips are the optional
tree-sitter-dmextra).