mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-31 15:48:22 +00:00
Use more precise ranges for class and function checks (#1476)
This commit is contained in:
parent
87681697ae
commit
3061a35e7c
17 changed files with 117 additions and 108 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
|
@ -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),
|
||||
));
|
||||
}
|
||||
|
|
|
@ -118,11 +118,14 @@ pub fn ambiguous_variable_name<T>(name: &str, located: &Located<T>) -> Option<Ch
|
|||
}
|
||||
|
||||
/// E742
|
||||
pub fn ambiguous_class_name(name: &str, location: Range) -> Option<Check> {
|
||||
pub fn ambiguous_class_name<F>(name: &str, locate: F) -> Option<Check>
|
||||
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<Check> {
|
|||
}
|
||||
|
||||
/// E743
|
||||
pub fn ambiguous_function_name(name: &str, location: Range) -> Option<Check> {
|
||||
pub fn ambiguous_function_name<F>(name: &str, locate: F) -> Option<Check>
|
||||
where
|
||||
F: FnOnce() -> Range,
|
||||
{
|
||||
if is_ambiguous_name(name) {
|
||||
Some(Check::new(
|
||||
CheckKind::AmbiguousFunctionName(name.to_string()),
|
||||
location,
|
||||
locate(),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
|
@ -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),
|
||||
));
|
||||
}
|
||||
|
||||
|
|
|
@ -5,9 +5,9 @@ expression: checks
|
|||
- kind: PublicClass
|
||||
location:
|
||||
row: 15
|
||||
column: 0
|
||||
column: 6
|
||||
end_location:
|
||||
row: 69
|
||||
row: 15
|
||||
column: 12
|
||||
fix: ~
|
||||
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
|
@ -5,9 +5,9 @@ expression: checks
|
|||
- kind: PublicFunction
|
||||
location:
|
||||
row: 400
|
||||
column: 0
|
||||
column: 4
|
||||
end_location:
|
||||
row: 400
|
||||
column: 27
|
||||
column: 17
|
||||
fix: ~
|
||||
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
|
@ -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: ~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue