-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Check required-version before parsing rules #22410
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
Changes from 1 commit
39b0411
1498b78
8771a6e
38e95ac
e17383f
31a295b
24b2813
03eb405
7bc15a3
2761bea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,9 @@ use pep440_rs::{Operator, Version, VersionSpecifiers}; | |
| use serde::{Deserialize, Serialize}; | ||
| use strum::IntoEnumIterator; | ||
|
|
||
| use ruff_linter::settings::types::PythonVersion; | ||
| use ruff_linter::settings::types::{PythonVersion, RequiredVersion}; | ||
|
|
||
| use crate::options::Options; | ||
| use crate::options::{Options, validate_required_version}; | ||
|
|
||
| #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
| struct Tools { | ||
|
|
@@ -45,6 +45,7 @@ fn parse_ruff_toml<P: AsRef<Path>>(path: P) -> Result<Options> { | |
| let path = path.as_ref(); | ||
| let contents = std::fs::read_to_string(path) | ||
| .with_context(|| format!("Failed to read {}", path.display()))?; | ||
| check_required_version(&contents, path, &[])?; | ||
| toml::from_str(&contents).with_context(|| format!("Failed to parse {}", path.display())) | ||
| } | ||
|
|
||
|
|
@@ -53,6 +54,7 @@ fn parse_pyproject_toml<P: AsRef<Path>>(path: P) -> Result<Pyproject> { | |
| let path = path.as_ref(); | ||
| let contents = std::fs::read_to_string(path) | ||
| .with_context(|| format!("Failed to read {}", path.display()))?; | ||
| check_required_version(&contents, path, &["tool", "ruff"])?; | ||
| toml::from_str(&contents).with_context(|| format!("Failed to parse {}", path.display())) | ||
| } | ||
|
|
||
|
|
@@ -98,6 +100,34 @@ pub fn find_settings_toml<P: AsRef<Path>>(path: P) -> Result<Option<PathBuf>> { | |
| Ok(None) | ||
| } | ||
|
|
||
| fn check_required_version(contents: &str, path: &Path, table_path: &[&str]) -> Result<()> { | ||
| let value: toml::Value = | ||
| toml::from_str(contents).with_context(|| format!("Failed to parse {}", path.display()))?; | ||
| let mut current = &value; | ||
| for key in table_path { | ||
| match current.get(*key) { | ||
| Some(next) => { | ||
| current = next; | ||
| } | ||
| None => return Ok(()), | ||
| } | ||
| } | ||
|
|
||
| let required_version = current | ||
| .get("required-version") | ||
| .or_else(|| current.get("required_version")) | ||
jelle-openai marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .and_then(|value| value.as_str()); | ||
|
|
||
| let Some(required_version) = required_version else { | ||
| return Ok(()); | ||
| }; | ||
|
|
||
| let required_version = RequiredVersion::try_from(required_version.to_string()) | ||
| .with_context(|| format!("Failed to parse {}", path.display()))?; | ||
|
||
| validate_required_version(&required_version)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Derive target version from `required-version` in `pyproject.toml`, if | ||
| /// such a file exists in an ancestor directory. | ||
| pub fn find_fallback_target_version<P: AsRef<Path>>(path: P) -> Option<PythonVersion> { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.