Skip to content
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

Warn user when D203 and D211 are enabled #1576

Merged
merged 1 commit into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3798,6 +3798,14 @@ impl Check {
}
}

/// Pairs of checks that shouldn't be enabled together.
pub const INCOMPATIBLE_CODES: &[(CheckCode, CheckCode, &str)] = &[(
CheckCode::D203,
CheckCode::D211,
"`D203` (OneBlankLineBeforeClass) and `D211` (NoBlankLinesBeforeClass) are incompatible. \
Consider adding `D203` to `ignore`.",
)];

/// A hash map from deprecated `CheckCodePrefix` to latest `CheckCodePrefix`.
pub static PREFIX_REDIRECTS: Lazy<FxHashMap<&'static str, CheckCodePrefix>> = Lazy::new(|| {
FxHashMap::from_iter([
Expand Down
25 changes: 20 additions & 5 deletions src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Result};
use colored::Colorize;
use globset::{Glob, GlobMatcher, GlobSet};
use itertools::Itertools;
use once_cell::sync::Lazy;
Expand All @@ -14,7 +15,7 @@ use regex::Regex;
use rustc_hash::FxHashSet;

use crate::cache::cache_dir;
use crate::registry::CheckCode;
use crate::registry::{CheckCode, INCOMPATIBLE_CODES};
use crate::registry_gen::{CheckCodePrefix, SuffixLength, CATEGORIES};
use crate::settings::configuration::Configuration;
use crate::settings::types::{
Expand All @@ -23,7 +24,7 @@ use crate::settings::types::{
use crate::{
flake8_annotations, flake8_bugbear, flake8_errmsg, flake8_import_conventions,
flake8_pytest_style, flake8_quotes, flake8_tidy_imports, flake8_unused_arguments, isort,
mccabe, pep8_naming, pydocstyle, pyupgrade,
mccabe, one_time_warning, pep8_naming, pydocstyle, pyupgrade,
};

pub mod configuration;
Expand All @@ -32,7 +33,6 @@ pub mod options;
pub mod options_base;
pub mod pyproject;
pub mod types;

const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Debug)]
Expand Down Expand Up @@ -113,7 +113,7 @@ impl Settings {
dummy_variable_rgx: config
.dummy_variable_rgx
.unwrap_or_else(|| DEFAULT_DUMMY_VARIABLE_RGX.clone()),
enabled: resolve_codes(
enabled: validate_enabled(resolve_codes(
config
.pydocstyle
.as_ref()
Expand All @@ -134,7 +134,7 @@ impl Settings {
.zip(config.extend_ignore.iter())
.map(|(select, ignore)| CheckCodeSpec { select, ignore }),
),
),
)),
exclude: resolve_globset(config.exclude.unwrap_or_else(|| DEFAULT_EXCLUDE.clone()))?,
extend_exclude: resolve_globset(config.extend_exclude)?,
external: FxHashSet::from_iter(config.external.unwrap_or_default()),
Expand Down Expand Up @@ -422,6 +422,21 @@ fn resolve_codes<'a>(
codes
}

/// Warn if the set of enabled codes contains any incompatibilities.
fn validate_enabled(enabled: FxHashSet<CheckCode>) -> FxHashSet<CheckCode> {
for (a, b, message) in INCOMPATIBLE_CODES {
if enabled.contains(a) && enabled.contains(b) {
one_time_warning!(
"{}{} {}",
"warning".yellow().bold(),
":".bold(),
message.bold()
);
}
}
enabled
}

#[cfg(test)]
mod tests {
use rustc_hash::FxHashSet;
Expand Down