[red-knot] Infer lambda expression#16547
Merged
dhruvmanila merged 2 commits intomainfrom Mar 11, 2025
Merged
Conversation
19 tasks
3d132d6 to
e4da8a3
Compare
e4da8a3 to
7d8fdb0
Compare
AlexWaygood
approved these changes
Mar 10, 2025
Member
AlexWaygood
left a comment
There was a problem hiding this comment.
This looks great, thanks!
dhruvmanila
commented
Mar 10, 2025
Comment on lines
+122
to
+129
| let file = input.file(db); | ||
| let _span = tracing::trace_span!( | ||
| "infer_definition_types_cycle_recovery", | ||
| range = ?input.kind(db).target_range(), | ||
| file = %file.path(db) | ||
| ) | ||
| .entered(); | ||
|
|
Member
Author
There was a problem hiding this comment.
This is similar to infer_definition_types and it allowed me to quickly understand the origin of the cycle and what was causing it.
Contributor
|
Contributor
|
As we discussed in 1:1, we can infer a return type in some cases if we make default values of the parameters standalone expressions, so they can be resolved without creating a cycle. Longer term, we may want to do "inlining" of lambdas at their usage sites so we can do better transparent inference, but that's for later. |
dcreager
added a commit
that referenced
this pull request
Mar 11, 2025
* main: [red-knot] Rework `Type::to_instance()` to return `Option<Type>` (#16428) [red-knot] Add tests asserting that `KnownClass::to_instance()` doesn't unexpectedly fallback to `Type::Unknown` with full typeshed stubs (#16608) [red-knot] Handle gradual intersection types in assignability (#16611) [red-knot] mypy_primer: split installation and execution (#16622) [red-knot] mypy_primer: pipeline improvements (#16620) [red-knot] Infer `lambda` expression (#16547) [red-knot] mypy_primer: strip ANSI codes (#16604) [red-knot] mypy_primer: comment on PRs (#16599)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Part of #15382
This PR adds support for inferring the
lambdaexpression and return theCallableType.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_typeto infer the return type of lambda but it would always lead to cycle. The main reason is that ininfer_parameter_definition, the default expression is being inferred usingfile_expression_type, which is correct, but it thenTake the following source code as an example:
Here's how the code will flow:
infer_scope_typesfor the global scopeinfer_lambda_expressioninfer_expressionfor the default value1file_expression_typefor the return type using the body expression. This is because the body creates it's own scopeinfer_scope_types(lambda body scope)infer_name_loadfor the symbolxwhose visible binding is the lambda parameterxinfer_parameter_definitionfor parameterxfile_expression_typefor the default value1infer_scope_typesfor the global scope because of the default expressionThis will then reach to
infer_definitionfor the parameterxagain which then creates the cycle.Test Plan
Add tests around
lambdaexpression inference.