Skip to content

fix(python): self.x() on an attribute no longer links another file's x - #3350

Merged
colbymchenry merged 1 commit into
mainfrom
claude/issue-2958-self-attribute-call
Nov 18, 2026
Merged

colbymchenry merged 1 commit into
mainfrom
claude/issue-2958-self-attribute-call

Conversation

@colbymchenry

@colbymchenry colbymchenry commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

In Python, self.x() or cls.x() on an attribute that holds a callable no longer links to a same-named module-level function or variable in another file, and, when the class sets that attribute itself, no longer links to another class's method of that name. flask's app loading (ScriptInfo.load_app calling self.create_app()) stops showing an example app as its callee.

Root cause

The extractor records self.x() and cls.x() by the bare name. When the class had no method x, nothing kept the call from name matching, which took any module-level function or variable x in the project (the issue's cases), or another class's method x (self.callback() after self.callback = callback linked Other.callback).

Fix

pythonSelfCallReaches (name-matcher.ts), asked in resolveOneIn for every resolved Python call whose receiver is self or cls:

  • a method of the class, of a project base or of a subclass (an override the call runs on the subclass's instances) stays, and so does a class;
  • when the class or a base binds the attribute itself (self.x = …, cls.x = …, or x = … in its body), the call is that value: the target stays only when the value names it under its own name (self.x = x for the module's x, x = staticmethod(x), self.Graph = nx.Graph). A parameter of that name, an instance (self.out = Layer()) or a tuple's element (self.repo, self.run = …) names nothing;
  • with no binding, a module-level function or variable of another file is out of reach; a function of the caller's own file stays (a file can put its functions on its classes, as attrs does), and another class's method stays (a mixin calls into the class it is mixed into);
  • framework-resolved edges are left alone.

Validation

  • New __tests__/python-self-attribute-call.test.ts, 10 cases: the 5 issue shapes (the flask repro, an instance attribute against a function and a variable, a tuple-bound attribute, another class's method, a class attribute on cls) fail on main and pass with the fix; 5 guards (own and inherited methods, a subclass override with a fallback binding, a mixin, what a binding names, a same-file function) pass on both.
  • The 35 Python test files pass (473 tests).
  • A/B against main, both arms with the 1.6.3 kernel, fresh init:
Repo Files Changed edges Triage
flask (d086db8) 91 2 removed both wrong, the issue's real cases: cli.py:342 → the celery example's create_app, test_basic.py:299 → the conftest app fixture
django django/ (3bdcd29) 930 32 removed 28 wrong: a parameter, instance or class the attribute holds, linked to an unrelated class's method of that name (get_response 4, func 4, get_content_type 4, model 3, copy 3, log 2, read 2, six single ones). 4 right only at run time, see below
httpx, click 66, 100 0
express, gin (controls) 149, 110 0 byte-identical

The 4 runtime-only edges: ddl_references' self.quote_name() (3), a parameter its callers fill with connection.ops.quote_name, and RelatedPopulator's self.from_db(), bound to a wrapper of the model's from_db. As with the shadowing locals kept out in #2732, nothing in the text tells these from the 28 wrong ones, so the change accepts them.

Fixes #2958

PR #3217 by nilotpal-n7 targets this issue; this one is written from the issue and our own diagnosis, and also covers another class's method reached through an attribute the class binds, which #3217 keeps. Thanks to inth3shadows for the report.

🤖 Generated with Claude Code

The extractor records `self.x()` and `cls.x()` by the bare name, and when
the class had no method `x`, nothing kept the call from name matching,
which took any module-level function or variable `x` in the project, or
another class's method `x`. flask's `ScriptInfo.load_app` calls
`self.create_app()`, the callable it was handed, and linked an example
app's `create_app`.

resolveOneIn now asks pythonSelfCallReaches whether such a call can mean
the target a strategy gave. It can when the target is a method of the
class, of a project base, or of a subclass (an override the call runs on
the subclass's instances), or a class. When the class or a base binds the
attribute itself (`self.x = ...`, `cls.x = ...`, `x = ...` in its body),
the call is that value, and the target only when the value names it under
its own name: `self.x = x` for the module's `x`, `x = staticmethod(x)`,
`self.Graph = nx.Graph`. A parameter of that name, an instance, or a
tuple's element names nothing. With no binding, a module-level function or
variable of another file is out of reach, a function of the caller's own
file stays (a file can put its functions on its classes), and another
class's method stays (a mixin calls into the class it is mixed into).
Framework-resolved edges are left alone.

Validation, against main (both arms with the 1.6.3 kernel, fresh init):
- New test file, 10 cases: the 5 for the issue's shapes fail on main and
  pass now; 5 guards (own and inherited methods, a subclass override with
  a fallback binding, a mixin, what a binding names, a same-file function)
  pass on both.
- flask: the issue's 2 edges are gone (cli.py:342 to the celery example's
  create_app, test_basic.py:299 to the conftest `app` fixture).
- django's django/ package: 32 removed. 28 were wrong: a parameter,
  instance or class the attribute holds, linked to an unrelated class's
  method of the name (get_response 4, func 4, get_content_type 4, model 3,
  copy 3, log 2, read 2, and paginator, UserModel, path, get_lookups,
  iterator, exception_class once each). 4 were right at run time only:
  ddl_references' `self.quote_name()` (3), a parameter its callers fill
  with `connection.ops.quote_name`, and RelatedPopulator's `self.from_db()`,
  bound to a wrapper of the model's from_db. As with the shadowing locals
  of Issue 2732, nothing in the text tells those 4 from the 28.
- httpx, click, express and gin: byte-identical.
- Index CPU on django (median of 3, interleaved): 35.7 s -> 35.0 s, noise.
- The 35 Python test files pass.

PR #3217 by nilotpal-n7 targets this issue; this change is written from the
issue and our own diagnosis, and also covers a method of an unrelated class
reached through an attribute the class binds, which #3217 keeps.

Fixes #2958

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Python: self.x() on an instance attribute links to an unrelated module-level function or variable x

1 participant