Skip to content

Python: no receiver-type inference for self.<attr>.<method>() — 33 live methods reported as dead code #2860

Closed

Description

@sai2142

Summary

Python has no receiver-type inference, so self.<attr>.<method>() calls produce no calls edge.
The practical effect is not just missing edges — it is confidently wrong dead-code answers:
on a 104-file / 23k-LOC Python repo, graphify explain reported 33 methods as having zero
callers when they demonstrably have callers
.

Every other receiver language already has this. Python is the gap:

helper in graphify/extractors/engine.py status
_java_method_receiver_types present
_csharp_method_receiver_types present
_ts_receiver_type_table present
_swift_receiver_name present
_python_method_receiver_types missing

I understand why: Java/C#/TS declare field types and Python does not. But in idiomatic Python the
binding is still recoverable from the constructor.

Reproduction

class AgentRunner:
    def bull_researcher(self, idea): ...

class Workflows:
    def __init__(self):
        self.agents = AgentRunner()          # <- the type is stated right here

    def deep_portfolio_review(self):
        return self.agents.bull_researcher(idea)   # <- no edge emitted

graphify explain <AgentRunner.bull_researcher> reports 0 incoming calls edges.

Measured impact (graphify 0.9.46, --code-only, 114 files, 1848 nodes / 4647 edges)

  • Recall on one representative method (Workflows.deep_portfolio_review): 11 of 19 real calls = 58%.
    All 8 misses were attribute-mediated — and in that codebase they were every single agent invocation,
    i.e. the architecturally most important edges present.
  • 33 methods reported as dead code that have callers, including the entire public API surface of one
    class and the repository add() method at the centre of a production crash we were debugging.
  • 36 methods had undercounted caller sets.
  • 180 self.X.y(...) call sites in src/ overall (70 distinct receivers).

Silent under-reporting is the dangerous failure mode here: the tool says "4 callers" with full
confidence when the answer is 12, and says "dead code" about live code.

A working fix

I prototyped a ~200-line post-pass that builds an attr→class table per class from self.x = C(...)
assignments and PEP 526 self.x: C annotations, then re-walks method bodies and emits the missing
edges as INFERRED. Results on the same repo:

  • recovered 61 edges
  • 61/61 precision — every recovered edge verified twice: the call site exists at the reported
    line, and the target lands on the real def
  • recall on the representative method 58% → 100%
  • no over-correction — a method that is genuinely dead still reports 0 callers
  • only 2 unresolved receivers, both legitimately hard (a lazily-initialised attribute and a class constant)

Guarding on "only bind to class names that already exist as nodes" is what keeps precision at 100% —
it stops self.rows = list() from producing bogus bindings.

Offer

Happy to port this into _python_method_receiver_types mirroring the Java/C# ones and send a PR
against v8, if that is a direction you want. Wanted to check before writing against a 5,772-line
engine.py. Also happy to just leave the analysis here if this is already on the roadmap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions