diff --git a/src/checkers/ast.rs b/src/checkers/ast.rs index 2b202aba7d..8d89612c7d 100644 --- a/src/checkers/ast.rs +++ b/src/checkers/ast.rs @@ -397,10 +397,9 @@ where .. } => { if self.settings.enabled.contains(&CheckCode::E743) { - if let Some(check) = pycodestyle::checks::ambiguous_function_name( - name, - Range::from_located(stmt), - ) { + if let Some(check) = pycodestyle::checks::ambiguous_function_name(name, || { + helpers::identifier_range(stmt, self.locator) + }) { self.add_check(check); } } @@ -585,9 +584,9 @@ where } if self.settings.enabled.contains(&CheckCode::E742) { - if let Some(check) = - pycodestyle::checks::ambiguous_class_name(name, Range::from_located(stmt)) - { + if let Some(check) = pycodestyle::checks::ambiguous_class_name(name, || { + helpers::identifier_range(stmt, self.locator) + }) { self.add_check(check); } } diff --git a/src/flake8_annotations/plugins.rs b/src/flake8_annotations/plugins.rs index f98d9f52ec..846c9c12cd 100644 --- a/src/flake8_annotations/plugins.rs +++ b/src/flake8_annotations/plugins.rs @@ -3,7 +3,7 @@ use rustpython_ast::{Constant, Expr, ExprKind, Stmt, StmtKind}; use crate::ast::types::Range; use crate::ast::visitor::Visitor; -use crate::ast::{cast, visitor}; +use crate::ast::{cast, helpers, visitor}; use crate::checkers::ast::Checker; use crate::checks::{CheckCode, CheckKind}; use crate::docstrings::definition::{Definition, DefinitionKind}; @@ -167,7 +167,7 @@ pub fn definition(checker: &mut Checker, definition: &Definition, visibility: &V if checker.settings.enabled.contains(&CheckCode::ANN201) { checker.add_check(Check::new( CheckKind::MissingReturnTypePublicFunction(name.to_string()), - Range::from_located(stmt), + helpers::identifier_range(stmt, checker.locator), )); } } @@ -175,7 +175,7 @@ pub fn definition(checker: &mut Checker, definition: &Definition, visibility: &V if checker.settings.enabled.contains(&CheckCode::ANN202) { checker.add_check(Check::new( CheckKind::MissingReturnTypePrivateFunction(name.to_string()), - Range::from_located(stmt), + helpers::identifier_range(stmt, checker.locator), )); } } @@ -309,14 +309,14 @@ pub fn definition(checker: &mut Checker, definition: &Definition, visibility: &V if checker.settings.enabled.contains(&CheckCode::ANN206) { checker.add_check(Check::new( CheckKind::MissingReturnTypeClassMethod(name.to_string()), - Range::from_located(stmt), + helpers::identifier_range(stmt, checker.locator), )); } } else if visibility::is_staticmethod(checker, cast::decorator_list(stmt)) { if checker.settings.enabled.contains(&CheckCode::ANN205) { checker.add_check(Check::new( CheckKind::MissingReturnTypeStaticMethod(name.to_string()), - Range::from_located(stmt), + helpers::identifier_range(stmt, checker.locator), )); } } else if visibility::is_init(stmt) { @@ -328,7 +328,7 @@ pub fn definition(checker: &mut Checker, definition: &Definition, visibility: &V { let mut check = Check::new( CheckKind::MissingReturnTypeSpecialMethod(name.to_string()), - Range::from_located(stmt), + helpers::identifier_range(stmt, checker.locator), ); if checker.patch(check.kind.code()) { match fixes::add_return_none_annotation(checker.locator, stmt) { @@ -343,7 +343,7 @@ pub fn definition(checker: &mut Checker, definition: &Definition, visibility: &V if checker.settings.enabled.contains(&CheckCode::ANN204) { checker.add_check(Check::new( CheckKind::MissingReturnTypeSpecialMethod(name.to_string()), - Range::from_located(stmt), + helpers::identifier_range(stmt, checker.locator), )); } } else { @@ -352,7 +352,7 @@ pub fn definition(checker: &mut Checker, definition: &Definition, visibility: &V if checker.settings.enabled.contains(&CheckCode::ANN201) { checker.add_check(Check::new( CheckKind::MissingReturnTypePublicFunction(name.to_string()), - Range::from_located(stmt), + helpers::identifier_range(stmt, checker.locator), )); } } @@ -360,7 +360,7 @@ pub fn definition(checker: &mut Checker, definition: &Definition, visibility: &V if checker.settings.enabled.contains(&CheckCode::ANN202) { checker.add_check(Check::new( CheckKind::MissingReturnTypePrivateFunction(name.to_string()), - Range::from_located(stmt), + helpers::identifier_range(stmt, checker.locator), )); } } diff --git a/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__allow_overload.snap b/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__allow_overload.snap index 630a15dc48..4e4fe5b7cc 100644 --- a/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__allow_overload.snap +++ b/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__allow_overload.snap @@ -6,9 +6,9 @@ expression: checks MissingReturnTypePublicFunction: bar location: row: 29 - column: 4 + column: 8 end_location: - row: 30 - column: 16 + row: 29 + column: 11 fix: ~ diff --git a/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__defaults.snap b/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__defaults.snap index 96fbe88020..328fcf5168 100644 --- a/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__defaults.snap +++ b/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__defaults.snap @@ -6,10 +6,10 @@ expression: checks MissingReturnTypePublicFunction: foo location: row: 4 - column: 0 + column: 4 end_location: - row: 5 - column: 8 + row: 4 + column: 7 fix: ~ - kind: MissingTypeFunctionArgument: a @@ -33,10 +33,10 @@ expression: checks MissingReturnTypePublicFunction: foo location: row: 9 - column: 0 + column: 4 end_location: - row: 10 - column: 8 + row: 9 + column: 7 fix: ~ - kind: MissingTypeFunctionArgument: b @@ -60,19 +60,19 @@ expression: checks MissingReturnTypePublicFunction: foo location: row: 19 - column: 0 + column: 4 end_location: - row: 20 - column: 8 + row: 19 + column: 7 fix: ~ - kind: MissingReturnTypePublicFunction: foo location: row: 24 - column: 0 + column: 4 end_location: - row: 25 - column: 8 + row: 24 + column: 7 fix: ~ - kind: DynamicallyTypedExpression: a diff --git a/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__mypy_init_return.snap b/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__mypy_init_return.snap index da4eb5e8d6..1b93f071b5 100644 --- a/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__mypy_init_return.snap +++ b/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__mypy_init_return.snap @@ -6,10 +6,10 @@ expression: checks MissingReturnTypeSpecialMethod: __init__ location: row: 5 - column: 4 + column: 8 end_location: - row: 6 - column: 11 + row: 5 + column: 16 fix: content: " -> None" location: @@ -22,10 +22,10 @@ expression: checks MissingReturnTypeSpecialMethod: __init__ location: row: 11 - column: 4 + column: 8 end_location: - row: 12 - column: 11 + row: 11 + column: 16 fix: content: " -> None" location: @@ -38,9 +38,9 @@ expression: checks MissingReturnTypePrivateFunction: __init__ location: row: 40 - column: 0 + column: 4 end_location: - row: 41 - column: 7 + row: 40 + column: 12 fix: ~ diff --git a/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__suppress_none_returning.snap b/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__suppress_none_returning.snap index f1cbb1c020..6ccd83cc80 100644 --- a/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__suppress_none_returning.snap +++ b/src/flake8_annotations/snapshots/ruff__flake8_annotations__tests__suppress_none_returning.snap @@ -6,18 +6,18 @@ expression: checks MissingReturnTypePublicFunction: foo location: row: 45 - column: 0 + column: 4 end_location: - row: 46 - column: 15 + row: 45 + column: 7 fix: ~ - kind: MissingReturnTypePublicFunction: foo location: row: 50 - column: 0 + column: 4 end_location: - row: 55 - column: 14 + row: 50 + column: 7 fix: ~ diff --git a/src/flake8_bugbear/plugins/f_string_docstring.rs b/src/flake8_bugbear/plugins/f_string_docstring.rs index f4240124f6..66b61dc788 100644 --- a/src/flake8_bugbear/plugins/f_string_docstring.rs +++ b/src/flake8_bugbear/plugins/f_string_docstring.rs @@ -1,6 +1,6 @@ use rustpython_ast::{ExprKind, Stmt, StmtKind}; -use crate::ast::types::Range; +use crate::ast::helpers; use crate::checkers::ast::Checker; use crate::checks::{Check, CheckKind}; @@ -17,6 +17,6 @@ pub fn f_string_docstring(checker: &mut Checker, body: &[Stmt]) { }; checker.add_check(Check::new( CheckKind::FStringDocstring, - Range::from_located(stmt), + helpers::identifier_range(stmt, checker.locator), )); } diff --git a/src/pycodestyle/checks.rs b/src/pycodestyle/checks.rs index 24922272ad..1b996a4130 100644 --- a/src/pycodestyle/checks.rs +++ b/src/pycodestyle/checks.rs @@ -118,11 +118,14 @@ pub fn ambiguous_variable_name(name: &str, located: &Located) -> Option Option { +pub fn ambiguous_class_name(name: &str, locate: F) -> Option +where + F: FnOnce() -> Range, +{ if is_ambiguous_name(name) { Some(Check::new( CheckKind::AmbiguousClassName(name.to_string()), - location, + locate(), )) } else { None @@ -130,11 +133,14 @@ pub fn ambiguous_class_name(name: &str, location: Range) -> Option { } /// E743 -pub fn ambiguous_function_name(name: &str, location: Range) -> Option { +pub fn ambiguous_function_name(name: &str, locate: F) -> Option +where + F: FnOnce() -> Range, +{ if is_ambiguous_name(name) { Some(Check::new( CheckKind::AmbiguousFunctionName(name.to_string()), - location, + locate(), )) } else { None diff --git a/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E742_E742.py.snap b/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E742_E742.py.snap index 9ef41403e5..28de6d848e 100644 --- a/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E742_E742.py.snap +++ b/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E742_E742.py.snap @@ -6,27 +6,27 @@ expression: checks AmbiguousClassName: l location: row: 1 - column: 0 + column: 6 end_location: - row: 2 - column: 8 + row: 1 + column: 7 fix: ~ - kind: AmbiguousClassName: I location: row: 5 - column: 0 + column: 6 end_location: - row: 6 - column: 8 + row: 5 + column: 7 fix: ~ - kind: AmbiguousClassName: O location: row: 9 - column: 0 + column: 6 end_location: - row: 10 - column: 8 + row: 9 + column: 7 fix: ~ diff --git a/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E743_E743.py.snap b/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E743_E743.py.snap index 0c77f1030d..a2f9b02c60 100644 --- a/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E743_E743.py.snap +++ b/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E743_E743.py.snap @@ -6,27 +6,27 @@ expression: checks AmbiguousFunctionName: l location: row: 1 - column: 0 + column: 4 end_location: - row: 2 - column: 8 + row: 1 + column: 5 fix: ~ - kind: AmbiguousFunctionName: I location: row: 5 - column: 0 + column: 4 end_location: - row: 6 - column: 8 + row: 5 + column: 5 fix: ~ - kind: AmbiguousFunctionName: O location: row: 10 - column: 4 + column: 8 end_location: - row: 11 - column: 12 + row: 10 + column: 9 fix: ~ diff --git a/src/pydocstyle/plugins.rs b/src/pydocstyle/plugins.rs index dd62ea5dfc..3fe58ca45b 100644 --- a/src/pydocstyle/plugins.rs +++ b/src/pydocstyle/plugins.rs @@ -4,6 +4,7 @@ use regex::Regex; use rustc_hash::FxHashSet; use rustpython_ast::{Location, StmtKind}; +use crate::ast::helpers::identifier_range; use crate::ast::types::Range; use crate::ast::whitespace::LinesWithTrailingNewline; use crate::ast::{cast, whitespace}; @@ -57,7 +58,7 @@ pub fn not_missing( if checker.settings.enabled.contains(&CheckCode::D101) { checker.add_check(Check::new( CheckKind::PublicClass, - Range::from_located(stmt), + identifier_range(stmt, checker.locator), )); } false @@ -66,7 +67,7 @@ pub fn not_missing( if checker.settings.enabled.contains(&CheckCode::D106) { checker.add_check(Check::new( CheckKind::PublicNestedClass, - Range::from_located(stmt), + identifier_range(stmt, checker.locator), )); } false @@ -78,7 +79,7 @@ pub fn not_missing( if checker.settings.enabled.contains(&CheckCode::D103) { checker.add_check(Check::new( CheckKind::PublicFunction, - Range::from_located(stmt), + identifier_range(stmt, checker.locator), )); } false @@ -93,20 +94,23 @@ pub fn not_missing( if checker.settings.enabled.contains(&CheckCode::D105) { checker.add_check(Check::new( CheckKind::MagicMethod, - Range::from_located(stmt), + identifier_range(stmt, checker.locator), )); } true } else if is_init(stmt) { if checker.settings.enabled.contains(&CheckCode::D107) { - checker.add_check(Check::new(CheckKind::PublicInit, Range::from_located(stmt))); + checker.add_check(Check::new( + CheckKind::PublicInit, + identifier_range(stmt, checker.locator), + )); } true } else { if checker.settings.enabled.contains(&CheckCode::D102) { checker.add_check(Check::new( CheckKind::PublicMethod, - Range::from_located(stmt), + identifier_range(stmt, checker.locator), )); } true @@ -835,7 +839,7 @@ pub fn if_needed(checker: &mut Checker, docstring: &Docstring) { } checker.add_check(Check::new( CheckKind::SkipDocstring, - Range::from_located(stmt), + identifier_range(stmt, checker.locator), )); } diff --git a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D101_D.py.snap b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D101_D.py.snap index 870dd4a6ea..fe05e9db9e 100644 --- a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D101_D.py.snap +++ b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D101_D.py.snap @@ -5,9 +5,9 @@ expression: checks - kind: PublicClass location: row: 15 - column: 0 + column: 6 end_location: - row: 69 + row: 15 column: 12 fix: ~ diff --git a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D102_D.py.snap b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D102_D.py.snap index 22927fb537..0444420d33 100644 --- a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D102_D.py.snap +++ b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D102_D.py.snap @@ -5,25 +5,25 @@ expression: checks - kind: PublicMethod location: row: 23 - column: 4 + column: 8 end_location: - row: 24 - column: 12 + row: 23 + column: 14 fix: ~ - kind: PublicMethod location: row: 56 - column: 4 + column: 8 end_location: - row: 57 - column: 12 + row: 56 + column: 15 fix: ~ - kind: PublicMethod location: row: 68 - column: 4 + column: 8 end_location: - row: 69 - column: 12 + row: 68 + column: 16 fix: ~ diff --git a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D103_D.py.snap b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D103_D.py.snap index f3108f1153..f591e371fb 100644 --- a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D103_D.py.snap +++ b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D103_D.py.snap @@ -5,9 +5,9 @@ expression: checks - kind: PublicFunction location: row: 400 - column: 0 + column: 4 end_location: row: 400 - column: 27 + column: 17 fix: ~ diff --git a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D105_D.py.snap b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D105_D.py.snap index 07695f3a06..522a4dff18 100644 --- a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D105_D.py.snap +++ b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D105_D.py.snap @@ -5,9 +5,9 @@ expression: checks - kind: MagicMethod location: row: 64 - column: 4 + column: 8 end_location: - row: 65 - column: 12 + row: 64 + column: 15 fix: ~ diff --git a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D107_D.py.snap b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D107_D.py.snap index e5f0df82d5..ab2668a87c 100644 --- a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D107_D.py.snap +++ b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D107_D.py.snap @@ -5,17 +5,17 @@ expression: checks - kind: PublicInit location: row: 60 - column: 4 + column: 8 end_location: - row: 61 - column: 12 + row: 60 + column: 16 fix: ~ - kind: PublicInit location: row: 534 - column: 4 + column: 8 end_location: - row: 535 - column: 12 + row: 534 + column: 16 fix: ~ diff --git a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D418_D.py.snap b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D418_D.py.snap index 2733f27e8f..ced6f3538c 100644 --- a/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D418_D.py.snap +++ b/src/pydocstyle/snapshots/ruff__pydocstyle__tests__D418_D.py.snap @@ -5,25 +5,25 @@ expression: checks - kind: SkipDocstring location: row: 34 - column: 4 + column: 8 end_location: - row: 36 - column: 11 + row: 34 + column: 25 fix: ~ - kind: SkipDocstring location: row: 90 - column: 4 + column: 8 end_location: - row: 92 - column: 11 + row: 90 + column: 30 fix: ~ - kind: SkipDocstring location: row: 110 - column: 0 + column: 4 end_location: - row: 112 - column: 7 + row: 110 + column: 19 fix: ~