Skip to content

Commit 4bcc96a

Browse files
Avoid shadowing diagnostics for @override methods (#12415)
Closes #12412.
1 parent c0a2b49 commit 4bcc96a

File tree

4 files changed

+48
-28
lines changed

4 files changed

+48
-28
lines changed

Diff for: crates/ruff_linter/resources/test/fixtures/flake8_builtins/A002.py

+14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@ def func1(str, /, type, *complex, Exception, **getattr):
55
async def func2(bytes):
66
pass
77

8+
89
async def func3(id, dir):
910
pass
1011

12+
1113
map([], lambda float: ...)
14+
15+
from typing import override, overload
16+
17+
18+
@override
19+
def func4(id, dir):
20+
pass
21+
22+
23+
@overload
24+
def func4(id, dir):
25+
pass

Diff for: crates/ruff_linter/src/rules/flake8_builtins/rules/builtin_argument_shadowing.rs

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use ruff_python_ast::Parameter;
33
use ruff_diagnostics::Diagnostic;
44
use ruff_diagnostics::Violation;
55
use ruff_macros::{derive_message_formats, violation};
6+
use ruff_python_semantic::analyze::visibility::{is_overload, is_override};
67
use ruff_text_size::Ranged;
78

89
use crate::checkers::ast::Checker;
@@ -69,6 +70,19 @@ pub(crate) fn builtin_argument_shadowing(checker: &mut Checker, parameter: &Para
6970
&checker.settings.flake8_builtins.builtins_ignorelist,
7071
checker.source_type,
7172
) {
73+
// Ignore `@override` and `@overload` decorated functions.
74+
if checker
75+
.semantic()
76+
.current_statement()
77+
.as_function_def_stmt()
78+
.is_some_and(|function_def| {
79+
is_override(&function_def.decorator_list, checker.semantic())
80+
|| is_overload(&function_def.decorator_list, checker.semantic())
81+
})
82+
{
83+
return;
84+
}
85+
7286
checker.diagnostics.push(Diagnostic::new(
7387
BuiltinArgumentShadowing {
7488
name: parameter.name.to_string(),

Diff for: crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A002_A002.py.snap

+16-22
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,24 @@ A002.py:5:17: A002 Argument `bytes` is shadowing a Python builtin
4343
6 | pass
4444
|
4545

46-
A002.py:8:17: A002 Argument `id` is shadowing a Python builtin
47-
|
48-
6 | pass
49-
7 |
50-
8 | async def func3(id, dir):
51-
| ^^ A002
52-
9 | pass
53-
|
46+
A002.py:9:17: A002 Argument `id` is shadowing a Python builtin
47+
|
48+
9 | async def func3(id, dir):
49+
| ^^ A002
50+
10 | pass
51+
|
5452

55-
A002.py:8:21: A002 Argument `dir` is shadowing a Python builtin
56-
|
57-
6 | pass
58-
7 |
59-
8 | async def func3(id, dir):
60-
| ^^^ A002
61-
9 | pass
62-
|
53+
A002.py:9:21: A002 Argument `dir` is shadowing a Python builtin
54+
|
55+
9 | async def func3(id, dir):
56+
| ^^^ A002
57+
10 | pass
58+
|
6359

64-
A002.py:11:16: A002 Argument `float` is shadowing a Python builtin
60+
A002.py:13:16: A002 Argument `float` is shadowing a Python builtin
6561
|
66-
9 | pass
67-
10 |
68-
11 | map([], lambda float: ...)
62+
13 | map([], lambda float: ...)
6963
| ^^^^^ A002
64+
14 |
65+
15 | from typing import override, overload
7066
|
71-
72-

Diff for: crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A002_A002.py_builtins_ignorelist.snap

+4-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,10 @@ A002.py:5:17: A002 Argument `bytes` is shadowing a Python builtin
4343
6 | pass
4444
|
4545

46-
A002.py:11:16: A002 Argument `float` is shadowing a Python builtin
46+
A002.py:13:16: A002 Argument `float` is shadowing a Python builtin
4747
|
48-
9 | pass
49-
10 |
50-
11 | map([], lambda float: ...)
48+
13 | map([], lambda float: ...)
5149
| ^^^^^ A002
50+
14 |
51+
15 | from typing import override, overload
5252
|
53-
54-

0 commit comments

Comments
 (0)