Skip to content

Commit

Permalink
Make Clap an optional feature for ruff crate (#3498)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Mar 14, 2023
1 parent 78c2b0a commit 106a93e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 41 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions crates/ruff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -72,7 +72,6 @@ insta = { workspace = true, features = ["yaml", "redactions"] }
pretty_assertions = "1.3.0"
test-case = { workspace = true }


[features]
default = []
logical_lines = []
5 changes: 2 additions & 3 deletions crates/ruff/src/rule_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ pub(crate) enum Specificity {
Code5Chars,
}

#[cfg(feature = "clap")]
mod clap_completion {
use clap::builder::{PossibleValue, TypedValueParser, ValueParserFactory};
use strum::IntoEnumIterator;
Expand Down Expand Up @@ -316,9 +317,7 @@ mod clap_completion {
.map_err(|e| clap::Error::raw(clap::error::ErrorKind::InvalidValue, e))
}

fn possible_values(
&self,
) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> {
fn possible_values(&self) -> Option<Box<dyn Iterator<Item = PossibleValue> + '_>> {
Some(Box::new(
std::iter::once(PossibleValue::new("ALL").help("all rules")).chain(
Linter::iter()
Expand Down
38 changes: 8 additions & 30 deletions crates/ruff/src/settings/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -43,28 +44,6 @@ pub enum PythonVersion {
Py311,
}

impl FromStr for PythonVersion {
type Err = anyhow::Error;

fn from_str(string: &str) -> Result<Self, Self::Err> {
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<PythonVersion> for Pep440Version {
fn from(version: PythonVersion) -> Self {
let (major, minor) = version.as_tuple();
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand All @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub struct CheckArgs {
#[arg(long, value_enum, env = "RUFF_FORMAT")]
pub format: Option<SerializationFormat>,
/// The minimum Python version that should be supported.
#[arg(long)]
#[arg(long, value_enum)]
pub target_version: Option<PythonVersion>,
/// Path to the `pyproject.toml` or `ruff.toml` file to use for
/// configuration.
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Options:
--format <FORMAT>
Output serialization format for violations [env: RUFF_FORMAT=] [possible values: text, json, junit, grouped, github, gitlab, pylint, azure]
--target-version <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 <CONFIG>
Path to the `pyproject.toml` or `ruff.toml` file to use for configuration
--statistics
Expand Down

0 comments on commit 106a93e

Please sign in to comment.