Repository navigation
fix(resolution): a local bound from a call is typed by the callee's return annotation (#3038) - #3120
johnhldiver-beep wants to merge 1 commit into
Conversation
…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.
|
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 Verified on head The return type is resolved in the caller's file. With # 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 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()
Rebased on What holds: a library return type ( |
…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>
…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>
…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>
…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>
…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>
|
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 |
Fixes #3038.
Problem
A method call on a value typed by a function's stdlib return annotation linked an unrelated project method:
On main,
size_of_madegot acallsedge toarm.py: Arm::stat(resolvedBy: instance-method), while the annotated-parameter case (size) correctly linked nothing.Root cause
inferLocalReceiverTypetyped 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 toArm::stat.Fix
inferLocalReceiverTypegains a Python fallback after its pattern scan comes up empty:pythonLocalCallResultTypefinds the nearest binding of the receiver that is an assignment-from-call, resolves the callee (a project function via the import-aware lookup, or aself.mmethod on the calling method's class), and reads its-> Tannotation through the samepythonAnnotationEvidencethe factory reader uses. The written type then decides:-> Armtypes the local for real (method lands onArm::stat);-> Path(or any capitalized type the project does not declare) is a definitive no-edge viaisUndeclaredTypeName.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
__tests__/python-stdlib-return-type.test.ts(5 e2e cases): the issue repro (noArm::stat,makekept), annotated parameter still links nothing,-> Armtypes the local for real, the issue's realself._write(...) -> Pathcase, no-annotation behavior unchanged. Written red first: 2 failed / 3 passed before the fix, 5/5 after.origin/maincheckout 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.codegraphwith a drifted hardcoded fanIn bar) — pre-existing environment failures, not regressions.tsc --noEmitgreen; 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).