[ty] Infer lambda return types from body expressions#23328
[ty] Infer lambda return types from body expressions#23328alex wants to merge 1 commit intoastral-sh:mainfrom
Conversation
Infer the return type of lambda expressions from their body expression, rather than always using `Unknown`. This can introduce new Salsa cycles when a lambda body references a name involved in a circular definition. Added cycle recovery guards to prevent panics from non-converging cycles, though I'm not confident this is the correct solution. https://claude.ai/code/session_014XYWYmMYnqZdVjBQCs5XCd
Typing conformance resultsNo changes detected ✅ |
|
Memory usage reportSummary
Significant changesClick to expand detailed breakdownprefect
sphinx
trio
flake8
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-argument-type |
17 | 16 | 118 |
invalid-assignment |
10 | 0 | 42 |
unresolved-attribute |
7 | 0 | 23 |
unused-type-ignore-comment |
1 | 19 | 0 |
unsupported-operator |
7 | 0 | 4 |
type-assertion-failure |
0 | 5 | 1 |
no-matching-overload |
4 | 0 | 0 |
not-subscriptable |
4 | 0 | 0 |
invalid-return-type |
2 | 0 | 1 |
invalid-await |
1 | 0 | 0 |
not-iterable |
0 | 0 | 1 |
| Total | 53 | 40 | 190 |
|
Important note, as indicated in a comment, this is kind of the bare minimum to infer types for lambdas where the return type isn't specific to the callsite/argument types. I'm hoping that since the volume of code is so minimal that it's an ok stepping stone. |
|
you may have seen the previous attempt in #22633 -- that one was attempting something slightly more ambitious with full type context, but it might be helpful for you |
|
Yeah, the test case from that is what led to discovering the cyclic issues. Given my lack of certainty about the correctness of those fixes, I figured I'd start with the most basic implementation. |
|
I'm not sure this usefully narrows scope compared to #22633. The benefit of inferring with type context is that we avoid false positives on e.g., I also don't think introducing cycle fallback based on an iteration threshold is a good idea. Based on the dicussion in #22818, we have a good idea of what the issue is, and I would rather delay this feature and work on a concrete solution than introduce fallbacks in arbitrary queries. Our recursive type inference should be robust to all of these issues. |
|
Ok! More than happy to defer to that.
All that is necessary for evil to succeed is for good people to do nothing.
…On Thu, Feb 19, 2026, 8:23 AM Ibraheem Ahmed ***@***.***> wrote:
*ibraheemdev* left a comment (astral-sh/ruff#23328)
<#23328 (comment)>
I'm not sure this usefully narrows scope compared to #22633
<#22633>. The benefit of inferring
with type context is that we avoid false positives on e.g., _:
Callable[[], MyTypedDict] = lambda: { "foo": 1 }, and extracting the type
context from the callable annotation is relatively trivial.
I also don't think introducing cycle fallback based on an iteration
threshold is a good idea. Based on the dicussion in #22818
<#22818>, we have a good idea of
what the issue is, and I would rather delay this feature and work on a
concrete solution than introduce fallbacks in arbitrary queries. Our
recursive type inference should be robust to all of these issues.
—
Reply to this email directly, view it on GitHub
<#23328 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAAGBFE2N3QJ2Y2DEVUGE34MW2OXAVCNFSM6AAAAACVHLGOYCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTSMRXGI4TIMZSGU>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
|
Closing this for now. |
Infer the return type of lambda expressions from their body expression, rather than always using
Unknown.This can introduce new Salsa cycles when a lambda body references a name involved in a circular definition. Added cycle recovery guards to prevent panics from non-converging cycles, though I'm not confident this is the correct solution.