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

[flake8-builtins] Skip lambda expressions in builtin-argument-shadowing (A002) #14144

Merged
merged 5 commits into from
Nov 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ async def func3(id, dir):
pass


# this is Ok for A002 (trigger A005 instead)
# https://github.com/astral-sh/ruff/issues/14135
map([], lambda float: ...)

from typing import override, overload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
lambda min, max: min
lambda id: id
lambda dir: dir

# Ok for A006 - should trigger A002 instead
# https://github.com/astral-sh/ruff/issues/14135
def func1(str, /, type, *complex, Exception, **getattr):
pass
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ruff_diagnostics::Diagnostic;
use ruff_diagnostics::Violation;
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::Parameter;
use ruff_python_ast::{Expr, Parameter};
use ruff_python_semantic::analyze::visibility::{is_overload, is_override};
use ruff_text_size::Ranged;

Expand Down Expand Up @@ -58,7 +58,7 @@ impl Violation for BuiltinArgumentShadowing {
#[derive_message_formats]
fn message(&self) -> String {
let BuiltinArgumentShadowing { name } = self;
format!("Argument `{name}` is shadowing a Python builtin")
format!("Function argument `{name}` is shadowing a Python builtin")
}
}

Expand All @@ -70,6 +70,15 @@ pub(crate) fn builtin_argument_shadowing(checker: &mut Checker, parameter: &Para
&checker.settings.flake8_builtins.builtins_ignorelist,
checker.settings.target_version,
) {
// Ignore parameters in lambda expressions.
// (That is the domain of A006.)
if checker
.semantic()
.current_expression()
.is_some_and(Expr::is_lambda_expr)
{
return;
}
// Ignore `@override` and `@overload` decorated functions.
if checker
.semantic()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,58 @@
---
source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs
---
A002.py:1:11: A002 Argument `str` is shadowing a Python builtin
A002.py:1:11: A002 Function argument `str` is shadowing a Python builtin
|
1 | def func1(str, /, type, *complex, Exception, **getattr):
| ^^^ A002
2 | pass
|

A002.py:1:19: A002 Argument `type` is shadowing a Python builtin
A002.py:1:19: A002 Function argument `type` is shadowing a Python builtin
|
1 | def func1(str, /, type, *complex, Exception, **getattr):
| ^^^^ A002
2 | pass
|

A002.py:1:26: A002 Argument `complex` is shadowing a Python builtin
A002.py:1:26: A002 Function argument `complex` is shadowing a Python builtin
|
1 | def func1(str, /, type, *complex, Exception, **getattr):
| ^^^^^^^ A002
2 | pass
|

A002.py:1:35: A002 Argument `Exception` is shadowing a Python builtin
A002.py:1:35: A002 Function argument `Exception` is shadowing a Python builtin
|
1 | def func1(str, /, type, *complex, Exception, **getattr):
| ^^^^^^^^^ A002
2 | pass
|

A002.py:1:48: A002 Argument `getattr` is shadowing a Python builtin
A002.py:1:48: A002 Function argument `getattr` is shadowing a Python builtin
|
1 | def func1(str, /, type, *complex, Exception, **getattr):
| ^^^^^^^ A002
2 | pass
|

A002.py:5:17: A002 Argument `bytes` is shadowing a Python builtin
A002.py:5:17: A002 Function argument `bytes` is shadowing a Python builtin
|
5 | async def func2(bytes):
| ^^^^^ A002
6 | pass
|

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

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

A002.py:13:16: A002 Argument `float` is shadowing a Python builtin
|
13 | map([], lambda float: ...)
| ^^^^^ A002
14 |
15 | from typing import override, overload
|
Original file line number Diff line number Diff line change
@@ -1,52 +1,44 @@
---
source: crates/ruff_linter/src/rules/flake8_builtins/mod.rs
---
A002.py:1:11: A002 Argument `str` is shadowing a Python builtin
A002.py:1:11: A002 Function argument `str` is shadowing a Python builtin
|
1 | def func1(str, /, type, *complex, Exception, **getattr):
| ^^^ A002
2 | pass
|

A002.py:1:19: A002 Argument `type` is shadowing a Python builtin
A002.py:1:19: A002 Function argument `type` is shadowing a Python builtin
|
1 | def func1(str, /, type, *complex, Exception, **getattr):
| ^^^^ A002
2 | pass
|

A002.py:1:26: A002 Argument `complex` is shadowing a Python builtin
A002.py:1:26: A002 Function argument `complex` is shadowing a Python builtin
|
1 | def func1(str, /, type, *complex, Exception, **getattr):
| ^^^^^^^ A002
2 | pass
|

A002.py:1:35: A002 Argument `Exception` is shadowing a Python builtin
A002.py:1:35: A002 Function argument `Exception` is shadowing a Python builtin
|
1 | def func1(str, /, type, *complex, Exception, **getattr):
| ^^^^^^^^^ A002
2 | pass
|

A002.py:1:48: A002 Argument `getattr` is shadowing a Python builtin
A002.py:1:48: A002 Function argument `getattr` is shadowing a Python builtin
|
1 | def func1(str, /, type, *complex, Exception, **getattr):
| ^^^^^^^ A002
2 | pass
|

A002.py:5:17: A002 Argument `bytes` is shadowing a Python builtin
A002.py:5:17: A002 Function argument `bytes` is shadowing a Python builtin
|
5 | async def func2(bytes):
| ^^^^^ A002
6 | pass
|

A002.py:13:16: A002 Argument `float` is shadowing a Python builtin
|
13 | map([], lambda float: ...)
| ^^^^^ A002
14 |
15 | from typing import override, overload
|
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ A006.py:5:8: A006 Lambda argument `dir` is shadowing a Python builtin
4 | lambda id: id
5 | lambda dir: dir
| ^^^ A006
6 |
7 | # Ok for A006 - should trigger A002 instead
|
Loading