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

Fix bug in invalid_range_checker #968

Closed
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Fix `naming-convention-violation` bug where `_` was considered an invalid variable name.
- Fix `naming-convention-violation` bug where top-level constants were being checked as regular variable names.
- Fix `invalid-range-index` bug where `range(1, 5, 3)` would not pass the check

### Enhancements

Expand Down
11 changes: 8 additions & 3 deletions python_ta/checkers/invalid_range_index_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ def visit_call(self, node):
if not (name in node.frame() or name in node.root()) and name == "range":
arg = node.args # the arguments of 'range' call
# guard nodes (e.g. Name) not properly handled by literal_eval.
if any([not isinstance(item, nodes.Const) for item in arg]):
if any(
[
not isinstance(item, nodes.Const) and not isinstance(item, nodes.UnaryOp)
for item in arg
]
):
return
eval_parm = list(map(lambda z: literal_eval(z.as_string()), arg))

Expand All @@ -44,8 +49,8 @@ def visit_call(self, node):
if (
abs(eval_parm[2]) >= abs(eval_parm[0] - eval_parm[1])
or eval_parm[2] == 0
or (eval_parm[0] > eval_parm[1] and eval_parm[2] < 0)
or (eval_parm[0] < eval_parm[1] and eval_parm[2] > 0)
or (eval_parm[0] > eval_parm[1] and eval_parm[2] > 0)
or (eval_parm[0] < eval_parm[1] and eval_parm[2] < 0)
):
args = "{}".format(node.lineno)
self.add_message("invalid-range-index", node=node, args=args)
Expand Down
85 changes: 85 additions & 0 deletions tests/test_custom_checkers/test_invalid_range_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""Test suite for testing the InvalidRangeIndexChecker."""
import astroid
import pylint.testutils

from python_ta.checkers.invalid_range_index_checker import InvalidRangeIndexChecker


class TestInvalidNameChecker(pylint.testutils.CheckerTestCase):
CHECKER_CLASS = InvalidRangeIndexChecker

def _test_invalid(self, src):
mod = astroid.parse(src)
node, *_ = mod.nodes_of_class(astroid.nodes.Call)

with self.assertAddsMessages(
pylint.testutils.MessageTest(msg_id="invalid-range-index", node=node, line=1, args="1"),
ignore_position=True,
):
self.checker.visit_call(node)

def _test_valid(self, src):
mod = astroid.parse(src)
node, *_ = mod.nodes_of_class(astroid.nodes.Call)

with self.assertNoMessages():
self.checker.visit_call(node)

def test_zero_param(self) -> None:
"""Tests range()"""

self._test_invalid("range()")

def test_one_param(self) -> None:
"""Tests range(x) where x >= 2"""

self._test_valid("range(5)")

def test_invalid_one_param(self) -> None:
"""Tests range(x) where x < 2"""

self._test_invalid("range(1)")
self._test_invalid("range(0)")
self._test_invalid("range(-1)")

def test_two_param(self) -> None:
"""Tests range(x, y) where y - x >= 2"""

self._test_valid("range(2, 5)")

def test_invalid_two_param(self) -> None:
"""Tests range(x, y) where y - x < 2"""

self._test_invalid("range(2, 3)")
self._test_invalid("range(2, 2)")
self._test_invalid("range(2, 1)")

def test_non_int_param(self) -> None:
"""Tests range(x) where x is not int"""

self._test_invalid("range(5.5)")

def test_three_param(self) -> None:
"""Tests range(x, y, z) where y - x < 2"""

self._test_valid("range(1, 5, 3)")
self._test_valid("range(5, 1, -3)")

def test_three_param_big_inc(self) -> None:
"""Tests range(x, y, z) where abs(z) > abs(x - y)"""

self._test_invalid("range(1, 5, 12)")
self._test_invalid("range(1, 5, -12)")
self._test_invalid("range(1, -5, 12)")
self._test_invalid("range(-1, -5, 12)")

def test_three_param_zero_inc(self) -> None:
"""Tests range(x, y, z) where z = 0"""

self._test_invalid("range(1, 5, 0)")

def test_three_param_opposite_inc(self) -> None:
"""Tests range(x, y, z) where z is going in the wrong direction"""

self._test_invalid("range(1, 5, -2)")
self._test_invalid("range(5, 1, 2)")