Skip to content

Commit

Permalink
Avoid shadowing diagnostics for @override methods (#12415)
Browse files Browse the repository at this point in the history
Closes #12412.
  • Loading branch information
charliermarsh authored Jul 20, 2024
1 parent c0a2b49 commit 4bcc96a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
14 changes: 14 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_builtins/A002.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@ def func1(str, /, type, *complex, Exception, **getattr):
async def func2(bytes):
pass


async def func3(id, dir):
pass


map([], lambda float: ...)

from typing import override, overload


@override
def func4(id, dir):
pass


@overload
def func4(id, dir):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ruff_python_ast::Parameter;
use ruff_diagnostics::Diagnostic;
use ruff_diagnostics::Violation;
use ruff_macros::{derive_message_formats, violation};
use ruff_python_semantic::analyze::visibility::{is_overload, is_override};
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -69,6 +70,19 @@ pub(crate) fn builtin_argument_shadowing(checker: &mut Checker, parameter: &Para
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.source_type,
) {
// Ignore `@override` and `@overload` decorated functions.
if checker
.semantic()
.current_statement()
.as_function_def_stmt()
.is_some_and(|function_def| {
is_override(&function_def.decorator_list, checker.semantic())
|| is_overload(&function_def.decorator_list, checker.semantic())
})
{
return;
}

checker.diagnostics.push(Diagnostic::new(
BuiltinArgumentShadowing {
name: parameter.name.to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,24 @@ A002.py:5:17: A002 Argument `bytes` is shadowing a Python builtin
6 | pass
|

A002.py:8:17: A002 Argument `id` is shadowing a Python builtin
|
6 | pass
7 |
8 | async def func3(id, dir):
| ^^ A002
9 | pass
|
A002.py:9:17: A002 Argument `id` is shadowing a Python builtin
|
9 | async def func3(id, dir):
| ^^ A002
10 | pass
|

A002.py:8:21: A002 Argument `dir` is shadowing a Python builtin
|
6 | pass
7 |
8 | async def func3(id, dir):
| ^^^ A002
9 | pass
|
A002.py:9:21: A002 Argument `dir` is shadowing a Python builtin
|
9 | async def func3(id, dir):
| ^^^ A002
10 | pass
|

A002.py:11:16: A002 Argument `float` is shadowing a Python builtin
A002.py:13:16: A002 Argument `float` is shadowing a Python builtin
|
9 | pass
10 |
11 | map([], lambda float: ...)
13 | map([], lambda float: ...)
| ^^^^^ A002
14 |
15 | from typing import override, overload
|


Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ A002.py:5:17: A002 Argument `bytes` is shadowing a Python builtin
6 | pass
|

A002.py:11:16: A002 Argument `float` is shadowing a Python builtin
A002.py:13:16: A002 Argument `float` is shadowing a Python builtin
|
9 | pass
10 |
11 | map([], lambda float: ...)
13 | map([], lambda float: ...)
| ^^^^^ A002
14 |
15 | from typing import override, overload
|


0 comments on commit 4bcc96a

Please sign in to comment.