Skip to content

Commit 3ce1a5f

Browse files
committed
use parser::PythonVersion in red_knot
1 parent d01ded8 commit 3ce1a5f

7 files changed

Lines changed: 37 additions & 74 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/red_knot/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ red_knot_python_semantic = { workspace = true }
1616
red_knot_project = { workspace = true, features = ["zstd"] }
1717
red_knot_server = { workspace = true }
1818
ruff_db = { workspace = true, features = ["os", "cache"] }
19-
ruff_python_parser = { workspace = true }
19+
ruff_python_parser = { workspace = true, features = ["clap"] }
2020

2121
anyhow = { workspace = true }
2222
chrono = { workspace = true }

crates/red_knot/src/args.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::logging::Verbosity;
2-
use crate::python_version::PythonVersion;
32
use clap::{ArgAction, ArgMatches, Error, Parser};
43
use red_knot_project::metadata::options::{EnvironmentOptions, Options, TerminalOptions};
54
use red_knot_project::metadata::value::{RangedValue, RelativePathBuf};
65
use red_knot_python_semantic::lint;
76
use ruff_db::system::SystemPathBuf;
7+
use ruff_python_parser::python_version::PythonVersion;
88

99
#[derive(Debug, Parser)]
1010
#[command(
@@ -94,9 +94,7 @@ impl CheckCommand {
9494

9595
Options {
9696
environment: Some(EnvironmentOptions {
97-
python_version: self
98-
.python_version
99-
.map(|version| RangedValue::cli(version.into())),
97+
python_version: self.python_version.map(RangedValue::cli),
10098
venv_path: self.venv_path.map(RelativePathBuf::cli),
10199
typeshed: self.typeshed.map(RelativePathBuf::cli),
102100
extra_paths: self.extra_search_path.map(|extra_search_paths| {

crates/red_knot/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use salsa::plumbing::ZalsaDatabase;
2121

2222
mod args;
2323
mod logging;
24-
mod python_version;
2524
mod version;
2625

2726
#[allow(clippy::print_stdout, clippy::unnecessary_wraps, clippy::print_stderr)]

crates/red_knot/src/python_version.rs

Lines changed: 0 additions & 68 deletions
This file was deleted.

crates/ruff_python_parser/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ruff_text_size = { workspace = true }
1919

2020
bitflags = { workspace = true }
2121
bstr = { workspace = true }
22+
clap = { workspace = true, optional = true }
2223
compact_str = { workspace = true }
2324
memchr = { workspace = true }
2425
rustc-hash = { workspace = true }

crates/ruff_python_parser/src/python_version.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,35 @@ mod schemars {
166166
}
167167
}
168168
}
169+
170+
#[cfg(feature = "clap")]
171+
mod clap {
172+
use clap::builder::PossibleValue;
173+
174+
impl clap::ValueEnum for super::PythonVersion {
175+
fn value_variants<'a>() -> &'a [Self] {
176+
&[
177+
Self::PY37,
178+
Self::PY38,
179+
Self::PY39,
180+
Self::PY310,
181+
Self::PY311,
182+
Self::PY312,
183+
Self::PY313,
184+
]
185+
}
186+
187+
fn to_possible_value(&self) -> Option<PossibleValue> {
188+
match (self.major, self.minor) {
189+
(3, 7) => Some(PossibleValue::new("3.7")),
190+
(3, 8) => Some(PossibleValue::new("3.8")),
191+
(3, 9) => Some(PossibleValue::new("3.9")),
192+
(3, 10) => Some(PossibleValue::new("3.10")),
193+
(3, 11) => Some(PossibleValue::new("3.11")),
194+
(3, 12) => Some(PossibleValue::new("3.12")),
195+
(3, 13) => Some(PossibleValue::new("3.13")),
196+
_ => None,
197+
}
198+
}
199+
}
200+
}

0 commit comments

Comments
 (0)