mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 05:20:49 -05:00
Automatically set baseline D codes based on convention (#1574)
This commit is contained in:
@@ -924,7 +924,7 @@ pub fn sections(checker: &mut Checker, docstring: &Docstring, convention: Option
|
||||
numpy_section(checker, docstring, context);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
Some(Convention::Pep257) | None => {
|
||||
// First, interpret as NumPy-style sections.
|
||||
let mut found_numpy_section = false;
|
||||
for context in §ion_contexts(&lines, &SectionStyle::Numpy) {
|
||||
|
||||
@@ -4,6 +4,8 @@ use ruff_macros::ConfigurationOptions;
|
||||
use schemars::JsonSchema;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::registry::CheckCode;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash, JsonSchema)]
|
||||
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
|
||||
pub enum Convention {
|
||||
@@ -11,6 +13,161 @@ pub enum Convention {
|
||||
Google,
|
||||
/// Use NumPy-style docstrings.
|
||||
Numpy,
|
||||
/// Use PEP257-style docstrings.
|
||||
Pep257,
|
||||
}
|
||||
|
||||
impl Convention {
|
||||
pub fn codes(&self) -> Vec<CheckCode> {
|
||||
match self {
|
||||
Convention::Google => vec![
|
||||
// All errors except D203, D204, D213, D215, D400, D401, D404, D406, D407, D408,
|
||||
// D409 and D413.
|
||||
CheckCode::D100,
|
||||
CheckCode::D101,
|
||||
CheckCode::D102,
|
||||
CheckCode::D103,
|
||||
CheckCode::D104,
|
||||
CheckCode::D105,
|
||||
CheckCode::D106,
|
||||
CheckCode::D107,
|
||||
CheckCode::D200,
|
||||
CheckCode::D201,
|
||||
CheckCode::D202,
|
||||
// CheckCode::D203,
|
||||
// CheckCode::D204,
|
||||
CheckCode::D205,
|
||||
CheckCode::D206,
|
||||
CheckCode::D207,
|
||||
CheckCode::D208,
|
||||
CheckCode::D209,
|
||||
CheckCode::D210,
|
||||
CheckCode::D211,
|
||||
CheckCode::D212,
|
||||
// CheckCode::D213,
|
||||
CheckCode::D214,
|
||||
// CheckCode::D215,
|
||||
CheckCode::D300,
|
||||
CheckCode::D301,
|
||||
// CheckCode::D400,
|
||||
CheckCode::D402,
|
||||
CheckCode::D403,
|
||||
// CheckCode::D404,
|
||||
CheckCode::D405,
|
||||
// CheckCode::D406,
|
||||
// CheckCode::D407,
|
||||
// CheckCode::D408,
|
||||
// CheckCode::D409,
|
||||
CheckCode::D410,
|
||||
CheckCode::D411,
|
||||
CheckCode::D412,
|
||||
// CheckCode::D413,
|
||||
CheckCode::D414,
|
||||
CheckCode::D415,
|
||||
CheckCode::D416,
|
||||
CheckCode::D417,
|
||||
CheckCode::D418,
|
||||
CheckCode::D419,
|
||||
],
|
||||
Convention::Numpy => vec![
|
||||
// All errors except D107, D203, D212, D213, D402, D413, D415, D416, and D417.
|
||||
CheckCode::D100,
|
||||
CheckCode::D101,
|
||||
CheckCode::D102,
|
||||
CheckCode::D103,
|
||||
CheckCode::D104,
|
||||
CheckCode::D105,
|
||||
CheckCode::D106,
|
||||
// CheckCode::D107,
|
||||
CheckCode::D200,
|
||||
CheckCode::D201,
|
||||
CheckCode::D202,
|
||||
// CheckCode::D203,
|
||||
CheckCode::D204,
|
||||
CheckCode::D205,
|
||||
CheckCode::D206,
|
||||
CheckCode::D207,
|
||||
CheckCode::D208,
|
||||
CheckCode::D209,
|
||||
CheckCode::D210,
|
||||
CheckCode::D211,
|
||||
// CheckCode::D212,
|
||||
// CheckCode::D213,
|
||||
CheckCode::D214,
|
||||
CheckCode::D215,
|
||||
CheckCode::D300,
|
||||
CheckCode::D301,
|
||||
CheckCode::D400,
|
||||
// CheckCode::D402,
|
||||
CheckCode::D403,
|
||||
CheckCode::D404,
|
||||
CheckCode::D405,
|
||||
CheckCode::D406,
|
||||
CheckCode::D407,
|
||||
CheckCode::D408,
|
||||
CheckCode::D409,
|
||||
CheckCode::D410,
|
||||
CheckCode::D411,
|
||||
CheckCode::D412,
|
||||
// CheckCode::D413,
|
||||
CheckCode::D414,
|
||||
// CheckCode::D415,
|
||||
// CheckCode::D416,
|
||||
// CheckCode::D417,
|
||||
CheckCode::D418,
|
||||
CheckCode::D419,
|
||||
],
|
||||
Convention::Pep257 => vec![
|
||||
// All errors except D203, D212, D213, D214, D215, D404, D405, D406, D407, D408,
|
||||
// D409, D410, D411, D413, D415, D416 and D417.
|
||||
CheckCode::D100,
|
||||
CheckCode::D101,
|
||||
CheckCode::D102,
|
||||
CheckCode::D103,
|
||||
CheckCode::D104,
|
||||
CheckCode::D105,
|
||||
CheckCode::D106,
|
||||
CheckCode::D107,
|
||||
CheckCode::D200,
|
||||
CheckCode::D201,
|
||||
CheckCode::D202,
|
||||
// CheckCode::D203,
|
||||
CheckCode::D204,
|
||||
CheckCode::D205,
|
||||
CheckCode::D206,
|
||||
CheckCode::D207,
|
||||
CheckCode::D208,
|
||||
CheckCode::D209,
|
||||
CheckCode::D210,
|
||||
CheckCode::D211,
|
||||
// CheckCode::D212,
|
||||
// CheckCode::D213,
|
||||
// CheckCode::D214,
|
||||
// CheckCode::D215,
|
||||
CheckCode::D300,
|
||||
CheckCode::D301,
|
||||
CheckCode::D400,
|
||||
CheckCode::D402,
|
||||
CheckCode::D403,
|
||||
// CheckCode::D404,
|
||||
// CheckCode::D405,
|
||||
// CheckCode::D406,
|
||||
// CheckCode::D407,
|
||||
// CheckCode::D408,
|
||||
// CheckCode::D409,
|
||||
// CheckCode::D410,
|
||||
// CheckCode::D411,
|
||||
CheckCode::D412,
|
||||
// CheckCode::D413,
|
||||
CheckCode::D414,
|
||||
// CheckCode::D415,
|
||||
// CheckCode::D416,
|
||||
// CheckCode::D417,
|
||||
CheckCode::D418,
|
||||
CheckCode::D419,
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(
|
||||
@@ -26,9 +183,8 @@ pub struct Options {
|
||||
convention = "google"
|
||||
"#
|
||||
)]
|
||||
/// Whether to use Google-style or Numpy-style conventions when detecting
|
||||
/// docstring sections. By default, conventions will be inferred from
|
||||
/// the available sections.
|
||||
/// Whether to use Google-style or NumPy-style conventions or the PEP257
|
||||
/// defaults when analyzing docstring sections.
|
||||
pub convention: Option<Convention>,
|
||||
}
|
||||
|
||||
|
||||
@@ -114,6 +114,12 @@ impl Settings {
|
||||
.dummy_variable_rgx
|
||||
.unwrap_or_else(|| DEFAULT_DUMMY_VARIABLE_RGX.clone()),
|
||||
enabled: resolve_codes(
|
||||
config
|
||||
.pydocstyle
|
||||
.as_ref()
|
||||
.and_then(|pydocstyle| pydocstyle.convention)
|
||||
.map(|convention| convention.codes())
|
||||
.unwrap_or_default(),
|
||||
[CheckCodeSpec {
|
||||
select: &config
|
||||
.select
|
||||
@@ -135,6 +141,7 @@ impl Settings {
|
||||
fix: config.fix.unwrap_or(false),
|
||||
fix_only: config.fix_only.unwrap_or(false),
|
||||
fixable: resolve_codes(
|
||||
vec![],
|
||||
[CheckCodeSpec {
|
||||
select: &config.fixable.unwrap_or_else(|| CATEGORIES.to_vec()),
|
||||
ignore: &config.unfixable.unwrap_or_default(),
|
||||
@@ -384,8 +391,11 @@ struct CheckCodeSpec<'a> {
|
||||
|
||||
/// Given a set of selected and ignored prefixes, resolve the set of enabled
|
||||
/// error codes.
|
||||
fn resolve_codes<'a>(specs: impl Iterator<Item = CheckCodeSpec<'a>>) -> FxHashSet<CheckCode> {
|
||||
let mut codes: FxHashSet<CheckCode> = FxHashSet::default();
|
||||
fn resolve_codes<'a>(
|
||||
baseline: Vec<CheckCode>,
|
||||
specs: impl Iterator<Item = CheckCodeSpec<'a>>,
|
||||
) -> FxHashSet<CheckCode> {
|
||||
let mut codes: FxHashSet<CheckCode> = FxHashSet::from_iter(baseline);
|
||||
for spec in specs {
|
||||
for specificity in [
|
||||
SuffixLength::None,
|
||||
@@ -423,6 +433,7 @@ mod tests {
|
||||
#[test]
|
||||
fn check_codes() {
|
||||
let actual = resolve_codes(
|
||||
vec![],
|
||||
[CheckCodeSpec {
|
||||
select: &[CheckCodePrefix::W],
|
||||
ignore: &[],
|
||||
@@ -433,6 +444,7 @@ mod tests {
|
||||
assert_eq!(actual, expected);
|
||||
|
||||
let actual = resolve_codes(
|
||||
vec![],
|
||||
[CheckCodeSpec {
|
||||
select: &[CheckCodePrefix::W6],
|
||||
ignore: &[],
|
||||
@@ -443,6 +455,7 @@ mod tests {
|
||||
assert_eq!(actual, expected);
|
||||
|
||||
let actual = resolve_codes(
|
||||
vec![],
|
||||
[CheckCodeSpec {
|
||||
select: &[CheckCodePrefix::W],
|
||||
ignore: &[CheckCodePrefix::W292],
|
||||
@@ -453,6 +466,7 @@ mod tests {
|
||||
assert_eq!(actual, expected);
|
||||
|
||||
let actual = resolve_codes(
|
||||
vec![],
|
||||
[CheckCodeSpec {
|
||||
select: &[CheckCodePrefix::W605],
|
||||
ignore: &[CheckCodePrefix::W605],
|
||||
@@ -463,6 +477,7 @@ mod tests {
|
||||
assert_eq!(actual, expected);
|
||||
|
||||
let actual = resolve_codes(
|
||||
vec![],
|
||||
[
|
||||
CheckCodeSpec {
|
||||
select: &[CheckCodePrefix::W],
|
||||
@@ -479,6 +494,7 @@ mod tests {
|
||||
assert_eq!(actual, expected);
|
||||
|
||||
let actual = resolve_codes(
|
||||
vec![],
|
||||
[
|
||||
CheckCodeSpec {
|
||||
select: &[CheckCodePrefix::W],
|
||||
|
||||
Reference in New Issue
Block a user