Skip to content

Commit

Permalink
SIM110: Remove false-positives (#96)
Browse files Browse the repository at this point in the history
BUG: Don't show SIM110 when an exception is returned instead of
returning

Closes #34
  • Loading branch information
MartinThoma committed Feb 11, 2022
1 parent 2525244 commit f439f0d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 16 additions & 1 deletion flake8_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ def _get_sim110_sim111(node: ast.For) -> List[Tuple[int, int, str]]:
return errors
if not hasattr(node.body[0].body[0].value, "value"):
return errors
if isinstance(node.next_sibling, ast.Raise): # type: ignore
return errors
check = to_source(node.body[0].test)
target = to_source(node.target)
iterable = to_source(node.iter)
Expand Down Expand Up @@ -1202,7 +1204,15 @@ def is_stmt_equal(a: ast.stmt, b: ast.stmt) -> bool:
if type(a) is not type(b):
return False
if isinstance(a, ast.AST):
specials = ("lineno", "col_offset", "ctx", "end_lineno", "parent")
specials = (
"lineno",
"col_offset",
"ctx",
"end_lineno",
"parent",
"previous_sibling",
"next_sibling",
)
for k, v in vars(a).items():
if k.startswith("_") or k in specials:
continue
Expand Down Expand Up @@ -1916,8 +1926,13 @@ def run(self) -> Generator[Tuple[int, int, str, Type[Any]], None, None]:

# Add parent
for node in ast.walk(self._tree):
previous_sibling = None
for child in ast.iter_child_nodes(node):
child.previous_sibling = previous_sibling # type: ignore
if previous_sibling:
child.previous_sibling.next_sibling = child # type: ignore
child.parent = node # type: ignore
previous_sibling = child
visitor.visit(self._tree)

for line, col, msg in visitor.errors:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ def opt2(iterable):
assert ret == {"1:0 SIM110 Use 'return any(check(x) for x in iterable)'"}


@pytest.mark.xfail(reason="See issue #34: False-positive for SIM110")
def test_sim110_raise_exception():
ret = _results(
"""
Expand All @@ -300,7 +299,8 @@ def test_sim110_raise_exception():
return True
raise Exception"""
)
assert ret == set()
for el in ret:
assert "SIM110" not in el


def test_sim111_all():
Expand Down

0 comments on commit f439f0d

Please sign in to comment.