Skip to content

Conversation

@dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Mar 7, 2025

Summary

Part of #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:

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.

@dhruvmanila dhruvmanila added the ty Multi-file analysis & type inference label Mar 7, 2025
Base automatically changed from dhruv/callable-type to main March 8, 2025 03:58
@dhruvmanila dhruvmanila force-pushed the dhruv/infer-lambda-expr branch from 3d132d6 to e4da8a3 Compare March 10, 2025 09:35
@dhruvmanila dhruvmanila force-pushed the dhruv/infer-lambda-expr branch from e4da8a3 to 7d8fdb0 Compare March 10, 2025 10:00
@dhruvmanila dhruvmanila marked this pull request as ready for review March 10, 2025 12:43
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks!

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();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is similar to infer_definition_types and it allowed me to quickly understand the origin of the cycle and what was causing it.

@github-actions
Copy link
Contributor

mypy_primer results

No ecosystem changes detected ✅

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

@carljm
Copy link
Contributor

carljm commented Mar 11, 2025

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.

@dhruvmanila dhruvmanila merged commit da069aa into main Mar 11, 2025
22 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/infer-lambda-expr branch March 11, 2025 05:55
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants