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

Include subscripts and attributes in static key rule #9416

Merged
merged 1 commit into from
Jan 6, 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
5 changes: 5 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF011.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
{v: v * v for v in range(10)}
{(0, "a", v): v * v for v in range(10)} # Tuple with variable
{constant: value.upper() for value in data for constant in data}
{value.attribute: value.upper() for value in data for constant in data}
{constant[value]: value.upper() for value in data for constant in data}
{value[constant]: value.upper() for value in data for constant in data}

# Errors
{"key": value.upper() for value in data}
Expand All @@ -15,3 +18,5 @@
{(1, "a"): value.upper() for value in data} # Constant tuple
{constant: value.upper() for value in data}
{constant + constant: value.upper() for value in data}
{constant.attribute: value.upper() for value in data}
{constant[0]: value.upper() for value in data}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ fn is_constant(key: &Expr, names: &FxHashMap<&str, &ast::ExprName>) -> bool {
match key {
Expr::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().all(|elt| is_constant(elt, names)),
Expr::Name(ast::ExprName { id, .. }) => !names.contains_key(id.as_str()),
Expr::Attribute(ast::ExprAttribute { value, .. }) => is_constant(value, names),
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
is_constant(value, names) && is_constant(slice, names)
}
Expr::BinOp(ast::ExprBinOp { left, right, .. }) => {
is_constant(left, names) && is_constant(right, names)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,80 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
---
RUF011.py:12:2: RUF011 Dictionary comprehension uses static key: `"key"`
RUF011.py:15:2: RUF011 Dictionary comprehension uses static key: `"key"`
|
11 | # Errors
12 | {"key": value.upper() for value in data}
14 | # Errors
15 | {"key": value.upper() for value in data}
| ^^^^^ RUF011
13 | {True: value.upper() for value in data}
14 | {0: value.upper() for value in data}
16 | {True: value.upper() for value in data}
17 | {0: value.upper() for value in data}
|

RUF011.py:13:2: RUF011 Dictionary comprehension uses static key: `True`
RUF011.py:16:2: RUF011 Dictionary comprehension uses static key: `True`
|
11 | # Errors
12 | {"key": value.upper() for value in data}
13 | {True: value.upper() for value in data}
14 | # Errors
15 | {"key": value.upper() for value in data}
16 | {True: value.upper() for value in data}
| ^^^^ RUF011
14 | {0: value.upper() for value in data}
15 | {(1, "a"): value.upper() for value in data} # Constant tuple
17 | {0: value.upper() for value in data}
18 | {(1, "a"): value.upper() for value in data} # Constant tuple
|

RUF011.py:14:2: RUF011 Dictionary comprehension uses static key: `0`
RUF011.py:17:2: RUF011 Dictionary comprehension uses static key: `0`
|
12 | {"key": value.upper() for value in data}
13 | {True: value.upper() for value in data}
14 | {0: value.upper() for value in data}
15 | {"key": value.upper() for value in data}
16 | {True: value.upper() for value in data}
17 | {0: value.upper() for value in data}
| ^ RUF011
15 | {(1, "a"): value.upper() for value in data} # Constant tuple
16 | {constant: value.upper() for value in data}
18 | {(1, "a"): value.upper() for value in data} # Constant tuple
19 | {constant: value.upper() for value in data}
|

RUF011.py:15:2: RUF011 Dictionary comprehension uses static key: `(1, "a")`
RUF011.py:18:2: RUF011 Dictionary comprehension uses static key: `(1, "a")`
|
13 | {True: value.upper() for value in data}
14 | {0: value.upper() for value in data}
15 | {(1, "a"): value.upper() for value in data} # Constant tuple
16 | {True: value.upper() for value in data}
17 | {0: value.upper() for value in data}
18 | {(1, "a"): value.upper() for value in data} # Constant tuple
| ^^^^^^^^ RUF011
16 | {constant: value.upper() for value in data}
17 | {constant + constant: value.upper() for value in data}
19 | {constant: value.upper() for value in data}
20 | {constant + constant: value.upper() for value in data}
|

RUF011.py:16:2: RUF011 Dictionary comprehension uses static key: `constant`
RUF011.py:19:2: RUF011 Dictionary comprehension uses static key: `constant`
|
14 | {0: value.upper() for value in data}
15 | {(1, "a"): value.upper() for value in data} # Constant tuple
16 | {constant: value.upper() for value in data}
17 | {0: value.upper() for value in data}
18 | {(1, "a"): value.upper() for value in data} # Constant tuple
19 | {constant: value.upper() for value in data}
| ^^^^^^^^ RUF011
17 | {constant + constant: value.upper() for value in data}
20 | {constant + constant: value.upper() for value in data}
21 | {constant.attribute: value.upper() for value in data}
|

RUF011.py:17:2: RUF011 Dictionary comprehension uses static key: `constant + constant`
RUF011.py:20:2: RUF011 Dictionary comprehension uses static key: `constant + constant`
|
15 | {(1, "a"): value.upper() for value in data} # Constant tuple
16 | {constant: value.upper() for value in data}
17 | {constant + constant: value.upper() for value in data}
18 | {(1, "a"): value.upper() for value in data} # Constant tuple
19 | {constant: value.upper() for value in data}
20 | {constant + constant: value.upper() for value in data}
| ^^^^^^^^^^^^^^^^^^^ RUF011
21 | {constant.attribute: value.upper() for value in data}
22 | {constant[0]: value.upper() for value in data}
|

RUF011.py:21:2: RUF011 Dictionary comprehension uses static key: `constant.attribute`
|
19 | {constant: value.upper() for value in data}
20 | {constant + constant: value.upper() for value in data}
21 | {constant.attribute: value.upper() for value in data}
| ^^^^^^^^^^^^^^^^^^ RUF011
22 | {constant[0]: value.upper() for value in data}
|

RUF011.py:22:2: RUF011 Dictionary comprehension uses static key: `constant[0]`
|
20 | {constant + constant: value.upper() for value in data}
21 | {constant.attribute: value.upper() for value in data}
22 | {constant[0]: value.upper() for value in data}
| ^^^^^^^^^^^ RUF011
|


Loading