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

Add required space when fixing SIM118 #7150

Merged
merged 1 commit into from
Sep 5, 2023
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
6 changes: 6 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_simplify/SIM118.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ def keys(self) -> KeysView[object]:

def __contains__(self, key: object) -> bool:
return key in self.keys() # OK


# Regression test for: https://github.com/astral-sh/ruff/issues/7124
key in obj.keys()and foo
(key in obj.keys())and foo
key in (obj.keys())and foo
18 changes: 17 additions & 1 deletion crates/ruff/src/rules/flake8_simplify/rules/key_in_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,24 @@ fn key_in_dict(
TextRange::new(left_range.start(), right_range.end()),
);
if checker.patch(diagnostic.kind.rule()) {
// If the `.keys()` is followed by (e.g.) a keyword, we need to insert a space,
// since we're removing parentheses, which could lead to invalid syntax, as in:
// ```python
// if key in foo.keys()and bar:
// ```
let requires_space = checker
.locator()
.after(right_range.end())
.chars()
.next()
.is_some_and(|char| char.is_ascii_alphabetic());

diagnostic.set_fix(Fix::suggested(Edit::range_replacement(
value_content.to_string(),
if requires_space {
format!("{value_content} ")
} else {
value_content.to_string()
},
right_range,
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,57 @@ SIM118.py:34:1: SIM118 [*] Use `(key) in (obj or {})` instead of `(key) in (obj
36 36 | from typing import KeysView
37 37 |

SIM118.py:48:1: SIM118 [*] Use `key in obj` instead of `key in obj.keys()`
|
47 | # Regression test for: https://github.com/astral-sh/ruff/issues/7124
48 | key in obj.keys()and foo
| ^^^^^^^^^^^^^^^^^ SIM118
49 | (key in obj.keys())and foo
50 | key in (obj.keys())and foo
|
= help: Convert to `key in obj`

ℹ Suggested fix
45 45 |
46 46 |
47 47 | # Regression test for: https://github.com/astral-sh/ruff/issues/7124
48 |-key in obj.keys()and foo
48 |+key in obj and foo
49 49 | (key in obj.keys())and foo
50 50 | key in (obj.keys())and foo

SIM118.py:49:2: SIM118 [*] Use `key in obj` instead of `key in obj.keys()`
|
47 | # Regression test for: https://github.com/astral-sh/ruff/issues/7124
48 | key in obj.keys()and foo
49 | (key in obj.keys())and foo
| ^^^^^^^^^^^^^^^^^ SIM118
50 | key in (obj.keys())and foo
|
= help: Convert to `key in obj`

ℹ Suggested fix
46 46 |
47 47 | # Regression test for: https://github.com/astral-sh/ruff/issues/7124
48 48 | key in obj.keys()and foo
49 |-(key in obj.keys())and foo
49 |+(key in obj)and foo
50 50 | key in (obj.keys())and foo

SIM118.py:50:1: SIM118 [*] Use `key in obj` instead of `key in obj.keys()`
|
48 | key in obj.keys()and foo
49 | (key in obj.keys())and foo
50 | key in (obj.keys())and foo
| ^^^^^^^^^^^^^^^^^^^ SIM118
|
= help: Convert to `key in obj`

ℹ Suggested fix
47 47 | # Regression test for: https://github.com/astral-sh/ruff/issues/7124
48 48 | key in obj.keys()and foo
49 49 | (key in obj.keys())and foo
50 |-key in (obj.keys())and foo
50 |+key in obj and foo


Loading