Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -55,8 +55,3 @@ def foo(some_other):
def foo():
dict = {"Tom": 23, "Maria": 23, "Dog": 11}
age = dict.get("Cat", None)

# https://github.com/astral-sh/ruff/issues/20341
ages = {"Tom": 23, "Maria": 23, "Dog": 11}
key_source = {"Thomas": "Tom"}
age = ages.get(key_source.get("Thomas", "Tom"), None)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Method call as key
ages = {"Tom": 23, "Maria": 23, "Dog": 11}
key_source = {"Thomas": "Tom"}
age = ages.get(key_source.get("Thomas", "Tom"), None)

# Property access as key
class Data:
def __init__(self):
self.name = "Tom"

data = Data()
ages = {"Tom": 23, "Maria": 23, "Dog": 11}
age = ages.get(data.name, None)

# Complex expression as key
ages = {"Tom": 23, "Maria": 23, "Dog": 11}
key = "Tom" if True else "Maria"
age = ages.get(key, None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually just another Expr::Name as key since it's assigned to a variable. Even the if expression itself is only a single Expr::If, but this is fine either way!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I inlined this too while I was at it)


# Function call as key
def get_key():
return "Tom"

ages = {"Tom": 23, "Maria": 23, "Dog": 11}
age = ages.get(get_key(), None)

# OK - these should not trigger even in preview mode
ages = {"Tom": 23, "Maria": 23, "Dog": 11}
age = ages.get("Tom") # No default value

ages = {"Tom": 23, "Maria": 23, "Dog": 11}
age = ages.get("Tom", "Unknown") # Non-None default
5 changes: 5 additions & 0 deletions crates/ruff_linter/src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,8 @@ pub(crate) const fn is_unnecessary_default_type_args_stubs_enabled(
) -> bool {
settings.preview.is_enabled()
}

// https://github.com/astral-sh/ruff/pull/20343
pub(crate) const fn is_sim910_expanded_key_support_enabled(settings: &LinterSettings) -> bool {
settings.preview.is_enabled()
}
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/flake8_simplify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mod tests {
}

#[test_case(Rule::SplitStaticString, Path::new("SIM905.py"))]
#[test_case(Rule::DictGetWithNoneDefault, Path::new("SIM910_preview.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ pub(crate) fn dict_get_with_none_default(checker: &Checker, expr: &Expr) {
let Some(key) = args.first() else {
return;
};
if !crate::preview::is_sim910_expanded_key_support_enabled(checker.settings()) {
if !(key.is_literal_expr() || key.is_name_expr()) {
return;
}
}
let Some(default) = args.get(1) else {
return;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,30 +160,10 @@ SIM910 [*] Use `dict.get("Cat")` instead of `dict.get("Cat", None)`
56 | dict = {"Tom": 23, "Maria": 23, "Dog": 11}
57 | age = dict.get("Cat", None)
| ^^^^^^^^^^^^^^^^^^^^^
58 |
59 | # https://github.com/astral-sh/ruff/issues/20341
|
help: Replace `dict.get("Cat", None)` with `dict.get("Cat")`
54 | # https://github.com/astral-sh/ruff/issues/18777
55 | def foo():
56 | dict = {"Tom": 23, "Maria": 23, "Dog": 11}
- age = dict.get("Cat", None)
57 + age = dict.get("Cat")
58 |
59 | # https://github.com/astral-sh/ruff/issues/20341
60 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}

SIM910 [*] Use `ages.get(key_source.get("Thomas", "Tom"))` instead of `ages.get(key_source.get("Thomas", "Tom"), None)`
--> SIM910.py:62:7
|
60 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
61 | key_source = {"Thomas": "Tom"}
62 | age = ages.get(key_source.get("Thomas", "Tom"), None)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Replace `ages.get(key_source.get("Thomas", "Tom"), None)` with `ages.get(key_source.get("Thomas", "Tom"))`
59 | # https://github.com/astral-sh/ruff/issues/20341
60 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
61 | key_source = {"Thomas": "Tom"}
- age = ages.get(key_source.get("Thomas", "Tom"), None)
62 + age = ages.get(key_source.get("Thomas", "Tom"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
---
SIM910 [*] Use `ages.get(key_source.get("Thomas", "Tom"))` instead of `ages.get(key_source.get("Thomas", "Tom"), None)`
--> SIM910_preview.py:4:7
|
2 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
3 | key_source = {"Thomas": "Tom"}
4 | age = ages.get(key_source.get("Thomas", "Tom"), None)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 |
6 | # Property access as key
|
help: Replace `ages.get(key_source.get("Thomas", "Tom"), None)` with `ages.get(key_source.get("Thomas", "Tom"))`
1 | # Method call as key
2 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
3 | key_source = {"Thomas": "Tom"}
- age = ages.get(key_source.get("Thomas", "Tom"), None)
4 + age = ages.get(key_source.get("Thomas", "Tom"))
5 |
6 | # Property access as key
7 | class Data:

SIM910 [*] Use `ages.get(data.name)` instead of `ages.get(data.name, None)`
--> SIM910_preview.py:13:7
|
11 | data = Data()
12 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
13 | age = ages.get(data.name, None)
| ^^^^^^^^^^^^^^^^^^^^^^^^^
14 |
15 | # Complex expression as key
|
help: Replace `ages.get(data.name, None)` with `ages.get(data.name)`
10 |
11 | data = Data()
12 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
- age = ages.get(data.name, None)
13 + age = ages.get(data.name)
14 |
15 | # Complex expression as key
16 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}

SIM910 [*] Use `ages.get(key)` instead of `ages.get(key, None)`
--> SIM910_preview.py:18:7
|
16 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
17 | key = "Tom" if True else "Maria"
18 | age = ages.get(key, None)
| ^^^^^^^^^^^^^^^^^^^
19 |
20 | # Function call as key
|
help: Replace `ages.get(key, None)` with `ages.get(key)`
15 | # Complex expression as key
16 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
17 | key = "Tom" if True else "Maria"
- age = ages.get(key, None)
18 + age = ages.get(key)
19 |
20 | # Function call as key
21 | def get_key():

SIM910 [*] Use `ages.get(get_key())` instead of `ages.get(get_key(), None)`
--> SIM910_preview.py:25:7
|
24 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
25 | age = ages.get(get_key(), None)
| ^^^^^^^^^^^^^^^^^^^^^^^^^
26 |
27 | # OK - these should not trigger even in preview mode
|
help: Replace `ages.get(get_key(), None)` with `ages.get(get_key())`
22 | return "Tom"
23 |
24 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}
- age = ages.get(get_key(), None)
25 + age = ages.get(get_key())
26 |
27 | # OK - these should not trigger even in preview mode
28 | ages = {"Tom": 23, "Maria": 23, "Dog": 11}