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 auto-fixes for C0113, C0121, C0123 #303

Merged
merged 26 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 25 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
65 changes: 48 additions & 17 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,32 +313,63 @@ def organize_imports(


REPLACEMENTS = {
"R0205:useless-object-inheritance": {
"pattern": r"class (\w+)\(object\):",
"repl": r"class \1:",
},
"R1721:unnecessary-comprehension": {
"pattern": r"\{([\w\s,]+) for [\w\s,]+ in ([\w\s,]+)\}",
"repl": r"set(\2)",
},
"E1141:dict-iter-missing-items": {
"pattern": r"for\s+(\w+),\s+(\w+)\s+in\s+(\w+)\s*:",
"repl": r"for \1, \2 in \3.items():",
},
"C0113:unneeded-not": [
{
"pattern": r"\snot\s+not",
"repl": r"",
}
],
"C0121:singleton-comparison": [
{
"pattern": r"(\w+)\s+(?:==\s+True|!=\s+False)|(?:True\s+==|False\s+!=)\s+(\w+)",
"repl": r"\1\2",
},
{
"pattern": r"(\w+)\s+(?:!=\s+True|==\s+False)|(?:True\s+!=|False\s+==)\s+(\w+)",
"repl": r"not \1\2",
},
],
"C0123:unidiomatic-typecheck": [
{
"pattern": r"type\((\w+)\)\s+is\s+(\w+)",
"repl": r"isinstance(\1, \2)",
}
],
"R0205:useless-object-inheritance": [
{
"pattern": r"class (\w+)\(object\):",
"repl": r"class \1:",
}
],
"R1721:unnecessary-comprehension": [
{
"pattern": r"\{([\w\s,]+) for [\w\s,]+ in ([\w\s,]+)\}",
"repl": r"set(\2)",
}
],
"E1141:dict-iter-missing-items": [
{
"pattern": r"for\s+(\w+),\s+(\w+)\s+in\s+(\w+)\s*:",
"repl": r"for \1, \2 in \3.items():",
}
],
}


def _get_replacement_edit(diagnostic: lsp.Diagnostic, lines: List[str]) -> lsp.TextEdit:
new_line = lines[diagnostic.range.start.line]
for replacement in REPLACEMENTS[diagnostic.code]:
new_line = re.sub(
replacement["pattern"],
replacement["repl"],
new_line,
)
return lsp.TextEdit(
lsp.Range(
start=lsp.Position(line=diagnostic.range.start.line, character=0),
end=lsp.Position(line=diagnostic.range.start.line + 1, character=0),
),
re.sub(
REPLACEMENTS[diagnostic.code]["pattern"],
REPLACEMENTS[diagnostic.code]["repl"],
lines[diagnostic.range.start.line],
),
new_line,
)


Expand Down
94 changes: 93 additions & 1 deletion src/test/python_tests/test_code_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,95 @@ def _handler(params):
@pytest.mark.parametrize(
("code", "contents", "new_text"),
[
(
"C0113:unneeded-not",
"""
if not not True:
pass""",
"""if True:
""",
),
(
"C0121:singleton-comparison",
"""
foo = True
if foo == True:
pass""",
"""if foo:
""",
),
(
"C0121:singleton-comparison",
"""
foo = True
if foo != False:
pass""",
"""if foo:
""",
),
(
"C0121:singleton-comparison",
"""
foo = True
if True == foo:
pass""",
"""if foo:
""",
),
(
"C0121:singleton-comparison",
"""
foo = True
if False != foo:
pass""",
"""if foo:
""",
),
(
"C0121:singleton-comparison",
"""
foo = True
if foo == False:
pass""",
"""if not foo:
""",
),
(
"C0121:singleton-comparison",
"""
foo = True
if foo != True:
pass""",
"""if not foo:
""",
),
(
"C0121:singleton-comparison",
"""
foo = True
if False == foo:
pass""",
"""if not foo:
""",
),
(
"C0121:singleton-comparison",
"""
foo = True
if True != foo:
pass""",
"""if not foo:
""",
),
(
"C0123:unidiomatic-typecheck",
"""
test_score = {"Biology": 95, "History": 80}
if type(test_score) is dict:
pass""",
"""if isinstance(test_score, dict):
""",
),
(
"R0205:useless-object-inheritance",
"""
Expand Down Expand Up @@ -246,4 +335,7 @@ def _handler(params):
for d in diagnostics
]

assert_that(actual_code_actions, is_(expected))
assert_that(
actual_code_actions[0]["edit"]["documentChanges"],
is_(expected[0]["edit"]["documentChanges"]),
)