From e6dd700dfd9402d91af3fa2504b73a8b7ef9d749 Mon Sep 17 00:00:00 2001 From: harupy Date: Sun, 20 Aug 2023 16:30:52 +0900 Subject: [PATCH 1/6] Add doc for E275 --- .../missing_whitespace_after_keyword.rs | 18 ++++++++++++++++++ scripts/check_docs_formatted.py | 1 + 2 files changed, 19 insertions(+) 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 8d72744bebfca..bc041033c72f8 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,24 @@ 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 +/// from a import(b, c) +/// ``` +/// +/// Use instead: +/// ```python +/// from a import (b, c) +/// ``` +/// +/// ## References +/// - [Python documentation: Keywords](https://docs.python.org/3/reference/lexical_analysis.html#keywords) #[violation] pub struct MissingWhitespaceAfterKeyword; diff --git a/scripts/check_docs_formatted.py b/scripts/check_docs_formatted.py index a60c992de6102..e8462b88767c0 100755 --- a/scripts/check_docs_formatted.py +++ b/scripts/check_docs_formatted.py @@ -37,6 +37,7 @@ "indentation-with-invalid-multiple", "line-too-long", "missing-trailing-comma", + "missing-whitespace-after-keyword", "missing-whitespace-around-arithmetic-operator", "missing-whitespace-around-bitwise-or-shift-operator", "missing-whitespace-around-modulo-operator", From dd85f410be0f78cad0b450ed0a29ed057aacdcb5 Mon Sep 17 00:00:00 2001 From: harupy Date: Sun, 20 Aug 2023 21:22:42 +0900 Subject: [PATCH 2/6] Fix examples --- .../rules/logical_lines/missing_whitespace_after_keyword.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 bc041033c72f8..c0df9e5d210de 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 @@ -14,12 +14,14 @@ use crate::rules::pycodestyle::rules::logical_lines::LogicalLine; /// /// ## Example /// ```python -/// from a import(b, c) +/// if(True): +/// pass /// ``` /// /// Use instead: /// ```python -/// from a import (b, c) +/// if (True): +/// pass /// ``` /// /// ## References From cd4f401bf8e8045b1097e6568b7957b8802a32d7 Mon Sep 17 00:00:00 2001 From: harupy Date: Sun, 20 Aug 2023 21:28:58 +0900 Subject: [PATCH 3/6] E231 --- .../rules/logical_lines/missing_whitespace.rs | 15 +++++++++++++++ scripts/check_docs_formatted.py | 1 + 2 files changed, 16 insertions(+) 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 05129879c2035..aac8295da14cc 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/scripts/check_docs_formatted.py b/scripts/check_docs_formatted.py index e8462b88767c0..0929ad2626025 100755 --- a/scripts/check_docs_formatted.py +++ b/scripts/check_docs_formatted.py @@ -37,6 +37,7 @@ "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", From 5ffc2f7310b7a709b0f032a124fc46cfca49db1b Mon Sep 17 00:00:00 2001 From: harupy Date: Sun, 20 Aug 2023 21:33:15 +0900 Subject: [PATCH 4/6] E251 --- ...hitespace_around_named_parameter_equals.rs | 23 +++++++++++++++++++ scripts/check_docs_formatted.py | 1 + 2 files changed, 24 insertions(+) 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 ca0f6814e63d0..688704bbec10d 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,29 @@ 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 unexpected spaces around keyword / parameter equals. +/// +/// ## Why is this bad? +/// Unexpected spaces around keyword / parameter equals makes the code harder to read. +/// +/// ## Example +/// ```python +/// def foo(bar = 0): +/// pass +/// +/// +/// foo(bar = 1) +/// ``` +/// +/// Use instead: +/// ```python +/// def foo(bar=0): +/// pass +/// +/// +/// foo(bar=1) +/// ``` #[violation] pub struct UnexpectedSpacesAroundKeywordParameterEquals; diff --git a/scripts/check_docs_formatted.py b/scripts/check_docs_formatted.py index 0929ad2626025..dc7480e628e4d 100755 --- a/scripts/check_docs_formatted.py +++ b/scripts/check_docs_formatted.py @@ -68,6 +68,7 @@ "triple-single-quotes", "under-indentation", "unexpected-indentation-comment", + "unexpected-spaces-around-keyword-parameter-equals", "unicode-kind-prefix", "unnecessary-class-parentheses", "useless-semicolon", From cdcd6b0119e4a4c302673339d49a14c4046a0ccc Mon Sep 17 00:00:00 2001 From: harupy Date: Sun, 20 Aug 2023 21:37:10 +0900 Subject: [PATCH 5/6] E252 --- .../whitespace_around_named_parameter_equals.rs | 17 +++++++++++++++++ scripts/check_docs_formatted.py | 1 + 2 files changed, 18 insertions(+) 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 688704bbec10d..0a0d4adc62dbd 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 @@ -40,6 +40,23 @@ impl Violation for UnexpectedSpacesAroundKeywordParameterEquals { } } +/// ## What it does +/// Checks for missing whitespace around parameter equals. +/// +/// ## Why is this bad? +/// Missing whitespace around parameter equals makes the code harder to read. +/// +/// ## Example +/// ```python +/// def add(a: int=0) -> int: +/// return a + 1 +/// ``` +/// +/// Use instead: +/// ```python +/// def add(a: int = 0) -> int: +/// return a + 1 +/// ``` #[violation] pub struct MissingWhitespaceAroundParameterEquals; diff --git a/scripts/check_docs_formatted.py b/scripts/check_docs_formatted.py index dc7480e628e4d..1b61b8ba5a8dd 100755 --- a/scripts/check_docs_formatted.py +++ b/scripts/check_docs_formatted.py @@ -43,6 +43,7 @@ "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", From 1b7588db95d5fbe4099c09d66dd90adc2dc8e74e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 20 Aug 2023 10:40:06 -0400 Subject: [PATCH 6/6] Tweaks --- ...hitespace_around_named_parameter_equals.rs | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) 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 0a0d4adc62dbd..d7af34968ef4c 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 @@ -8,28 +8,30 @@ use crate::checkers::logical_lines::LogicalLinesContext; use crate::rules::pycodestyle::rules::logical_lines::{LogicalLine, LogicalLineToken}; /// ## What it does -/// Checks for unexpected spaces around keyword / parameter equals. +/// Checks for missing whitespace around the equals sign in an unannotated +/// function keyword parameter. /// /// ## Why is this bad? -/// Unexpected spaces around keyword / parameter equals makes the code harder to read. +/// 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 foo(bar = 0): -/// pass -/// -/// -/// foo(bar = 1) +/// def add(a = 0) -> int: +/// return a + 1 /// ``` /// /// Use instead: /// ```python -/// def foo(bar=0): -/// pass -/// -/// -/// foo(bar=1) +/// 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; @@ -41,10 +43,16 @@ impl Violation for UnexpectedSpacesAroundKeywordParameterEquals { } /// ## What it does -/// Checks for missing whitespace around parameter equals. +/// Checks for missing whitespace around the equals sign in an annotated +/// function keyword parameter. /// /// ## Why is this bad? -/// Missing whitespace around parameter equals makes the code harder to read. +/// 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 @@ -57,6 +65,8 @@ impl Violation for UnexpectedSpacesAroundKeywordParameterEquals { /// 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;