From 106a93eab073a3864e81bcefab1854e5cf7864df Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 14 Mar 2023 11:02:05 -0400 Subject: [PATCH] Make Clap an optional feature for ruff crate (#3498) --- Cargo.lock | 15 ++++++++++-- crates/ruff/Cargo.toml | 3 +-- crates/ruff/src/rule_selector.rs | 5 ++-- crates/ruff/src/settings/types.rs | 38 +++++++------------------------ crates/ruff_cli/Cargo.toml | 4 ++-- crates/ruff_cli/src/args.rs | 2 +- docs/configuration.md | 2 +- 7 files changed, 28 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 164cdfaca96e1..94078d0797bb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -313,13 +313,14 @@ dependencies = [ [[package]] name = "clap_complete_command" -version = "0.4.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4160b4a4f72ef58bd766bad27c09e6ef1cc9d82a22f6a0f55d152985a4a48e31" +checksum = "183495371ea78d4c9ff638bfc6497d46fed2396e4f9c50aebc1278a4a9919a3d" dependencies = [ "clap 4.1.8", "clap_complete", "clap_complete_fig", + "clap_complete_nushell", ] [[package]] @@ -332,6 +333,16 @@ dependencies = [ "clap_complete", ] +[[package]] +name = "clap_complete_nushell" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7fa41f5e6aa83bd151b70fd0ceaee703d68cd669522795dc812df9edad1252c" +dependencies = [ + "clap 4.1.8", + "clap_complete", +] + [[package]] name = "clap_derive" version = "4.1.8" diff --git a/crates/ruff/Cargo.toml b/crates/ruff/Cargo.toml index bc67ee1b5bb55..3325a0d532e68 100644 --- a/crates/ruff/Cargo.toml +++ b/crates/ruff/Cargo.toml @@ -25,7 +25,7 @@ anyhow = { workspace = true } bisection = { version = "0.1.0" } bitflags = { workspace = true } chrono = { workspace = true } -clap = { workspace = true, features = ["derive", "env", "string"] } +clap = { workspace = true, features = ["derive", "string"], optional = true } colored = { workspace = true } dirs = { version = "4.0.0" } fern = { version = "0.6.1" } @@ -72,7 +72,6 @@ insta = { workspace = true, features = ["yaml", "redactions"] } pretty_assertions = "1.3.0" test-case = { workspace = true } - [features] default = [] logical_lines = [] diff --git a/crates/ruff/src/rule_selector.rs b/crates/ruff/src/rule_selector.rs index 9a01f03ed0803..55f34add8e6ea 100644 --- a/crates/ruff/src/rule_selector.rs +++ b/crates/ruff/src/rule_selector.rs @@ -277,6 +277,7 @@ pub(crate) enum Specificity { Code5Chars, } +#[cfg(feature = "clap")] mod clap_completion { use clap::builder::{PossibleValue, TypedValueParser, ValueParserFactory}; use strum::IntoEnumIterator; @@ -316,9 +317,7 @@ mod clap_completion { .map_err(|e| clap::Error::raw(clap::error::ErrorKind::InvalidValue, e)) } - fn possible_values( - &self, - ) -> Option + '_>> { + fn possible_values(&self) -> Option + '_>> { Some(Box::new( std::iter::once(PossibleValue::new("ALL").help("all rules")).chain( Linter::iter() diff --git a/crates/ruff/src/settings/types.rs b/crates/ruff/src/settings/types.rs index 18e0168d32e45..a487a2549643f 100644 --- a/crates/ruff/src/settings/types.rs +++ b/crates/ruff/src/settings/types.rs @@ -4,21 +4,21 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use std::string::ToString; -use anyhow::{anyhow, bail, Result}; -use clap::ValueEnum; +use anyhow::{bail, Result}; use globset::{Glob, GlobSet, GlobSetBuilder}; use pep440_rs::{Version as Pep440Version, VersionSpecifiers}; -use ruff_cache::{CacheKey, CacheKeyHasher}; -use ruff_macros::CacheKey; use rustc_hash::FxHashSet; use schemars::JsonSchema; use serde::{de, Deserialize, Deserializer, Serialize}; use strum::IntoEnumIterator; use strum_macros::EnumIter; +use ruff_cache::{CacheKey, CacheKeyHasher}; +use ruff_macros::CacheKey; + +use crate::fs; use crate::registry::Rule; use crate::rule_selector::RuleSelector; -use crate::{fs, warn_user_once}; #[derive( Clone, @@ -34,6 +34,7 @@ use crate::{fs, warn_user_once}; CacheKey, EnumIter, )] +#[cfg_attr(feature = "clap", derive(clap::ValueEnum))] #[serde(rename_all = "lowercase")] pub enum PythonVersion { Py37, @@ -43,28 +44,6 @@ pub enum PythonVersion { Py311, } -impl FromStr for PythonVersion { - type Err = anyhow::Error; - - fn from_str(string: &str) -> Result { - match string { - "py33" | "py34" | "py35" | "py36" => { - warn_user_once!( - "Specified a version below the minimum supported Python version. Defaulting \ - to Python 3.7." - ); - Ok(Self::Py37) - } - "py37" => Ok(Self::Py37), - "py38" => Ok(Self::Py38), - "py39" => Ok(Self::Py39), - "py310" => Ok(Self::Py310), - "py311" => Ok(Self::Py311), - _ => Err(anyhow!("Unknown version: {string} (try: \"py37\")")), - } - } -} - impl From for Pep440Version { fn from(version: PythonVersion) -> Self { let (major, minor) = version.as_tuple(); @@ -240,9 +219,8 @@ impl FromStr for PatternPrefixPair { } } -#[derive( - Clone, Copy, ValueEnum, PartialEq, Eq, Serialize, Deserialize, Debug, JsonSchema, Hash, -)] +#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Debug, JsonSchema, Hash)] +#[cfg_attr(feature = "clap", derive(clap::ValueEnum))] #[serde(rename_all = "kebab-case")] pub enum SerializationFormat { Text, diff --git a/crates/ruff_cli/Cargo.toml b/crates/ruff_cli/Cargo.toml index 53b82af2769b1..6571be0314cc1 100644 --- a/crates/ruff_cli/Cargo.toml +++ b/crates/ruff_cli/Cargo.toml @@ -22,7 +22,7 @@ name = "ruff" doc = false [dependencies] -ruff = { path = "../ruff" } +ruff = { path = "../ruff", features = ["clap"] } ruff_cache = { path = "../ruff_cache" } ruff_diagnostics = { path = "../ruff_diagnostics" } @@ -34,7 +34,7 @@ bitflags = { workspace = true } cachedir = { version = "0.3.0" } chrono = { workspace = true } clap = { workspace = true, features = ["derive", "env"] } -clap_complete_command = { version = "0.4.0" } +clap_complete_command = { version = "0.5.1" } clearscreen = { version = "2.0.0" } colored = { workspace = true } filetime = { workspace = true } diff --git a/crates/ruff_cli/src/args.rs b/crates/ruff_cli/src/args.rs index 335a39393725f..9103ccd245d46 100644 --- a/crates/ruff_cli/src/args.rs +++ b/crates/ruff_cli/src/args.rs @@ -99,7 +99,7 @@ pub struct CheckArgs { #[arg(long, value_enum, env = "RUFF_FORMAT")] pub format: Option, /// The minimum Python version that should be supported. - #[arg(long)] + #[arg(long, value_enum)] pub target_version: Option, /// Path to the `pyproject.toml` or `ruff.toml` file to use for /// configuration. diff --git a/docs/configuration.md b/docs/configuration.md index 19c562e79512a..5710074bb5c2d 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -211,7 +211,7 @@ Options: --format Output serialization format for violations [env: RUFF_FORMAT=] [possible values: text, json, junit, grouped, github, gitlab, pylint, azure] --target-version - The minimum Python version that should be supported + The minimum Python version that should be supported [possible values: py37, py38, py39, py310, py311] --config Path to the `pyproject.toml` or `ruff.toml` file to use for configuration --statistics