From 980088f8d43845786585b62528707c19a2970a59 Mon Sep 17 00:00:00 2001 From: Jendrik Seipp Date: Sun, 24 Nov 2024 20:33:57 +0100 Subject: [PATCH] Split into two functions. --- tests/__init__.py | 24 ++++++++++++------------ tests/test_reachability.py | 8 +++----- vulture/core.py | 2 +- vulture/reachability.py | 1 + 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index f235a817..4167a4dc 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -31,22 +31,22 @@ def check(items_or_names, expected_names): assert items_or_names == set(expected_names) -def check_unreachable(v, lineno, size, name, multiple=False): - # Match by lineno, if we allow multiple unreachables, otherwise - # the first unreachable item is taken. - if multiple: - item = next( - item for item in v.unreachable_code if item.first_lineno == lineno - ) - else: - assert len(v.unreachable_code) == 1 - item = v.unreachable_code[0] - assert item.first_lineno == lineno - +def check_unreachable(v, lineno, size, name): + assert len(v.unreachable_code) == 1 + item = v.unreachable_code[0] + assert item.first_lineno == lineno assert item.size == size assert item.name == name +def check_multiple_unreachable(v, checks): + assert len(v.unreachable_code) == len(checks) + for item, (lineno, size, name) in zip(v.unreachable_code, checks): + assert item.first_lineno == lineno + assert item.size == size + assert item.name == name + + @pytest.fixture def v(): return core.Vulture(verbose=True) diff --git a/tests/test_reachability.py b/tests/test_reachability.py index 8469d86c..a7eb24a3 100644 --- a/tests/test_reachability.py +++ b/tests/test_reachability.py @@ -1,4 +1,4 @@ -from . import check_unreachable +from . import check_multiple_unreachable, check_unreachable from . import v assert v # Silence pyflakes @@ -479,8 +479,7 @@ def foo(a): print(":-(") """ ) - check_unreachable(v, 2, 2, "if", multiple=True) - check_unreachable(v, 4, 1, "if", multiple=True) + check_multiple_unreachable(v, [(2, 2, "if"), (4, 1, "if")]) def test_if_true_return_else(v): @@ -494,8 +493,7 @@ def foo(a): print(":-(") """ ) - check_unreachable(v, 5, 1, "else", multiple=True) - check_unreachable(v, 6, 1, "if", multiple=True) + check_multiple_unreachable(v, [(5, 1, "else"), (6, 1, "if")]) def test_if_some_branches_return(v): diff --git a/vulture/core.py b/vulture/core.py index 48e14af6..5b7db43a 100644 --- a/vulture/core.py +++ b/vulture/core.py @@ -12,8 +12,8 @@ from vulture import noqa from vulture import utils from vulture.config import InputError, make_config -from vulture.utils import ExitCode from vulture.reachability import Reachability +from vulture.utils import ExitCode DEFAULT_CONFIDENCE = 60 diff --git a/vulture/reachability.py b/vulture/reachability.py index b12cf2f3..fc043828 100644 --- a/vulture/reachability.py +++ b/vulture/reachability.py @@ -9,6 +9,7 @@ def __init__(self, report): self._no_fall_through_nodes = set() def visit(self, node): + """When called, all children of this node have already been visited.""" if isinstance(node, (ast.Break, ast.Continue, ast.Return, ast.Raise)): self._mark_as_no_fall_through(node) elif isinstance(