-
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.
[red-knot] Support custom typeshed Markdown tests
- Loading branch information
Showing
9 changed files
with
242 additions
and
141 deletions.
There are no files selected for viewing
60 changes: 57 additions & 3 deletions
60
crates/red_knot_python_semantic/resources/mdtest/import/builtins.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 |
---|---|---|
@@ -1,8 +1,62 @@ | ||
# Importing builtin module | ||
# Builtins | ||
|
||
## Importing builtin module | ||
|
||
```py | ||
import builtins | ||
|
||
x = builtins.chr | ||
reveal_type(x) # revealed: Literal[chr] | ||
reveal_type(builtins.chr) # revealed: Literal[chr] | ||
``` | ||
|
||
## Implicit use of builtin | ||
|
||
```py | ||
reveal_type(chr) # revealed: Literal[chr] | ||
``` | ||
|
||
## `str` builtin | ||
|
||
```py | ||
reveal_type(str) # revealed: Literal[str] | ||
``` | ||
|
||
## Builtin symbol from custom typeshed | ||
|
||
```toml | ||
[environment] | ||
typeshed = "/typeshed" | ||
``` | ||
|
||
```pyi path=/typeshed/stdlib/builtins.pyi | ||
class Custom: ... | ||
|
||
custom_builtin: Custom | ||
``` | ||
|
||
```pyi path=/typeshed/stdlib/typing_extensions.pyi | ||
def reveal_type(obj, /): ... | ||
``` | ||
|
||
```py | ||
reveal_type(custom_builtin) # revealed: Custom | ||
``` | ||
|
||
## Unknown builtin (later defined) | ||
|
||
```toml | ||
[environment] | ||
typeshed = "/typeshed" | ||
``` | ||
|
||
```pyi path=/typeshed/stdlib/builtins.pyi | ||
foo = bar | ||
bar = 1 | ||
``` | ||
|
||
```pyi path=/typeshed/stdlib/typing_extensions.pyi | ||
def reveal_type(obj, /): ... | ||
``` | ||
|
||
```py | ||
reveal_type(foo) # revealed: Unknown | ||
``` |
87 changes: 87 additions & 0 deletions
87
crates/red_knot_python_semantic/resources/mdtest/mdtest_custom_typeshed.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,87 @@ | ||
# Custom typeshed | ||
|
||
The `environment.typeshed` configuration option can be used to specify a custom typeshed directory | ||
for Markdown-based tests. Custom typeshed stubs can then be placed in the specified directory using | ||
fenced code blocks with language `pyi`, and will be used instead of the vendored copy of typeshed. | ||
|
||
A fenced code block with language `text` can be used to provide a `stdlib/VERSIONS` file in the | ||
custom typeshed root. If no such file is created explicitly, it will be automatically created with | ||
entries enabling all specified `<typeshed-root>/stdlib` files for all supported Python versions. | ||
|
||
## Basic example (auto-generated `VERSIONS` file) | ||
|
||
First, we specify `/typeshed` as the custom typeshed directory: | ||
|
||
```toml | ||
[environment] | ||
typeshed = "/typeshed" | ||
``` | ||
|
||
We can then place custom stub files in `/typeshed/stdlib`, for example: | ||
|
||
```pyi path=/typeshed/stdlib/builtins.pyi | ||
class BuiltinClass: ... | ||
|
||
builtin_symbol: BuiltinClass | ||
``` | ||
|
||
And finally write a normal Python code block that makes use of the custom stubs: | ||
|
||
```py | ||
b: BuiltinClass = builtin_symbol | ||
|
||
class OtherClass: ... | ||
|
||
o: OtherClass = builtin_symbol # error: [invalid-assignment] | ||
``` | ||
|
||
## Custom `VERSIONS` file | ||
|
||
If we want to specify a custom `VERSIONS` file, we can do so by creating a fenced code block with | ||
language `text`: | ||
|
||
```toml | ||
[environment] | ||
python-version = "3.10" | ||
typeshed = "/typeshed" | ||
``` | ||
|
||
```pyi path=/typeshed/stdlib/old_module.pyi | ||
class OldClass: ... | ||
``` | ||
|
||
```pyi path=/typeshed/stdlib/new_module.pyi | ||
class NewClass: ... | ||
``` | ||
|
||
```text path=/typeshed/stdlib/VERSIONS | ||
old_module: 3.0- | ||
new_module: 3.11- | ||
``` | ||
|
||
```py | ||
from old_module import OldClass | ||
|
||
# error: [unresolved-import] "Cannot resolve import `new_module`" | ||
from new_module import NewClass | ||
``` | ||
|
||
## Using `reveal_type` with a custom typeshed | ||
|
||
When providing a custom typeshed directory, basic things like `reveal_type` will stop working | ||
because we rely on being able to import it from `typing_extensions`. The actual definition of | ||
`reveal_type` in typeshed is slightly involved (depends on generics, `TypeVar`, etc.), but a very | ||
simple untyped definition is enough to make `reveal_type` work in tests: | ||
|
||
```toml | ||
[environment] | ||
typeshed = "/typeshed" | ||
``` | ||
|
||
```pyi path=/typeshed/stdlib/typing_extensions.pyi | ||
def reveal_type(obj, /): ... | ||
``` | ||
|
||
```py | ||
reveal_type(()) # revealed: tuple[()] | ||
``` |
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
Oops, something went wrong.