From 4bcc96ae514127a1a1bece13f55d0409d98bbf68 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 19 Jul 2024 21:32:33 -0400 Subject: [PATCH] Avoid shadowing diagnostics for `@override` methods (#12415) Closes https://github.com/astral-sh/ruff/issues/12412. --- .../test/fixtures/flake8_builtins/A002.py | 14 +++++++ .../rules/builtin_argument_shadowing.rs | 14 +++++++ ..._flake8_builtins__tests__A002_A002.py.snap | 38 ++++++++----------- ...sts__A002_A002.py_builtins_ignorelist.snap | 10 ++--- 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_builtins/A002.py b/crates/ruff_linter/resources/test/fixtures/flake8_builtins/A002.py index 492091bbe40cb..5dc8d11fcf42d 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_builtins/A002.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_builtins/A002.py @@ -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 diff --git a/crates/ruff_linter/src/rules/flake8_builtins/rules/builtin_argument_shadowing.rs b/crates/ruff_linter/src/rules/flake8_builtins/rules/builtin_argument_shadowing.rs index 5c826b083836e..74088538aa51f 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/rules/builtin_argument_shadowing.rs +++ b/crates/ruff_linter/src/rules/flake8_builtins/rules/builtin_argument_shadowing.rs @@ -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; @@ -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(), diff --git a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A002_A002.py.snap b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A002_A002.py.snap index d58e06a78593c..b00ca145fcf5e 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A002_A002.py.snap +++ b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A002_A002.py.snap @@ -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 | - - diff --git a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A002_A002.py_builtins_ignorelist.snap b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A002_A002.py_builtins_ignorelist.snap index ed3377c02346c..d5627625962a8 100644 --- a/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A002_A002.py_builtins_ignorelist.snap +++ b/crates/ruff_linter/src/rules/flake8_builtins/snapshots/ruff_linter__rules__flake8_builtins__tests__A002_A002.py_builtins_ignorelist.snap @@ -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 | - -