mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:21 +00:00
Avoid N802 violations for @override
methods (#3912)
This commit is contained in:
parent
8b17508ef1
commit
29ec6df24f
3 changed files with 35 additions and 10 deletions
|
@ -39,3 +39,11 @@ class Test(unittest.TestCase):
|
|||
|
||||
def testTest(self):
|
||||
assert True
|
||||
|
||||
|
||||
from typing import override
|
||||
|
||||
|
||||
@override
|
||||
def BAD_FUNC():
|
||||
pass
|
||||
|
|
|
@ -344,6 +344,7 @@ where
|
|||
|expr| self.ctx.resolve_call_path(expr),
|
||||
));
|
||||
}
|
||||
|
||||
if self.settings.rules.enabled(Rule::AmbiguousFunctionName) {
|
||||
if let Some(diagnostic) =
|
||||
pycodestyle::rules::ambiguous_function_name(name, || {
|
||||
|
@ -358,7 +359,9 @@ where
|
|||
if let Some(diagnostic) = pep8_naming::rules::invalid_function_name(
|
||||
stmt,
|
||||
name,
|
||||
decorator_list,
|
||||
&self.settings.pep8_naming.ignore_names,
|
||||
&self.ctx,
|
||||
self.locator,
|
||||
) {
|
||||
self.diagnostics.push(diagnostic);
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
use rustpython_parser::ast::Stmt;
|
||||
use rustpython_parser::ast::{Expr, Stmt};
|
||||
|
||||
use ruff_diagnostics::{Diagnostic, Violation};
|
||||
use ruff_macros::{derive_message_formats, violation};
|
||||
use ruff_python_ast::helpers::identifier_range;
|
||||
use ruff_python_ast::source_code::Locator;
|
||||
use ruff_python_semantic::analyze::visibility;
|
||||
use ruff_python_semantic::context::Context;
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for functions names that do not follow the `snake_case` naming
|
||||
|
@ -47,21 +49,33 @@ impl Violation for InvalidFunctionName {
|
|||
|
||||
/// N802
|
||||
pub fn invalid_function_name(
|
||||
func_def: &Stmt,
|
||||
stmt: &Stmt,
|
||||
name: &str,
|
||||
decorator_list: &[Expr],
|
||||
ignore_names: &[String],
|
||||
ctx: &Context,
|
||||
locator: &Locator,
|
||||
) -> Option<Diagnostic> {
|
||||
// Ignore any explicitly-ignored function names.
|
||||
if ignore_names.iter().any(|ignore_name| ignore_name == name) {
|
||||
return None;
|
||||
}
|
||||
if name.to_lowercase() != name {
|
||||
return Some(Diagnostic::new(
|
||||
InvalidFunctionName {
|
||||
name: name.to_string(),
|
||||
},
|
||||
identifier_range(func_def, locator),
|
||||
));
|
||||
|
||||
// Ignore any function names that are already lowercase.
|
||||
if name.to_lowercase() == name {
|
||||
return None;
|
||||
}
|
||||
None
|
||||
|
||||
// Ignore any functions that are explicitly `@override`. These are defined elsewhere,
|
||||
// so if they're first-party, we'll flag them at the definition site.
|
||||
if visibility::is_override(ctx, decorator_list) {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Diagnostic::new(
|
||||
InvalidFunctionName {
|
||||
name: name.to_string(),
|
||||
},
|
||||
identifier_range(stmt, locator),
|
||||
))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue