ruff/crates/red_knot_python_semantic/resources/mdtest/stubs/ellipsis.md
InSync 11cfe2ea8a
[red-knot] Enforce specifying paths for mdtest code blocks in a separate preceding line (#15890)
## Summary

Resolves #15695, rework of #15704.

This change modifies the Mdtests framework so that:

* Paths must now be specified in a separate preceding line:

	`````markdown
	`a.py`:

	```py
	x = 1
	```
	`````

If the path of a file conflicts with its `lang`, an error will be
thrown.

* Configs are no longer accepted. The pattern still take them into
account, however, to avoid "Unterminated code block" errors.
* Unnamed files are now assigned unique, `lang`-respecting paths
automatically.

Additionally, all legacy usages have been updated.

## Test Plan

Unit tests and Markdown tests.

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
2025-02-04 08:27:17 +01:00

2 KiB

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.

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.

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:

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:

# 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.

# 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.

# error: 7 [invalid-parameter-default] "Default value of type `ellipsis` is not assignable to annotated parameter type `int`"
def f(x: int = Ellipsis) -> None: ...