nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: MartinThoma/flake8-simplify
Pull request #95 (2022-02-10 06:46:48)

SIM106: Remove false-positives
Don't trigger if the 'else' block ends with a
ValueError or a NotImplementedError


Patch:
diff --git a/flake8_simplify.py b/flake8_simplify.py
index c2da734..a49f270 100644
--- a/flake8_simplify.py
+++ b/flake8_simplify.py
@@ -421,6 +421,18 @@ def _get_sim106(node: ast.If) -> List[Tuple[int, int, str]]:
     )
     if not (just_one or many):
         return errors
+    ast_raise = node.orelse[-1]
+    if not isinstance(ast_raise, ast.Raise):
+        return errors
+    ast_raised = ast_raise.exc
+    if (
+        isinstance(ast_raised, ast.Call)
+        and ast_raised.func
+        and isinstance(ast_raised.func, ast.Name)
+        and ast_raised.func.id in ["ValueError", "NotImplementedError"]
+    ):
+        return errors
+
     errors.append((node.lineno, node.col_offset, SIM106))
     return errors
 


Test patch:
diff --git a/tests/test_simplify.py b/tests/test_simplify.py
index b429d04..d59ba34 100644
--- a/tests/test_simplify.py
+++ b/tests/test_simplify.py
@@ -195,14 +195,33 @@ else:
     assert ret == {"1:0 SIM106 Handle error-cases first"}
 
 
-def test_sim106_no():
-    ret = _results(
+@pytest.mark.parametrize(
+    "s",
+    (
+        """if image_extension in ['.jpg', '.jpeg']:
+    return 'JPEG'
+elif image_extension in ['.png']:
+    return 'PNG'
+else:
+    raise ValueError("Unknwon image extension {image extension}")""",
+        """if image_extension in ['.jpg', '.jpeg']:
+    return 'JPEG'
+elif image_extension in ['.png']:
+    return 'PNG'
+else:
+    logger.error("Unknwon image extension {image extension}")
+    raise ValueError("Unknwon image extension {image extension}")""",
         """if cond:
     raise Exception
 else:
-    raise Exception"""
-    )
-    assert ret == set()
+    raise Exception""",
+    ),
+    ids=["ValueError", "ValueError-with-logging", "TwoExceptions"],
+)
+def test_sim106_false_positive(s):
+    ret = _results(s)
+    for el in ret:
+        assert "SIM106" not in el
 
 
 def test_sim107():
Repository: MartinThoma/flake8-simplify
Pull request #128 (2022-03-29 05:50:05)

SIM908: Show false-positive
See #126


Patch:
diff --git a/flake8_simplify/rules/ast_if.py b/flake8_simplify/rules/ast_if.py
index eacf557..b45c98e 100644
--- a/flake8_simplify/rules/ast_if.py
+++ b/flake8_simplify/rules/ast_if.py
@@ -341,6 +341,12 @@ def get_sim908(node: ast.If) -> List[Tuple[int, int, str]]:
         and isinstance(node.body[0].targets[0], ast.Name)
     ):
         return errors
+
+    test_var = node.test.left
+    slice_var = node.body[0].value.slice
+    if to_source(slice_var) != to_source(test_var):
+        return errors
+
     key = to_source(node.test.left)
     dictname = to_source(node.test.comparators[0])
     errors.append(


Test patch:
diff --git a/tests/test_900_rules.py b/tests/test_900_rules.py
index 0410811..31226aa 100644
--- a/tests/test_900_rules.py
+++ b/tests/test_900_rules.py
@@ -154,6 +154,25 @@ if "some_key" in some_dict:
     }
 
 
+@pytest.mark.parametrize(
+    "s",
+    (
+        # Credits to jonyscathe for this example:
+        # https://github.com/MartinThoma/flake8-simplify/issues/126
+        """if "." in resistance:
+    # Swap '.' with suffix
+    resistance = resistance.replace(".", resistance[-1])[:-1]""",
+        """if "." in iterable:
+    iterable = iterable[:-1]""",
+    ),
+    ids=("issue-125", "issue-125-simplified"),
+)
+def test_sim908_false_positive(s):
+    results = _results(s)
+    for el in results:
+        assert "SIM908" not in el
+
+
 @pytest.mark.parametrize(
     ("s", "msg"),
     (