-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow assigning ellipsis literal as parameter default value (#14982)
Resolves #14840 ## Summary Usage of ellipsis literal as default argument is allowed in stub files. ## Test Plan Added mdtest for both python files and stub files. --------- Co-authored-by: Carl Meyer <carl@oddbird.net> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
- Loading branch information
1 parent
2ea6362
commit b264489
Showing
6 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
78 changes: 78 additions & 0 deletions
78
crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
This file contains 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,78 @@ | ||
# Ellipsis | ||
|
||
## Function and methods | ||
|
||
The ellipsis literal `...` can be used as a placeholder default value for a function parameter, in a | ||
stub file only, regardless of the type of the parameter. | ||
|
||
```py path=test.pyi | ||
def f(x: int = ...) -> None: | ||
reveal_type(x) # revealed: int | ||
|
||
def f2(x: str = ...) -> None: | ||
reveal_type(x) # revealed: str | ||
``` | ||
|
||
## Class and module symbols | ||
|
||
The ellipsis literal can be assigned to a class or module symbol, regardless of its declared type, | ||
in a stub file only. | ||
|
||
```py path=test.pyi | ||
y: bytes = ... | ||
reveal_type(y) # revealed: bytes | ||
x = ... | ||
reveal_type(x) # revealed: Unknown | ||
|
||
class Foo: | ||
y: int = ... | ||
|
||
reveal_type(Foo.y) # revealed: int | ||
``` | ||
|
||
## Unpacking ellipsis literal in assignment | ||
|
||
No diagnostic is emitted if an ellipsis literal is "unpacked" in a stub file as part of an | ||
assignment statement: | ||
|
||
```py path=test.pyi | ||
x, y = ... | ||
reveal_type(x) # revealed: Unknown | ||
reveal_type(y) # revealed: Unknown | ||
``` | ||
|
||
## Unpacking ellipsis literal in for loops | ||
|
||
Iterating over an ellipsis literal as part of a `for` loop in a stub is invalid, however, and | ||
results in a diagnostic: | ||
|
||
```py path=test.pyi | ||
# error: [not-iterable] "Object of type `ellipsis` is not iterable" | ||
for a, b in ...: | ||
reveal_type(a) # revealed: Unknown | ||
reveal_type(b) # revealed: Unknown | ||
``` | ||
|
||
## Ellipsis usage in non stub file | ||
|
||
In a non-stub file, there's no special treatment of ellipsis literals. An ellipsis literal can only | ||
be assigned if `EllipsisType` is actually assignable to the annotated type. | ||
|
||
```py | ||
# error: 7 [invalid-parameter-default] "Default value of type `ellipsis` is not assignable to annotated parameter type `int`" | ||
def f(x: int = ...) -> None: ... | ||
|
||
# error: 1 [invalid-assignment] "Object of type `ellipsis` is not assignable to `int`" | ||
a: int = ... | ||
b = ... | ||
reveal_type(b) # revealed: ellipsis | ||
``` | ||
|
||
## Use of `Ellipsis` symbol | ||
|
||
There is no special treatment of the builtin name `Ellipsis` in stubs, only of `...` literals. | ||
|
||
```py path=test.pyi | ||
# error: 7 [invalid-parameter-default] "Default value of type `ellipsis` is not assignable to annotated parameter type `int`" | ||
def f(x: int = Ellipsis) -> None: ... | ||
``` |
This file contains 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 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 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 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