-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Support for TOML configs in Markdown tests #14785
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
7 commits
Select commit
Hold shift + click to select a range
c3c11ca
[red-knot] Preliminary support for TOML configs in MD tests
sharkdp baaaa4e
Model knot.toml instead of pyproject.toml in TOML blocks
sharkdp 4b36e3d
Clarify comment
sharkdp 575d00c
Hierarchical configuration
sharkdp ef7235c
Rename SectionStack::parent to SectionStack::top
sharkdp 798a99f
Move bool flag to Parser
sharkdp 54b4393
Update documentation
sharkdp 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
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 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,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 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
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
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 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 |
---|---|---|
|
@@ -2,14 +2,17 @@ use camino::Utf8Path; | |
use colored::Colorize; | ||
use parser as test_parser; | ||
use red_knot_python_semantic::types::check_types; | ||
use red_knot_python_semantic::Program; | ||
use ruff_db::diagnostic::{Diagnostic, ParseDiagnostic}; | ||
use ruff_db::files::{system_path_to_file, File, Files}; | ||
use ruff_db::parsed::parsed_module; | ||
use ruff_db::system::{DbWithTestSystem, SystemPathBuf}; | ||
use ruff_source_file::LineIndex; | ||
use ruff_text_size::TextSize; | ||
use salsa::Setter; | ||
|
||
mod assertion; | ||
mod config; | ||
mod db; | ||
mod diagnostic; | ||
mod matcher; | ||
|
@@ -26,7 +29,7 @@ pub fn run(path: &Utf8Path, long_title: &str, short_title: &str, test_name: &str | |
let suite = match test_parser::parse(short_title, &source) { | ||
Ok(suite) => suite, | ||
Err(err) => { | ||
panic!("Error parsing `{path}`: {err}") | ||
panic!("Error parsing `{path}`: {err:?}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes it possible to see the full error trace, not just the topmost context. For example:
|
||
} | ||
}; | ||
|
||
|
@@ -39,6 +42,10 @@ pub fn run(path: &Utf8Path, long_title: &str, short_title: &str, test_name: &str | |
continue; | ||
} | ||
|
||
Program::get(&db) | ||
.set_target_version(&mut db) | ||
.to(test.target_version()); | ||
|
||
// Remove all files so that the db is in a "fresh" state. | ||
db.memory_file_system().remove_all(); | ||
Files::sync_all(&mut db); | ||
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.