-
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.
Add support for configuring knot in
pyproject.toml
files (#15493)
## Summary This PR adds support for configuring Red Knot in the `tool.knot` section of the project's `pyproject.toml` section. Options specified on the CLI precede the options in the configuration file. This PR only supports the `environment` and the `src.root` options for now. Other options will be added as separate PRs. There are also a few concerns that I intentionally ignored as part of this PR: * Handling of relative paths: We need to anchor paths relative to the current working directory (CLI), or the project (`pyproject.toml` or `knot.toml`) * Tracking the source of a value. Diagnostics would benefit from knowing from which configuration a value comes so that we can point the user to the right configuration file (or CLI) if the configuration is invalid. * Schema generation and there's a lot more; see #15491 This PR changes the default for first party codes: Our existing default was to only add the project root. Now, Red Knot adds the project root and `src` (if such a directory exists). Theoretically, we'd have to add a file watcher event that changes the first-party search paths if a user later creates a `src` directory. I think this is pretty uncommon, which is why I ignored the complexity for now but I can be persuaded to handle it if it's considered important. Part of #15491 ## Test Plan Existing tests, new file watching test demonstrating that changing the python version and platform is correctly reflected.
- Loading branch information
1 parent
9ed67ba
commit eb47a66
Showing
36 changed files
with
820 additions
and
413 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.
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,60 @@ | ||
use anyhow::Context; | ||
use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; | ||
use std::process::Command; | ||
use tempfile::TempDir; | ||
|
||
/// Specifying an option on the CLI should take precedence over the same setting in the | ||
/// project's configuration. | ||
#[test] | ||
fn test_config_override() -> anyhow::Result<()> { | ||
let tempdir = TempDir::new()?; | ||
|
||
std::fs::write( | ||
tempdir.path().join("pyproject.toml"), | ||
r#" | ||
[tool.knot.environment] | ||
python-version = "3.11" | ||
"#, | ||
) | ||
.context("Failed to write settings")?; | ||
|
||
std::fs::write( | ||
tempdir.path().join("test.py"), | ||
r#" | ||
import sys | ||
# Access `sys.last_exc` that was only added in Python 3.12 | ||
print(sys.last_exc) | ||
"#, | ||
) | ||
.context("Failed to write test.py")?; | ||
|
||
insta::with_settings!({filters => vec![(&*tempdir_filter(&tempdir), "<temp_dir>/")]}, { | ||
assert_cmd_snapshot!(knot().arg("--project").arg(tempdir.path()), @r" | ||
success: false | ||
exit_code: 1 | ||
----- stdout ----- | ||
error[lint:unresolved-attribute] <temp_dir>/test.py:5:7 Type `<module 'sys'>` has no attribute `last_exc` | ||
----- stderr ----- | ||
"); | ||
}); | ||
|
||
assert_cmd_snapshot!(knot().arg("--project").arg(tempdir.path()).arg("--python-version").arg("3.12"), @r" | ||
success: true | ||
exit_code: 0 | ||
----- stdout ----- | ||
----- stderr ----- | ||
"); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn knot() -> Command { | ||
Command::new(get_cargo_bin("red_knot")) | ||
} | ||
|
||
fn tempdir_filter(tempdir: &TempDir) -> String { | ||
format!(r"{}\\?/?", regex::escape(tempdir.path().to_str().unwrap())) | ||
} |
Oops, something went wrong.