Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid shadowing diagnostics for @override methods #12415

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
|


Loading