[Bug]: Python: a class used in a type annotation turns calls to its constructor into no edge at all (0.9.62+)
What happened?
Since 0.9.62, calling a project class whose name also appears in a type annotation in the same file drops the
calls edge to its constructor. 0.9.61 and older resolve it. Still reproduces on 0.9.70 and on v8@4c21b15e9b.
Two shapes, same root:
-
A. annotation in another function: the annotation leaves a sourceless stub node
Parserin the file. Inextractors/engine.pythe in-file resolution bindsParser()to that stub. Then neither branch runs, so the call is emitted neither as an edge nor as a raw call:external_stub_target = bool(tgt_nid and not nid_to_sf.get(tgt_nid)) if tgt_nid and not external_stub_target: ... # emits calls elif callee_name and not tgt_nid: ... # raw_call for cross-file resolutionBefore #3156 the edge to the stub was emitted and later remapped to the real class.
-
B. annotation in the same function (
def detect(self) -> Parser: return Parser()): even when the call reaches the cross-file pass, it is skipped because the pair already has the annotation edge:if tgt != caller and (caller, tgt) not in existing_pairs:(extract.py). Before, the in-filecallsand the genericreferencescollapsed in build (_GENERIC_RELATIONS, specific beats generic).
Steps to reproduce
mkdir -p repro/app/engines/transcript && cd repro
touch app/engines/__init__.py app/engines/transcript/__init__.py
cat > app/engines/transcript/parser.py <<'EOF'
class Parser:
def feed(self, line):
return line
EOF
cat > app/engines/engine.py <<'EOF'
from typing import Optional
from engines.transcript.parser import Parser
class Engine:
def detect(self): # shape A
return Parser()
def get_parser(self) -> Optional[Parser]:
return None
def make(self) -> Parser: # shape B
return Parser()
EOF
git init -q . && graphify update .
Error output or graph output
edges from app/engines/engine.py
0.9.57, 0.9.61: calls .detect()->Parser; calls .make()->Parser; references .get_parser()->Parser
0.9.62, 0.9.70: references .get_parser()->Parser; references .make()->Parser <- both calls lost
0.9.70 + shape-A fix: calls .detect()->Parser; references .get_parser(); references .make()
0.9.70 + shape-A + B fixes: calls .detect()->Parser; calls .make()->Parser; references .get_parser()
controls: no annotation -> calls on every version;
external class (fastapi.HTTPException) annotated + called -> only references on 0.9.62+ (the #3156
intent), unchanged by the patches below
Suggested fix, measured on 0.9.70:
-
Shape A, one line in
extractors/engine.py: let a call bound to a sourceless stub fall through to cross-file resolution, where the #3156 guard still drops truly external targets:- elif callee_name and not tgt_nid: + elif callee_name and (not tgt_nid or external_stub_target):Restores shape A (12 of 48 lost constructor edges in our repo). External-class control unchanged.
-
Shape B: a pair that has only generic edges (
references/uses/mentions) should not block acalls, which build then collapses. We tried this (ageneric_only_pairsset checked next toexisting_pairsin the direct-call branch). It recovers the other 36, but it is wider than the regression: it also turns 223 long-standinguses/referencespairs intocalls. We checked all of them by AST and each one is a real call site, so it looks correct, but it is a behavior change beyond 0.9.61. Leaving that decision to you.
Version: 0.9.62 → 0.9.70 (last good: 0.9.61) · OS: macOS · Python: 3.11 · Install: uv (clean fixture, no LLM backend)
Additional context: introduced alongside #3156 (0.9.62). Not covered by #3405 or #2447.
Thanks for opening this issue, @wpoPR. A maintainer will take a look soon.
If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.
To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.
C# is affected by the same regression. For C#, the shape-A line suggested above should not be merged on its own. It recovers the lost constructor edges, but it also brings back almost as many wrong ones: in one C# code base, 202 recovered new X() edges against 187 new edges from new X() to an enum member or property that merely shares the name X. A small guard (diff below) keeps the recovery and removes almost all of those member bindings, including most of the ones 0.9.70 already has (359 → 14). Details and numbers below.
Repro (C#, four files, nothing else needed)
Lib/Widget.cs
namespace Demo
{
public class Widget
{
}
}
Lib/Kinds.cs
namespace Demo
{
public enum ViewKind { List, Grid }
}
App/Factory.cs
using System.Collections.Generic;
namespace Demo
{
public class Registry
{
public List<Widget> Items;
}
public class Factory
{
public object MakeA() { return new Widget(); }
public Widget MakeB() { return new Widget(); }
public object MakeList() { return new List<int>(); }
}
}
App/Plain.cs (control: Widget is not used as a type in this file)
namespace Demo
{
public class Plain
{
public object Make() { return new Widget(); }
}
}
graphify extract src --code-only, then the calls/references edges leaving the four methods:
0.9.61 0.9.62 / 0.9.70 0.9.70 + shape-A line 0.9.70 + line + guard
.Make() -> calls Widget calls Widget calls Widget calls Widget
.MakeA() -> calls Widget (no edge) calls Widget calls Widget
.MakeB() -> calls Widget references Widget references Widget references Widget
.MakeList() -> calls List (no edge) calls List (no edge)
MakeAis shape A:Widgetappears as a type argument of a field elsewhere in the same file, sonew Widget()binds to the sourceless stub and is dropped.Plain.Make()is the control and keeps its edge on every version.MakeBis shape B (the return type isWidget).- The
Listin theMakeListrows is notSystem.Collections.Generic.List<T>. It is the enum memberViewKind.List(nodelib_kinds_demo_viewkind_list, the target of acase_ofedge). 0.9.61 had this wrong binding; since 0.9.62 it is gone, because the call is dropped along with the stub. The suggested line restores shape A and restores this wrong binding. The guard below keeps the first and prevents the second.
The guard, as a unified diff against the 0.9.70 release. It contains the suggested shape-A line plus two small additions: tag a C# object creation in its raw call, and in the shared cross-file pass drop candidates that are the target of a case_of or defines edge. That is the same kind of exclusion #3815 added to the C# type-definition index in 0.9.68.
--- a/graphify/extractors/engine.py
+++ b/graphify/extractors/engine.py
@@ -6615,7 +6615,7 @@
"source_location": f"L{line}",
"weight": 1.0,
})
- elif callee_name and not tgt_nid:
+ elif callee_name and (not tgt_nid or external_stub_target):
# In Python, if an unqualified call names a local non-callable variable or parameter,
# do NOT append it to raw_calls (#3405 Part 3/4).
is_py_local_data = (
@@ -6653,6 +6653,8 @@
# class fields/properties are the base scope.
if config.ts_module == "tree_sitter_c_sharp":
rc_entry["lang"] = "csharp"
+ if node.type == "object_creation_expression":
+ rc_entry["csharp_new"] = True
if csharp_qualified_prefix:
rc_entry["qualified_prefix"] = csharp_qualified_prefix
receiver_type = _csharp_scoped_receiver_type(
--- a/graphify/extract.py
+++ b/graphify/extract.py
@@ -8233,6 +8233,9 @@
# of these files with no import evidence is gated below (#1659).
_JS_TS_CALL_SUFFIXES = (".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs")
_go_module_cache: dict[Path, str | None] = {}
+ _csharp_member_nids = {
+ e.get("target") for e in all_edges if e.get("relation") in ("case_of", "defines")
+ }
for rc in all_raw_calls:
if rc.get("_ambiguous_python_import"):
continue
@@ -8281,6 +8284,10 @@
candidates = global_label_to_nids_ci.get(callee.lower(), [])
if not candidates:
continue
+ if rc.get("csharp_new"):
+ candidates = [c for c in candidates if c not in _csharp_member_nids]
+ if not candidates:
+ continue
# Cross-language guard: never bind a call to a definition in a different
# language family. Name-only matching was resolving a TSX callback passed
# by name to a same-named Kotlin method in the Android half of the repo
Size in one real C# code base (about 2,300 .cs files). Unpatched releases, same source tree, raw graph.json, counted as distinct (caller, target) pairs. Every counted site was matched against its source line (new <Name> at the reported line):
| 0.9.61 | 0.9.70 | 0.9.70 + line | 0.9.70 + guard | 0.9.70 + line + guard | |
|---|---|---|---|---|---|
new X() → class defined in the corpus, calls |
1,688 | 1,380 | 1,582 | 1,519 | 1,789 |
| of the 1,688: no edge at all (shape A) | – | 179 | 7 | 179 | 7 |
of the 1,688: references only (shape B) |
– | 129 | 129 | 129 | 129 |
new X() → enum member or property with the same name as X |
502 | 359 | 546 | 14 | 14 |
- The line alone recovers 172 of the 179 shape-A losses, plus 30
new X()sites that had no edge in 0.9.61 either, but adds 187 wrong member pairs compared with 0.9.70. 143 of the 187 existed in 0.9.61 and disappeared in 0.9.62 (also measured on 0.9.62), so the line brings back the part of an older mis-binding that the #3531 change had removed as a side effect. In 156 of the 187 the name belongs only to a framework type (new List<T>()and the like); the corpus uses it for an enum member or a property but has no class of that name. - The guard alone removes 345 of the 359 member pairs that 0.9.70 still has, and nothing else: every
callspair it removes is one of them. It also adds 139new X()→ class pairs, where a class shared its name with a member and was therefore an ambiguous candidate before. The 14 member pairs left are all bound within a single file, i.e. by the in-file lookup, which the guard does not touch. - Line + guard against 0.9.70: 345
callspairs removed (all of them member bindings), 412 added (409new X()→ class, 3 method calls). For 38 of the 409 the corpus has more than one class of that name; we did not check whether the existing tie-breaker picked the right one.
Request for C#: please let the shape-A change land only together with a guard like this. Without it, a C# graph trades one error for another. We have not run the test suite with the guard.
Measured on 0.9.61, 0.9.62 and 0.9.70 (latest release), Windows 11, Python 3.14, installed with uv, --code-only, no LLM backend.