Skip to content

Commit

Permalink
Add exclusions for B905 (#388)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexey Nikitin <alexeynikitin@swatmobility.com>
  • Loading branch information
NewGlad and Alexey Nikitin committed Jul 3, 2023
1 parent a224d62 commit f1c391a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,11 @@ See `the exception chaining tutorial <https://docs.python.org/3/tutorial/errors.
for details.

**B905**: ``zip()`` without an explicit `strict=` parameter set. ``strict=True`` causes the resulting iterator
to raise a ``ValueError`` if the arguments are exhausted at differing lengths. The ``strict=`` argument
was added in Python 3.10, so don't enable this flag for code that should work on <3.10.
to raise a ``ValueError`` if the arguments are exhausted at differing lengths.

Exclusions are `itertools.count <https://docs.python.org/3/library/itertools.html#itertools.count>`_, `itertools.cycle <https://docs.python.org/3/library/itertools.html#itertools.cycle>`_ and `itertools.repeat <https://docs.python.org/3/library/itertools.html#itertools.repeat>`_ (with times=None) since they are infinite iterators.

The ``strict=`` argument was added in Python 3.10, so don't enable this flag for code that should work on <3.10.
For more information: https://peps.python.org/pep-0618/

**B906**: ``visit_`` function with no further call to a ``visit`` function. This is often an error, and will stop the visitor from recursing into the subnodes of a visited node. Consider adding a call ``self.generic_visit(node)`` at the end of the function.
Expand Down
44 changes: 39 additions & 5 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,12 +1169,46 @@ def check_for_b025(self, node):
for duplicate in duplicates:
self.errors.append(B025(node.lineno, node.col_offset, vars=(duplicate,)))

def check_for_b905(self, node):
if (
isinstance(node.func, ast.Name)
and node.func.id == "zip"
and not any(kw.arg == "strict" for kw in node.keywords)
@staticmethod
def _is_infinite_iterator(node: ast.expr) -> bool:
if not (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Attribute)
and isinstance(node.func.value, ast.Name)
and node.func.value.id == "itertools"
):
return False
if node.func.attr in {"cycle", "count"}:
return True
elif node.func.attr == "repeat":
if len(node.args) == 1 and len(node.keywords) == 0:
# itertools.repeat(iterable)
return True
if (
len(node.args) == 2
and isinstance(node.args[1], ast.Constant)
and node.args[1].value is None
):
# itertools.repeat(iterable, None)
return True
for kw in node.keywords:
# itertools.repeat(iterable, times=None)
if (
kw.arg == "times"
and isinstance(kw.value, ast.Constant)
and kw.value.value is None
):
return True

return False

def check_for_b905(self, node):
if not (isinstance(node.func, ast.Name) and node.func.id == "zip"):
return
for arg in node.args:
if self._is_infinite_iterator(arg):
return
if not any(kw.arg == "strict" for kw in node.keywords):
self.errors.append(B905(node.lineno, node.col_offset))

def check_for_b906(self, node: ast.FunctionDef):
Expand Down
12 changes: 12 additions & 0 deletions tests/b905_py310.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@
zip(range(3), strict=True)
zip("a", "b", strict=False)
zip("a", "b", "c", strict=True)

# infinite iterators from itertools module should not raise errors
import itertools

zip([1, 2, 3], itertools.cycle("ABCDEF"))
zip([1, 2, 3], itertools.count())
zip([1, 2, 3], itertools.repeat(1))
zip([1, 2, 3], itertools.repeat(1, None))
zip([1, 2, 3], itertools.repeat(1, times=None))

zip([1, 2, 3], itertools.repeat(1, 1))
zip([1, 2, 3], itertools.repeat(1, times=4))
2 changes: 2 additions & 0 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@ def test_b905(self):
B905(4, 15),
B905(5, 4),
B905(6, 0),
B905(21, 0),
B905(22, 0),
]
self.assertEqual(errors, self.errors(*expected))

Expand Down

0 comments on commit f1c391a

Please sign in to comment.