-
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 for TOML configs in Markdown tests (#14785)
## Summary This adds support for specifying the target Python version from a Markdown test. It is a somewhat limited ad-hoc solution, but designed to be future-compatible. TOML blocks can be added to arbitrary sections in the Markdown block. They have the following format: ````markdown ```toml [tool.knot.environment] target-version = "3.13" ``` ```` So far, there is nothing else that can be configured, but it should be straightforward to extend this to things like a custom typeshed path. This is in preparation for the statically-known branches feature where we are going to have to specify the target version for lots of tests. ## Test Plan - New Markdown test that fails without the explicitly specified `target-version`. - Manually tested various error paths when specifying a wrong `target-version` field. - Made sure that running tests is as fast as before.
- Loading branch information
Showing
8 changed files
with
202 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
crates/red_knot_python_semantic/resources/mdtest/mdtest_config.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,67 @@ | ||
This test makes sure that `red_knot_test` correctly parses the TOML configuration blocks and applies | ||
the correct settings hierarchically. | ||
|
||
The following configuration will be attached to the *root* section (without any heading): | ||
|
||
```toml | ||
[environment] | ||
target-version = "3.10" | ||
``` | ||
|
||
# Basic | ||
|
||
Here, we simply make sure that we pick up the global configuration from the root section: | ||
|
||
```py | ||
reveal_type(sys.version_info[:2] == (3, 10)) # revealed: Literal[True] | ||
``` | ||
|
||
# Inheritance | ||
|
||
## Child | ||
|
||
### Grandchild | ||
|
||
The same should work for arbitrarly nested sections: | ||
|
||
```py | ||
reveal_type(sys.version_info[:2] == (3, 10)) # revealed: Literal[True] | ||
``` | ||
|
||
# Overwriting | ||
|
||
Here, we make sure that we can overwrite the global configuration in a child section: | ||
|
||
```toml | ||
[environment] | ||
target-version = "3.11" | ||
``` | ||
|
||
```py | ||
reveal_type(sys.version_info[:2] == (3, 11)) # revealed: Literal[True] | ||
``` | ||
|
||
# No global state | ||
|
||
There is no global state. This section should again use the root configuration: | ||
|
||
```py | ||
reveal_type(sys.version_info[:2] == (3, 10)) # revealed: Literal[True] | ||
``` | ||
|
||
# Overwriting affects children | ||
|
||
Children in this section should all use the section configuration: | ||
|
||
```toml | ||
[environment] | ||
target-version = "3.12" | ||
``` | ||
|
||
## Child | ||
|
||
### Grandchild | ||
|
||
```py | ||
reveal_type(sys.version_info[:2] == (3, 12)) # revealed: Literal[True] | ||
``` |
5 changes: 5 additions & 0 deletions
5
crates/red_knot_python_semantic/resources/mdtest/sys_version_info.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//! TOML-deserializable Red Knot configuration, similar to `knot.toml`, to be able to | ||
//! control some configuration options from Markdown files. For now, this supports the | ||
//! following limited structure: | ||
//! | ||
//! ```toml | ||
//! [environment] | ||
//! target-version = "3.10" | ||
//! ``` | ||
use anyhow::Context; | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize)] | ||
pub(crate) struct MarkdownTestConfig { | ||
pub(crate) environment: Environment, | ||
} | ||
|
||
impl MarkdownTestConfig { | ||
pub(crate) fn from_str(s: &str) -> anyhow::Result<Self> { | ||
toml::from_str(s).context("Error while parsing Markdown TOML config") | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub(crate) struct Environment { | ||
#[serde(rename = "target-version")] | ||
pub(crate) target_version: String, | ||
} |
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.