mirror of https://github.com/astral-sh/ruff
## 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. |
||
|---|---|---|
| .. | ||
| red_knot | ||
| red_knot_project | ||
| red_knot_python_semantic | ||
| red_knot_server | ||
| red_knot_test | ||
| red_knot_vendored | ||
| red_knot_wasm | ||
| ruff | ||
| ruff_annotate_snippets | ||
| ruff_benchmark | ||
| ruff_cache | ||
| ruff_db | ||
| ruff_dev | ||
| ruff_diagnostics | ||
| ruff_formatter | ||
| ruff_graph | ||
| ruff_index | ||
| ruff_linter | ||
| ruff_macros | ||
| ruff_notebook | ||
| ruff_python_ast | ||
| ruff_python_ast_integration_tests | ||
| ruff_python_codegen | ||
| ruff_python_formatter | ||
| ruff_python_index | ||
| ruff_python_literal | ||
| ruff_python_parser | ||
| ruff_python_resolver | ||
| ruff_python_semantic | ||
| ruff_python_stdlib | ||
| ruff_python_trivia | ||
| ruff_python_trivia_integration_tests | ||
| ruff_server | ||
| ruff_source_file | ||
| ruff_text_size | ||
| ruff_wasm | ||
| ruff_workspace | ||