ruff/crates/red_knot_python_semantic/resources/mdtest/expression
Dhruv Manilawala da069aa00c
[red-knot] Infer `lambda` expression (#16547)
## Summary

Part of https://github.com/astral-sh/ruff/issues/15382

This PR adds support for inferring the `lambda` expression and return
the `CallableType`.

Currently, this is only limited to inferring the parameters and a todo
type for the return type.

For posterity, I tried using the `file_expression_type` to infer the
return type of lambda but it would always lead to cycle. The main reason
is that in `infer_parameter_definition`, the default expression is being
inferred using `file_expression_type`, which is correct, but it then

Take the following source code as an example:
```py
lambda x=1: x
```

Here's how the code will flow:
* `infer_scope_types` for the global scope
* `infer_lambda_expression`
* `infer_expression` for the default value `1`
* `file_expression_type` for the return type using the body expression.
This is because the body creates it's own scope
* `infer_scope_types` (lambda body scope)
* `infer_name_load` for the symbol `x` whose visible binding is the
lambda parameter `x`
* `infer_parameter_definition` for parameter `x`
* `file_expression_type` for the default value `1`
* `infer_scope_types` for the global scope because of the default
expression

This will then reach to `infer_definition` for the parameter `x` again
which then creates the cycle.

## Test Plan

Add tests around `lambda` expression inference.
2025-03-11 11:25:20 +05:30
..
assert.md [red-knot] Diagnostics for incorrect `bool` usages (#16238) 2025-02-21 19:26:05 +01:00
attribute.md [red-knot] Use `Unknown | T_inferred` for undeclared public symbols (#15674) 2025-01-24 12:47:48 +01:00
boolean.md [red-knot] Add diagnostic for class-object access to pure instance variables (#16036) 2025-02-24 15:17:16 +01:00
if.md [red-knot] Add missing imports in mdtests (#15869) 2025-02-03 09:27:29 +00:00
lambda.md [red-knot] Infer `lambda` expression (#16547) 2025-03-11 11:25:20 +05:30
len.md [red-knot] Use `Unknown | T_inferred` for undeclared public symbols (#15674) 2025-01-24 12:47:48 +01:00