Skip to content

fix(resolution): a local bound from a call is typed by the callee's return annotation (#3038) - #3120

Closed
johnhldiver-beep wants to merge 1 commit into
colbymchenry:mainfrom
johnhldiver-beep:fix/3038-stdlib-return-type
Closed

johnhldiver-beep wants to merge 1 commit into
colbymchenry:mainfrom
johnhldiver-beep:fix/3038-stdlib-return-type

Conversation

@johnhldiver-beep

Copy link
Copy Markdown

Fixes #3038.

Problem

A method call on a value typed by a function's stdlib return annotation linked an unrelated project method:

# use.py
from pathlib import Path


def make(p: Path) -> Path:
    return p


def size_of_made(q: Path) -> int:
    made = make(q)
    return made.stat().st_size       # wrongly linked to Arm::stat

On main, size_of_made got a calls edge to arm.py: Arm::stat (resolvedBy: instance-method), while the annotated-parameter case (size) correctly linked nothing.

Root cause

inferLocalReceiverType typed a Python method-call receiver from four line patterns only (Django manager call, direct constructor call, quoted forward reference, PEP 526 annotation). A local bound from a call (made = make(q)) matched none of them, so the receiver stayed untyped and the call fell through to the name-only strategies, where the one-method guess bound it to Arm::stat.

Fix

inferLocalReceiverType gains a Python fallback after its pattern scan comes up empty: pythonLocalCallResultType finds the nearest binding of the receiver that is an assignment-from-call, resolves the callee (a project function via the import-aware lookup, or a self.m method on the calling method's class), and reads its -> T annotation through the same pythonAnnotationEvidence the factory reader uses. The written type then decides: -> Arm types the local for real (method lands on Arm::stat); -> Path (or any capitalized type the project does not declare) is a definitive no-edge via isUndeclaredTypeName.

Scope guards: only a written annotation speaks (no annotation, async def, unreadable callee/annotation -> unchanged behavior); the nearest binding decides (constructor-call bindings stay with the existing assignment pattern). Related #2946 (TypeScript form) and #2957 (module-attribute form) are out of scope.

Tests

  • New __tests__/python-stdlib-return-type.test.ts (5 e2e cases): the issue repro (no Arm::stat, make kept), annotated parameter still links nothing, -> Arm types the local for real, the issue's real self._write(...) -> Path case, no-annotation behavior unchanged. Written red first: 2 failed / 3 passed before the fix, 5/5 after.
  • Python + receiver-typing suites: 140/140.
  • Full suite: 9368 passed / 5 failed / 439 skipped. All 5 failures were re-proven on a bare origin/main checkout this session (4 x gitignore-pattern-parity: the box's git 2.47.3 vs the parity harness; 1 x ui-server-api "busiest symbol": indexes the workspace's gitignored .codegraph with a drifted hardcoded fanIn bar) — pre-existing environment failures, not regressions.
  • After rebasing onto current main (274b43c): build + tsc --noEmit green; affected tests re-run green (51/51 incl. upstream's new outside-bases suite, plus a wider python/resolution sweep 108/108).

Files

3 files, +237/-2: src/resolution/name-matcher.ts, __tests__/python-stdlib-return-type.test.ts (new), CHANGELOG.md ([Unreleased] Fixes entry).

…eturn annotation (colbymchenry#3038)

A Python method call's receiver was typed from four line patterns — a
Django manager call, a direct constructor call, a quoted forward
reference, and a PEP 526 annotation (which is why a typed parameter
already linked nothing: the annotation names a type the project does not
declare, and the shared receiver path makes that a definitive no-edge).
A local bound from a call to a project function — `made = make(q)` with
`make` declared `-> Path` — matched none of them, so the call fell
through to the name-only strategies and the one-method guess bound
`made.stat()` to an unrelated project `Arm::stat`.

inferLocalReceiverType gains a Python fallback after its pattern scan
(the Go `r := newRing()` precedent, colbymchenry#2782): the nearest binding of the
receiver that is an assignment-from-call resolves its callee — a project
function, or a `self.m` method on the class the calling method belongs
to — and reads its `-> T` annotation. The written type then decides, as
a typed parameter's does: the project class's own method for `-> Arm`,
no edge for `-> Path`. Only a written annotation speaks: a callee
without one, an `async def`, an unreadable callee or annotation, and a
binding the reader cannot type all say nothing, and the call keeps its
old strategies.
@inth3shadows

Copy link
Copy Markdown
Contributor

This fixes #3038's shape, but the return annotation is resolved in the caller's file instead of the file that declares it. That creates a wrong edge when two project classes share a name. Aliased classes also lose the edge main already had.

Verified on head 1caa467e against main at f72c9d2e.

The return type is resolved in the caller's file. With a/arm.py and b/arm.py each defining class Arm with a stat method:

# b/util.py
from b.arm import Arm
def make() -> Arm:
    return Arm()

# a/use.py
from a.arm import Arm
from b.util import make
def go():
    x = make()
    return x.stat()

This branch links go -> a/arm.py Arm.stat, but the call goes to b/arm.py's Arm. main links neither. Resolving the annotation against b/util.py's imports would fix it.

An aliased class in the annotation loses its edge.

# util.py
from arm import Arm as A
def mk() -> A:
    return A()

# use.py
from util import mk
def go():
    x = mk()
    return x.stat()

main links go -> arm.py Arm.stat; this branch links nothing.

Rebased on main at f72c9d2e, name-matcher-memo-clears.test.ts fails: expected ['PY_CALLEE_RETURN'] to deeply equal []. The new memo isn't cleared on sync. In a running server, an edited return annotation keeps its old type until restart.

What holds: a library return type (-> Path from pathlib) no longer links the project class Arm, which is the issue's case. Optional, union, string and TypeVar returns behave the same way, and project-class annotations still link.

colbymchenry added a commit that referenced this pull request Nov 18, 2026
…turns

A method call on a Python local bound from a call ignored the type the
callee declares. `made = make(q)` with `def make(p: Path) -> Path` left
`made` untyped, so `made.stat()` fell to the guess by the method's name and
linked the one project method named `stat` (Issue 3038); `-> str` did the
same. A local bound from another local's method, `s = store.stage()` with
`def stage(self) -> Stager`, stayed untyped even for that case's own
reader, so `s.add()` linked nothing (Issue 3240), and with a method name
two classes share, neither did `s = make(); s.push()`.

The general method path now asks pythonLocalClass, the reader of every
binding of a local that typed builtin-named calls (Issue 2821), whenever no
pattern typed the receiver. A project class gives its own method, a base's,
or the one subclass's that defines it; a builtin or a class from outside
the project (a `-> Path` after `from pathlib import Path`, a `-> str`, a
parameter annotated `str` or `t.TextIO`, an `io.BytesIO()`) gives no
project method, so no edge; unknown or contested, the name strategies
guess as before. Four changes to the reader serve it:
- a factory's `-> T` naming a class from outside the project now answers
  "outside", not "unknown";
- a binding `s = store.stage()` reads `stage`'s return on the class the
  local `store` holds, one link deep;
- a value wrapped in parentheses, `timeout = (A(x) if c else A(1))`, is
  read as the expression inside, not as a tuple;
- the lines of a def that belong to it alone are read once per def.

Validation, against main (both arms with the 1.6.3 kernel, fresh init):
- New test file, 6 cases: 4 fail on main and pass now (both issues'
  repros, `-> pathlib.Path` / `-> str` / a `str` parameter, a project
  class among namesakes); 2 guards (a parenthesized value, a factory whose
  result can't be read) pass on both.
- click: 15 removed, all right (10 `getvalue()` on `io.BytesIO` /
  `io.StringIO` linked to HelpFormatter, 3 on an `optparse.OptionParser`
  linked to click's own parser, 2 `fileno()` on a `t.TextIO` parameter),
  1 new and right (a closure's call on its enclosing method's `ctx:
  Context`), 11 keep their target at 0.85. django's django/ package: 4
  removed, all right (`context.update()` on a dict literal linked to the
  template Context). flask: 1 removed, right (`ctx.invoke()` on a
  `click.Context` parameter linked to FlaskCliRunner). httpx, express and
  gin: byte-identical.
- Index CPU on django (median of 5, interleaved): 35.0 s on both arms. A
  first build cost +2.5%: every def re-read its own lines for each name
  asked of it, so those lines are now read once per def.
- The 35 Python test files pass.

PR #3120 by johnhldiver-beep targets Issue 3038; this change is written
from the issues and our own diagnosis. It also types a local bound from
another local's method, and reads `-> str` and other builtins as no
project class, which #3120's reader leaves to the guess.

Fixes #3038
Fixes #3240

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
colbymchenry added a commit that referenced this pull request Nov 18, 2026
…turns

A method call on a Python local bound from a call ignored the type the
callee declares. `made = make(q)` with `def make(p: Path) -> Path` left
`made` untyped, so `made.stat()` fell to the guess by the method's name and
linked the one project method named `stat` (Issue 3038); `-> str` did the
same. A local bound from another local's method, `s = store.stage()` with
`def stage(self) -> Stager`, stayed untyped even for that case's own
reader, so `s.add()` linked nothing (Issue 3240), and with a method name
two classes share, neither did `s = make(); s.push()`.

The general method path now asks pythonLocalClass, the reader of every
binding of a local that typed builtin-named calls (Issue 2821), whenever no
pattern typed the receiver. A project class gives its own method, a base's,
or the one subclass's that defines it; a builtin or a class from outside
the project (a `-> Path` after `from pathlib import Path`, a `-> str`, a
parameter annotated `str` or `t.TextIO`, an `io.BytesIO()`) gives no
project method, so no edge; unknown or contested, the name strategies
guess as before. Four changes to the reader serve it:
- a factory's `-> T` naming a class from outside the project now answers
  "outside", not "unknown";
- a binding `s = store.stage()` reads `stage`'s return on the class the
  local `store` holds, one link deep;
- a value wrapped in parentheses, `timeout = (A(x) if c else A(1))`, is
  read as the expression inside, not as a tuple;
- the lines of a def that belong to it alone are read once per def.

Validation, against main (both arms with the 1.6.3 kernel, fresh init):
- New test file, 6 cases: 4 fail on main and pass now (both issues'
  repros, `-> pathlib.Path` / `-> str` / a `str` parameter, a project
  class among namesakes); 2 guards (a parenthesized value, a factory whose
  result can't be read) pass on both.
- click: 15 removed, all right (10 `getvalue()` on `io.BytesIO` /
  `io.StringIO` linked to HelpFormatter, 3 on an `optparse.OptionParser`
  linked to click's own parser, 2 `fileno()` on a `t.TextIO` parameter),
  1 new and right (a closure's call on its enclosing method's `ctx:
  Context`), 11 keep their target at 0.85. django's django/ package: 4
  removed, all right (`context.update()` on a dict literal linked to the
  template Context). flask: 1 removed, right (`ctx.invoke()` on a
  `click.Context` parameter linked to FlaskCliRunner). httpx, express and
  gin: byte-identical.
- Index CPU on django (median of 5, interleaved): 35.0 s on both arms. A
  first build cost +2.5%: every def re-read its own lines for each name
  asked of it, so those lines are now read once per def.
- The 35 Python test files pass.

PR #3120 by johnhldiver-beep targets Issue 3038; this change is written
from the issues and our own diagnosis. It also types a local bound from
another local's method, and reads `-> str` and other builtins as no
project class, which #3120's reader leaves to the guess.

Fixes #3038
Fixes #3240

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
colbymchenry added a commit that referenced this pull request Nov 18, 2026
…turns

A method call on a Python local bound from a call ignored the type the
callee declares. `made = make(q)` with `def make(p: Path) -> Path` left
`made` untyped, so `made.stat()` fell to the guess by the method's name and
linked the one project method named `stat` (Issue 3038); `-> str` did the
same. A local bound from another local's method, `s = store.stage()` with
`def stage(self) -> Stager`, stayed untyped even for that case's own
reader, so `s.add()` linked nothing (Issue 3240), and with a method name
two classes share, neither did `s = make(); s.push()`.

The general method path now asks pythonLocalClass, the reader of every
binding of a local that typed builtin-named calls (Issue 2821), whenever no
pattern typed the receiver. A project class gives its own method, a base's,
or the one subclass's that defines it; a builtin or a class from outside
the project (a `-> Path` after `from pathlib import Path`, a `-> str`, a
parameter annotated `str` or `t.TextIO`, an `io.BytesIO()`) gives no
project method, so no edge; unknown or contested, the name strategies
guess as before. Four changes to the reader serve it:
- a factory's `-> T` naming a class from outside the project now answers
  "outside", not "unknown";
- a binding `s = store.stage()` reads `stage`'s return on the class the
  local `store` holds, one link deep;
- a value wrapped in parentheses, `timeout = (A(x) if c else A(1))`, is
  read as the expression inside, not as a tuple;
- the lines of a def that belong to it alone are read once per def.

Validation, against main (both arms with the 1.6.3 kernel, fresh init):
- New test file, 6 cases: 4 fail on main and pass now (both issues'
  repros, `-> pathlib.Path` / `-> str` / a `str` parameter, a project
  class among namesakes); 2 guards (a parenthesized value, a factory whose
  result can't be read) pass on both.
- click: 15 removed, all right (10 `getvalue()` on `io.BytesIO` /
  `io.StringIO` linked to HelpFormatter, 3 on an `optparse.OptionParser`
  linked to click's own parser, 2 `fileno()` on a `t.TextIO` parameter),
  1 new and right (a closure's call on its enclosing method's `ctx:
  Context`), 11 keep their target at 0.85. django's django/ package: 4
  removed, all right (`context.update()` on a dict literal linked to the
  template Context). flask: 1 removed, right (`ctx.invoke()` on a
  `click.Context` parameter linked to FlaskCliRunner). httpx, express and
  gin: byte-identical.
- Index CPU on django (median of 5, interleaved): 35.0 s on both arms. A
  first build cost +2.5%: every def re-read its own lines for each name
  asked of it, so those lines are now read once per def.
- The 35 Python test files pass.

One existing test changes its fixture: sync-removed-competitor's Python
case took `x = make()`, with `make` returning `A()`, for a value of
unknown type. That local is now an A, so `x.go()` links A.go from the
first index, the call is never ambiguous, and the case stopped testing
the sync. It now takes `x` as a parameter nothing annotates, as its
TypeScript case does. With the sync rebind off (CODEGRAPH_NO_REBIND=1)
its delete and rename cases still fail, and with it on all 6 pass.

PR #3120 by johnhldiver-beep targets Issue 3038; this change is written
from the issues and our own diagnosis. It also types a local bound from
another local's method, and reads `-> str` and other builtins as no
project class, which #3120's reader leaves to the guess.

Fixes #3038
Fixes #3240

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
colbymchenry added a commit that referenced this pull request Nov 18, 2026
…turns

A method call on a Python local bound from a call ignored the type the
callee declares. `made = make(q)` with `def make(p: Path) -> Path` left
`made` untyped, so `made.stat()` fell to the guess by the method's name and
linked the one project method named `stat` (Issue 3038); `-> str` did the
same. A local bound from another local's method, `s = store.stage()` with
`def stage(self) -> Stager`, stayed untyped even for that case's own
reader, so `s.add()` linked nothing (Issue 3240), and with a method name
two classes share, neither did `s = make(); s.push()`.

The general method path now asks pythonLocalClass, the reader of every
binding of a local that typed builtin-named calls (Issue 2821), whenever no
pattern typed the receiver. A project class gives its own method, a base's,
or the one subclass's that defines it; a builtin or a class from outside
the project (a `-> Path` after `from pathlib import Path`, a `-> str`, a
parameter annotated `str` or `t.TextIO`, an `io.BytesIO()`) gives no
project method, so no edge; unknown or contested, the name strategies
guess as before. Four changes to the reader serve it:
- a factory's `-> T` naming a class from outside the project now answers
  "outside", not "unknown";
- a binding `s = store.stage()` reads `stage`'s return on the class the
  local `store` holds, one link deep;
- a value wrapped in parentheses, `timeout = (A(x) if c else A(1))`, is
  read as the expression inside, not as a tuple;
- the lines of a def that belong to it alone are read once per def.

Validation, against main (both arms with the 1.6.3 kernel, fresh init):
- New test file, 6 cases: 4 fail on main and pass now (both issues'
  repros, `-> pathlib.Path` / `-> str` / a `str` parameter, a project
  class among namesakes); 2 guards (a parenthesized value, a factory whose
  result can't be read) pass on both.
- click: 15 removed, all right (10 `getvalue()` on `io.BytesIO` /
  `io.StringIO` linked to HelpFormatter, 3 on an `optparse.OptionParser`
  linked to click's own parser, 2 `fileno()` on a `t.TextIO` parameter),
  1 new and right (a closure's call on its enclosing method's `ctx:
  Context`), 11 keep their target at 0.85. django's django/ package: 4
  removed, all right (`context.update()` on a dict literal linked to the
  template Context). flask: 1 removed, right (`ctx.invoke()` on a
  `click.Context` parameter linked to FlaskCliRunner). httpx, express and
  gin: byte-identical.
- Index CPU on django (median of 5, interleaved): 35.0 s on both arms. A
  first build cost +2.5%: every def re-read its own lines for each name
  asked of it, so those lines are now read once per def.
- The 35 Python test files pass.

One existing test changes its fixture: sync-removed-competitor's Python
case took `x = make()`, with `make` returning `A()`, for a value of
unknown type. That local is now an A, so `x.go()` links A.go from the
first index, the call is never ambiguous, and the case stopped testing
the sync. It now takes `x` as a parameter nothing annotates, as its
TypeScript case does. With the sync rebind off (CODEGRAPH_NO_REBIND=1)
its delete and rename cases still fail, and with it on all 6 pass.

PR #3120 by johnhldiver-beep targets Issue 3038; this change is written
from the issues and our own diagnosis. It also types a local bound from
another local's method, and reads `-> str` and other builtins as no
project class, which #3120's reader leaves to the guess.

Fixes #3038
Fixes #3240

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
colbymchenry added a commit that referenced this pull request Nov 18, 2026
…turns (#3375)

A method call on a Python local bound from a call ignored the type the
callee declares. `made = make(q)` with `def make(p: Path) -> Path` left
`made` untyped, so `made.stat()` fell to the guess by the method's name and
linked the one project method named `stat` (Issue 3038); `-> str` did the
same. A local bound from another local's method, `s = store.stage()` with
`def stage(self) -> Stager`, stayed untyped even for that case's own
reader, so `s.add()` linked nothing (Issue 3240), and with a method name
two classes share, neither did `s = make(); s.push()`.

The general method path now asks pythonLocalClass, the reader of every
binding of a local that typed builtin-named calls (Issue 2821), whenever no
pattern typed the receiver. A project class gives its own method, a base's,
or the one subclass's that defines it; a builtin or a class from outside
the project (a `-> Path` after `from pathlib import Path`, a `-> str`, a
parameter annotated `str` or `t.TextIO`, an `io.BytesIO()`) gives no
project method, so no edge; unknown or contested, the name strategies
guess as before. Four changes to the reader serve it:
- a factory's `-> T` naming a class from outside the project now answers
  "outside", not "unknown";
- a binding `s = store.stage()` reads `stage`'s return on the class the
  local `store` holds, one link deep;
- a value wrapped in parentheses, `timeout = (A(x) if c else A(1))`, is
  read as the expression inside, not as a tuple;
- the lines of a def that belong to it alone are read once per def.

Validation, against main (both arms with the 1.6.3 kernel, fresh init):
- New test file, 6 cases: 4 fail on main and pass now (both issues'
  repros, `-> pathlib.Path` / `-> str` / a `str` parameter, a project
  class among namesakes); 2 guards (a parenthesized value, a factory whose
  result can't be read) pass on both.
- click: 15 removed, all right (10 `getvalue()` on `io.BytesIO` /
  `io.StringIO` linked to HelpFormatter, 3 on an `optparse.OptionParser`
  linked to click's own parser, 2 `fileno()` on a `t.TextIO` parameter),
  1 new and right (a closure's call on its enclosing method's `ctx:
  Context`), 11 keep their target at 0.85. django's django/ package: 4
  removed, all right (`context.update()` on a dict literal linked to the
  template Context). flask: 1 removed, right (`ctx.invoke()` on a
  `click.Context` parameter linked to FlaskCliRunner). httpx, express and
  gin: byte-identical.
- Index CPU on django (median of 5, interleaved): 35.0 s on both arms. A
  first build cost +2.5%: every def re-read its own lines for each name
  asked of it, so those lines are now read once per def.
- The 35 Python test files pass.

One existing test changes its fixture: sync-removed-competitor's Python
case took `x = make()`, with `make` returning `A()`, for a value of
unknown type. That local is now an A, so `x.go()` links A.go from the
first index, the call is never ambiguous, and the case stopped testing
the sync. It now takes `x` as a parameter nothing annotates, as its
TypeScript case does. With the sync rebind off (CODEGRAPH_NO_REBIND=1)
its delete and rename cases still fail, and with it on all 6 pass.

PR #3120 by johnhldiver-beep targets Issue 3038; this change is written
from the issues and our own diagnosis. It also types a local bound from
another local's method, and reads `-> str` and other builtins as no
project class, which #3120's reader leaves to the guess.

Fixes #3038
Fixes #3240

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@colbymchenry

Copy link
Copy Markdown
Owner

Thanks for taking on #3038, @johnhldiver-beep. #3375, our own commit with credit to you, fixes it: in Python, a method called on a local bound from a call, like made = make(q), now goes by the type the called function declares it returns. Closing this in favor of #3375. It ships in the next release.

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: a method call on a value typed by a function's stdlib return annotation links an unrelated project method

3 participants