Skip to content

Commit

Permalink
Add rule WPS111 from wemake-python-styleguide
Browse files Browse the repository at this point in the history
  • Loading branch information
bo5o committed Dec 6, 2023
1 parent b4a050c commit 5e8962f
Show file tree
Hide file tree
Showing 39 changed files with 725 additions and 6 deletions.
14 changes: 14 additions & 0 deletions crates/flake8_to_ruff/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use ruff_workspace::options::{
Flake8AnnotationsOptions, Flake8BugbearOptions, Flake8BuiltinsOptions, Flake8ErrMsgOptions,
Flake8PytestStyleOptions, Flake8QuotesOptions, Flake8TidyImportsOptions, LintCommonOptions,
LintOptions, McCabeOptions, Options, Pep8NamingOptions, PydocstyleOptions,
WemakePythonStyleguideOptions,
};
use ruff_workspace::pyproject::Pyproject;

Expand Down Expand Up @@ -110,6 +111,7 @@ pub(crate) fn convert(
let mut mccabe = McCabeOptions::default();
let mut pep8_naming = Pep8NamingOptions::default();
let mut pydocstyle = PydocstyleOptions::default();
let mut wemake_python_styleguide = WemakePythonStyleguideOptions::default();
for (key, value) in flake8 {
if let Some(value) = value {
match key.as_str() {
Expand Down Expand Up @@ -346,6 +348,15 @@ pub(crate) fn convert(
}
}
}
// wemake-python-styleguide
"min-name-length" | "min_name_length" => match value.parse::<usize>() {
Ok(min_name_length) => {
wemake_python_styleguide.min_name_length = Some(min_name_length);
}
Err(e) => {
warn_user!("Unable to parse '{key}' property: {e}");
}
},
// Unknown
_ => {
warn_user!("Skipping unsupported property: {}", key);
Expand Down Expand Up @@ -397,6 +408,9 @@ pub(crate) fn convert(
if pydocstyle != PydocstyleOptions::default() {
lint_options.pydocstyle = Some(pydocstyle);
}
if wemake_python_styleguide != WemakePythonStyleguideOptions::default() {
lint_options.wemake_python_styleguide = Some(wemake_python_styleguide);
}

// Extract any settings from the existing `pyproject.toml`.
if let Some(black) = &external_config.black {
Expand Down
9 changes: 9 additions & 0 deletions crates/flake8_to_ruff/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum Plugin {
PandasVet,
Pyupgrade,
Tryceratops,
WemakePythonStyleguide,
}

impl FromStr for Plugin {
Expand Down Expand Up @@ -82,6 +83,7 @@ impl FromStr for Plugin {
"pandas-vet" => Ok(Plugin::PandasVet),
"pyupgrade" => Ok(Plugin::Pyupgrade),
"tryceratops" => Ok(Plugin::Tryceratops),
"wemake-python-styleguide" => Ok(Plugin::WemakePythonStyleguide),
_ => Err(anyhow!("Unknown plugin: {string}")),
}
}
Expand Down Expand Up @@ -126,6 +128,7 @@ impl fmt::Debug for Plugin {
Plugin::PandasVet => "pandas-vet",
Plugin::Pyupgrade => "pyupgrade",
Plugin::Tryceratops => "tryceratops",
Plugin::WemakePythonStyleguide => "wemake-python-styleguide",
}
)
}
Expand Down Expand Up @@ -167,6 +170,7 @@ impl From<&Plugin> for Linter {
Plugin::PandasVet => Linter::PandasVet,
Plugin::Pyupgrade => Linter::Pyupgrade,
Plugin::Tryceratops => Linter::Tryceratops,
Plugin::WemakePythonStyleguide => Linter::WemakePythonStyleguide,
}
}
}
Expand Down Expand Up @@ -282,6 +286,10 @@ pub(crate) fn infer_plugins_from_options(flake8: &HashMap<String, Option<String>
"max-string-length" | "max_string_length" => {
plugins.insert(Plugin::Flake8ErrMsg);
}
// wemake-python-styleguide
"min-name-length" | "min_name_length" => {
plugins.insert(Plugin::WemakePythonStyleguide);
}
_ => {}
}
}
Expand Down Expand Up @@ -327,6 +335,7 @@ pub(crate) fn infer_plugins_from_codes(selectors: &HashSet<RuleSelector>) -> Vec
Plugin::PEP8Naming,
Plugin::PandasVet,
Plugin::Tryceratops,
Plugin::WemakePythonStyleguide,
]
.into_iter()
.filter(|plugin| {
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,10 +895,11 @@ fn preview_enabled_all() {
exit_code: 1
----- stdout -----
-:1:1: E741 Ambiguous variable name: `I`
-:1:1: WPS111 Found too short name: `I`
-:1:1: D100 Missing docstring in public module
-:1:1: CPY001 Missing copyright notice at top of file
-:1:2: E225 [*] Missing whitespace around operator
Found 4 errors.
Found 5 errors.
[*] 1 fixable with the `--fix` option.
----- stderr -----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from matplotlib import pyplot as p # bad
from matplotlib import pyplot as plt # good

import numpy as n # bad
import numpy as np # good

x = 1 # bad
x_coordinate = 1 # good

y = 2 # bad
abscissa = 2 # good

z: int = 1 # bad
z_coordinate: int = 1 # good

c: str | bytes = "drop foo bar" # bad
command: str | bytes = "drop foo bar" # good

_ = "unused" # good


def f(a: int, b): # bad
pass


def some_function(arg1: int, arg2): # good
pass


class C: # bad
a: int # bad
b = 2 # bad

def __init__(self, i: str, j): # bad
self.i = i # bad
self.j = j # bad


class SomeClass: # good
attr1: int # good
attr2 = 2 # good

def __init__(self, attr3: str, attr4): # good
self.attr3 = attr3 # good
self.attr4 = attr4 # good


match command.split():
case ["quit" as c]: # bad
pass
case ["go", d]: # bad
pass
case ["drop", *o]: # bad
pass


match command.split():
case ["quit" as cmd]: # good
pass
case ["go", direction]: # good
pass
case ["drop", *objects]: # good
pass
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::rules::{
flake8_logging_format, flake8_pie, flake8_print, flake8_pyi, flake8_pytest_style, flake8_self,
flake8_simplify, flake8_tidy_imports, flake8_trio, flake8_use_pathlib, flynt, numpy,
pandas_vet, pep8_naming, pycodestyle, pyflakes, pygrep_hooks, pylint, pyupgrade, refurb, ruff,
wemake_python_styleguide,
};
use crate::settings::types::PythonVersion;

Expand Down Expand Up @@ -236,6 +237,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
checker.diagnostics.push(diagnostic);
}
}
if checker.enabled(Rule::TooShortName) {
wemake_python_styleguide::rules::too_short_name(checker, expr);
}
if let ScopeKind::Class(class_def) = checker.semantic.current_scope().kind {
if checker.enabled(Rule::BuiltinAttributeShadowing) {
flake8_builtins::rules::builtin_attribute_shadowing(
Expand Down Expand Up @@ -322,6 +326,9 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
if checker.enabled(Rule::PrivateMemberAccess) {
flake8_self::rules::private_member_access(checker, expr);
}
if checker.enabled(Rule::TooShortName) {
wemake_python_styleguide::rules::too_short_name(checker, expr);
}
if checker.enabled(Rule::CollectionsNamedTuple) {
flake8_pyi::rules::collections_named_tuple(checker, expr);
}
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(super) use expression::expression;
pub(super) use module::module;
pub(super) use parameter::parameter;
pub(super) use parameters::parameters;
pub(super) use pattern::pattern;
pub(super) use statement::statement;
pub(super) use suite::suite;
pub(super) use unresolved_references::unresolved_references;
Expand All @@ -24,6 +25,7 @@ mod expression;
mod module;
mod parameter;
mod parameters;
mod pattern;
mod statement;
mod suite;
mod unresolved_references;
5 changes: 4 additions & 1 deletion crates/ruff_linter/src/checkers/ast/analyze/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
use crate::codes::Rule;
use crate::rules::{flake8_builtins, pep8_naming, pycodestyle};
use crate::rules::{flake8_builtins, pep8_naming, pycodestyle, wemake_python_styleguide};

/// Run lint rules over a [`Parameter`] syntax node.
pub(crate) fn parameter(parameter: &Parameter, checker: &mut Checker) {
Expand All @@ -26,4 +26,7 @@ pub(crate) fn parameter(parameter: &Parameter, checker: &mut Checker) {
if checker.enabled(Rule::BuiltinArgumentShadowing) {
flake8_builtins::rules::builtin_argument_shadowing(checker, parameter);
}
if checker.enabled(Rule::TooShortName) {
wemake_python_styleguide::rules::too_short_name(checker, parameter);
}
}
24 changes: 24 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use ruff_python_ast::{Pattern, PatternMatchAs, PatternMatchMapping, PatternMatchStar};

use crate::checkers::ast::Checker;
use crate::codes::Rule;
use crate::rules::wemake_python_styleguide;

/// Run lint rules over a [`Pattern`] syntax node.
pub(crate) fn pattern(pattern: &Pattern, checker: &mut Checker) {
if let Pattern::MatchAs(PatternMatchAs {
name: Some(name), ..
})
| Pattern::MatchStar(PatternMatchStar {
name: Some(name),
range: _,
})
| Pattern::MatchMapping(PatternMatchMapping {
rest: Some(name), ..
}) = pattern
{
if checker.enabled(Rule::TooShortName) {
wemake_python_styleguide::rules::too_short_name(checker, name);
}
}
}
13 changes: 13 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::rules::{
flake8_pytest_style, flake8_raise, flake8_return, flake8_simplify, flake8_slots,
flake8_tidy_imports, flake8_trio, flake8_type_checking, mccabe, pandas_vet, pep8_naming,
perflint, pycodestyle, pyflakes, pygrep_hooks, pylint, pyupgrade, refurb, ruff, tryceratops,
wemake_python_styleguide,
};
use crate::settings::types::PythonVersion;

Expand Down Expand Up @@ -344,6 +345,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.enabled(Rule::FStringDocstring) {
flake8_bugbear::rules::f_string_docstring(checker, body);
}
if checker.enabled(Rule::TooShortName) {
wemake_python_styleguide::rules::too_short_name(checker, name);
}
if let ScopeKind::Class(class_def) = checker.semantic.current_scope().kind {
if checker.enabled(Rule::BuiltinAttributeShadowing) {
flake8_builtins::rules::builtin_method_shadowing(
Expand Down Expand Up @@ -534,6 +538,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.enabled(Rule::BadDunderMethodName) {
pylint::rules::bad_dunder_method_name(checker, body);
}
if checker.enabled(Rule::TooShortName) {
wemake_python_styleguide::rules::too_short_name(checker, name);
}
}
Stmt::Import(ast::StmtImport { names, range: _ }) => {
if checker.enabled(Rule::MultipleImportsOnOneLine) {
Expand Down Expand Up @@ -574,6 +581,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
);
}
}
if checker.enabled(Rule::TooShortName) {
wemake_python_styleguide::rules::too_short_name(checker, alias);
}
if checker.enabled(Rule::Debugger) {
if let Some(diagnostic) =
flake8_debugger::rules::debugger_import(stmt, None, &alias.name)
Expand Down Expand Up @@ -969,6 +979,9 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
checker.diagnostics.push(diagnostic);
}
}
if checker.enabled(Rule::TooShortName) {
wemake_python_styleguide::rules::too_short_name(checker, asname);
}
if !checker.source_type.is_stub() {
if checker.enabled(Rule::UselessImportAlias) {
pylint::rules::useless_import_alias(checker, alias);
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,9 @@ where

// Step 2: Traversal
walk_pattern(self, pattern);

// Step 3: Analysis
analyze::pattern(pattern, self);
}

fn visit_body(&mut self, body: &'b [Stmt]) {
Expand Down
8 changes: 8 additions & 0 deletions crates/ruff_linter/src/checkers/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ruff_source_file::Locator;
use crate::registry::Rule;
use crate::rules::flake8_no_pep420::rules::implicit_namespace_package;
use crate::rules::pep8_naming::rules::invalid_module_name;
use crate::rules::wemake_python_styleguide::rules::too_short_module_name;
use crate::settings::LinterSettings;

pub(crate) fn check_file_path(
Expand Down Expand Up @@ -41,5 +42,12 @@ pub(crate) fn check_file_path(
}
}

// wemake-python-styleguide
if settings.rules.enabled(Rule::TooShortName) {
if let Some(diagnostic) = too_short_module_name(path, package, settings) {
diagnostics.push(diagnostic);
}
}

diagnostics
}
3 changes: 3 additions & 0 deletions crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,9 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Flake8Logging, "007") => (RuleGroup::Preview, rules::flake8_logging::rules::ExceptionWithoutExcInfo),
(Flake8Logging, "009") => (RuleGroup::Preview, rules::flake8_logging::rules::UndocumentedWarn),

// wemake-python-styleguide
(WemakePythonStyleguide, "111") => (RuleGroup::Preview, rules::wemake_python_styleguide::rules::TooShortName),

_ => return None,
})
}
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn check_path(
if settings
.rules
.iter_enabled()
.any(|rule_code| rule_code.lint_source().is_filesystem())
.any(|rule_code| rule_code.lint_source().is_filesystem() || rule_code == Rule::TooShortName)
{
diagnostics.extend(check_file_path(path, package, locator, indexer, settings));
}
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff_linter/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ pub enum Linter {
/// Ruff-specific rules
#[prefix = "RUF"]
Ruff,
/// [wemake-python-styleguide](https://pypi.org/project/wemake-python-styleguide/)
#[prefix = "WPS"]
WemakePythonStyleguide,
}

pub trait RuleNamespace: Sized {
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ pub mod pyupgrade;
pub mod refurb;
pub mod ruff;
pub mod tryceratops;
pub mod wemake_python_styleguide;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(super) mod naming;
Loading

0 comments on commit 5e8962f

Please sign in to comment.