-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Infer lambda expression
#16547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
crates/red_knot_python_semantic/resources/mdtest/expression/lambda.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # `lambda` expression | ||
|
|
||
| ## No parameters | ||
|
|
||
| `lambda` expressions can be defined without any parameters. | ||
|
|
||
| ```py | ||
| reveal_type(lambda: 1) # revealed: () -> @Todo(lambda return type) | ||
|
|
||
| # error: [unresolved-reference] | ||
| reveal_type(lambda: a) # revealed: () -> @Todo(lambda return type) | ||
| ``` | ||
|
|
||
| ## With parameters | ||
|
|
||
| Unlike parameters in function definition, the parameters in a `lambda` expression cannot be | ||
| annotated. | ||
|
|
||
| ```py | ||
| reveal_type(lambda a: a) # revealed: (a) -> @Todo(lambda return type) | ||
| reveal_type(lambda a, b: a + b) # revealed: (a, b) -> @Todo(lambda return type) | ||
| ``` | ||
|
|
||
| But, it can have default values: | ||
|
|
||
| ```py | ||
| reveal_type(lambda a=1: a) # revealed: (a=Literal[1]) -> @Todo(lambda return type) | ||
| reveal_type(lambda a, b=2: a) # revealed: (a, b=Literal[2]) -> @Todo(lambda return type) | ||
| ``` | ||
|
|
||
| And, positional-only parameters: | ||
|
|
||
| ```py | ||
| reveal_type(lambda a, b, /, c: c) # revealed: (a, b, /, c) -> @Todo(lambda return type) | ||
| ``` | ||
|
|
||
| And, keyword-only parameters: | ||
|
|
||
| ```py | ||
| reveal_type(lambda a, *, b=2, c: b) # revealed: (a, *, b=Literal[2], c) -> @Todo(lambda return type) | ||
| ``` | ||
|
|
||
| And, variadic parameter: | ||
|
|
||
| ```py | ||
| reveal_type(lambda *args: args) # revealed: (*args) -> @Todo(lambda return type) | ||
| ``` | ||
|
|
||
| And, keyword-varidic parameter: | ||
|
|
||
| ```py | ||
| reveal_type(lambda **kwargs: kwargs) # revealed: (**kwargs) -> @Todo(lambda return type) | ||
| ``` | ||
|
|
||
| Mixing all of them together: | ||
|
|
||
| ```py | ||
| # revealed: (a, b, /, c=Literal[True], *args, *, d=Literal["default"], e=Literal[5], **kwargs) -> @Todo(lambda return type) | ||
| reveal_type(lambda a, b, /, c=True, *args, d="default", e=5, **kwargs: None) | ||
| ``` | ||
|
|
||
| ## Parameter type | ||
|
|
||
| In addition to correctly inferring the `lambda` expression, the parameters should also be inferred | ||
| correctly. | ||
|
|
||
| Using a parameter with no default value: | ||
|
|
||
| ```py | ||
| lambda x: reveal_type(x) # revealed: Unknown | ||
| ``` | ||
|
|
||
| Using a parameter with default value: | ||
|
|
||
| ```py | ||
| lambda x=1: reveal_type(x) # revealed: Unknown | Literal[1] | ||
| ``` | ||
|
|
||
| Using a variadic paramter: | ||
|
|
||
| ```py | ||
| # TODO: should be `tuple[Unknown, ...]` (needs generics) | ||
| lambda *args: reveal_type(args) # revealed: tuple | ||
| ``` | ||
|
|
||
| Using a keyword-varidic parameter: | ||
|
|
||
| ```py | ||
| # TODO: should be `dict[str, Unknown]` (needs generics) | ||
| lambda **kwargs: reveal_type(kwargs) # revealed: dict | ||
| ``` | ||
|
|
||
| ## Nested `lambda` expressions | ||
|
|
||
| Here, a `lambda` expression is used as the default value for a parameter in another `lambda` | ||
| expression. | ||
|
|
||
| ```py | ||
| reveal_type(lambda a=lambda x, y: 0: 2) # revealed: (a=(x, y) -> @Todo(lambda return type)) -> @Todo(lambda return type) | ||
| ``` |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
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_typesand it allowed me to quickly understand the origin of the cycle and what was causing it.