diff --git a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs index 05129879c2..aac8295da1 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs @@ -9,6 +9,21 @@ use crate::checkers::logical_lines::LogicalLinesContext; use super::LogicalLine; +/// ## What it does +/// Checks for missing whitespace after `,`, `;`, and `:`. +/// +/// ## Why is this bad? +/// Missing whitespace after `,`, `;`, and `:` makes the code harder to read. +/// +/// ## Example +/// ```python +/// a = (1,2) +/// ``` +/// +/// Use instead: +/// ```python +/// a = (1, 2) +/// ``` #[violation] pub struct MissingWhitespace { token: TokenKind, diff --git a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs index 8d72744beb..c0df9e5d21 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs @@ -6,6 +6,26 @@ use ruff_python_parser::TokenKind; use crate::checkers::logical_lines::LogicalLinesContext; use crate::rules::pycodestyle::rules::logical_lines::LogicalLine; +/// ## What it does +/// Checks for missing whitespace after keywords. +/// +/// ## Why is this bad? +/// Missing whitespace after keywords makes the code harder to read. +/// +/// ## Example +/// ```python +/// if(True): +/// pass +/// ``` +/// +/// Use instead: +/// ```python +/// if (True): +/// pass +/// ``` +/// +/// ## References +/// - [Python documentation: Keywords](https://docs.python.org/3/reference/lexical_analysis.html#keywords) #[violation] pub struct MissingWhitespaceAfterKeyword; diff --git a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/whitespace_around_named_parameter_equals.rs b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/whitespace_around_named_parameter_equals.rs index ca0f6814e6..d7af34968e 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/whitespace_around_named_parameter_equals.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/whitespace_around_named_parameter_equals.rs @@ -7,6 +7,31 @@ use ruff_text_size::{TextRange, TextSize}; use crate::checkers::logical_lines::LogicalLinesContext; use crate::rules::pycodestyle::rules::logical_lines::{LogicalLine, LogicalLineToken}; +/// ## What it does +/// Checks for missing whitespace around the equals sign in an unannotated +/// function keyword parameter. +/// +/// ## Why is this bad? +/// According to [PEP 8], there should be no spaces around the equals sign in a +/// keyword parameter, if it is unannotated: +/// +/// > Don’t use spaces around the = sign when used to indicate a keyword +/// > argument, or when used to indicate a default value for an unannotated +/// > function parameter. +/// +/// ## Example +/// ```python +/// def add(a = 0) -> int: +/// return a + 1 +/// ``` +/// +/// Use instead: +/// ```python +/// def add(a = 0) -> int: +/// return a + 1 +/// ``` +/// +/// [PEP 8]: https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements #[violation] pub struct UnexpectedSpacesAroundKeywordParameterEquals; @@ -17,6 +42,31 @@ impl Violation for UnexpectedSpacesAroundKeywordParameterEquals { } } +/// ## What it does +/// Checks for missing whitespace around the equals sign in an annotated +/// function keyword parameter. +/// +/// ## Why is this bad? +/// According to [PEP 8], the spaces around the equals sign in a keyword +/// parameter should only be omitted when the parameter is unannotated: +/// +/// > Don’t use spaces around the = sign when used to indicate a keyword +/// > argument, or when used to indicate a default value for an unannotated +/// > function parameter. +/// +/// ## Example +/// ```python +/// def add(a: int=0) -> int: +/// return a + 1 +/// ``` +/// +/// Use instead: +/// ```python +/// def add(a: int = 0) -> int: +/// return a + 1 +/// ``` +/// +/// [PEP 8]: https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements #[violation] pub struct MissingWhitespaceAroundParameterEquals; diff --git a/scripts/check_docs_formatted.py b/scripts/check_docs_formatted.py index 4ad6dc40dd..2b55a83127 100755 --- a/scripts/check_docs_formatted.py +++ b/scripts/check_docs_formatted.py @@ -37,10 +37,13 @@ KNOWN_FORMATTING_VIOLATIONS = [ "indentation-with-invalid-multiple", "line-too-long", "missing-trailing-comma", + "missing-whitespace", + "missing-whitespace-after-keyword", "missing-whitespace-around-arithmetic-operator", "missing-whitespace-around-bitwise-or-shift-operator", "missing-whitespace-around-modulo-operator", "missing-whitespace-around-operator", + "missing-whitespace-around-parameter-equals", "multi-line-implicit-string-concatenation", "multiple-leading-hashes-for-block-comment", "multiple-spaces-after-comma", @@ -66,6 +69,7 @@ KNOWN_FORMATTING_VIOLATIONS = [ "triple-single-quotes", "under-indentation", "unexpected-indentation-comment", + "unexpected-spaces-around-keyword-parameter-equals", "unicode-kind-prefix", "unnecessary-class-parentheses", "useless-semicolon",