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

Add B034: re.sub/subn/split must pass flags/count/maxsplit as keyword arguments #398

Merged
merged 6 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ second usage. Save the result to a list if the result is needed multiple times.

**B033**: Sets should not contain duplicate items. Duplicate items will be replaced with a single item at runtime.

**B034**: Calls to `re.sub`, `re.subn` or `re.split` should pass `flags` or `output`/`maxsplit` as keyword arguments. Not doing so is a very common source of confusion, and will likely be deprecated in future python versions.
jakkdl marked this conversation as resolved.
Show resolved Hide resolved

Opinionated warnings
~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -329,6 +331,10 @@ MIT
Change Log
----------

Future
~~~~~~
* Add B034: re.sub/subn/split must pass flags/count/maxsplit as keyword arguments.

23.6.5
~~~~~~

Expand Down
28 changes: 27 additions & 1 deletion bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,9 @@ def visit_Call(self, node):

self.check_for_b026(node)

self.check_for_b905(node)
self.check_for_b028(node)
self.check_for_b034(node)
self.check_for_b905(node)
self.generic_visit(node)

def visit_Module(self, node):
Expand Down Expand Up @@ -1368,6 +1369,25 @@ def check_for_b033(self, node):
else:
seen.add(elt.value)

def check_for_b034(self, node: ast.Call):
if not isinstance(node.func, ast.Attribute):
return
if not isinstance(node.func.value, ast.Name) or node.func.value.id != "re":
return

def check(num_args, param_name):
if len(node.args) > num_args:
self.errors.append(
B034(
node.lineno, node.col_offset, vars=(node.func.attr, param_name)
jakkdl marked this conversation as resolved.
Show resolved Hide resolved
)
)

if node.func.attr in ("sub", "subn"):
check(3, "count")
elif node.func.attr == "split":
check(2, "maxsplit")


def compose_call_path(node):
if isinstance(node, ast.Attribute):
Expand Down Expand Up @@ -1772,6 +1792,12 @@ def visit_Lambda(self, node):
)
)

B034 = Error(
message=(
"B034 {} should pass `{}` and `flags` as keyword arguments to avoid confusion."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"B034 {} should pass `{}` and `flags` as keyword arguments to avoid confusion."
"B034 {} should pass `{}` and `flags` as keyword arguments to avoid confusion due to different argument positions."

)
)

# Warnings disabled by default.
B901 = Error(
message=(
Expand Down
30 changes: 30 additions & 0 deletions tests/b034.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import re
from re import sub

# error
re.sub("a", "b", "aaa", re.IGNORECASE)
re.sub("a", "b", "aaa", 5)
re.sub("a", "b", "aaa", 5, re.IGNORECASE)
re.subn("a", "b", "aaa", re.IGNORECASE)
re.subn("a", "b", "aaa", 5)
re.subn("a", "b", "aaa", 5, re.IGNORECASE)
re.split(" ", "a a a a", re.I)
re.split(" ", "a a a a", 2)
re.split(" ", "a a a a", 2, re.I)

# okay
re.sub("a", "b", "aaa")
re.sub("a", "b", "aaa", flags=re.IGNORECASE)
re.sub("a", "b", "aaa", count=5)
re.sub("a", "b", "aaa", count=5, flags=re.IGNORECASE)
re.subn("a", "b", "aaa")
re.subn("a", "b", "aaa", flags=re.IGNORECASE)
re.subn("a", "b", "aaa", count=5)
re.subn("a", "b", "aaa", count=5, flags=re.IGNORECASE)
re.split(" ", "a a a a", flags=re.I)
re.split(" ", "a a a a", maxsplit=2)
re.split(" ", "a a a a", maxsplit=2, flags=re.I)


# not covered
sub("a", "b", "aaa", re.IGNORECASE)
19 changes: 19 additions & 0 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
B031,
B032,
B033,
B034,
B901,
B902,
B903,
Expand Down Expand Up @@ -505,6 +506,23 @@ def test_b033(self):
)
self.assertEqual(errors, expected)

def test_b034(self):
filename = Path(__file__).absolute().parent / "b034.py"
bbc = BugBearChecker(filename=str(filename))
errors = list(bbc.run())
expected = self.errors(
B034(5, 0, vars=("sub", "count")),
B034(6, 0, vars=("sub", "count")),
B034(7, 0, vars=("sub", "count")),
B034(8, 0, vars=("subn", "count")),
B034(9, 0, vars=("subn", "count")),
B034(10, 0, vars=("subn", "count")),
B034(11, 0, vars=("split", "maxsplit")),
B034(12, 0, vars=("split", "maxsplit")),
B034(13, 0, vars=("split", "maxsplit")),
)
self.assertEqual(errors, expected)

def test_b908(self):
filename = Path(__file__).absolute().parent / "b908.py"
bbc = BugBearChecker(filename=str(filename))
Expand All @@ -520,6 +538,7 @@ def test_b908(self):
)
self.assertEqual(errors, expected)

@unittest.skipIf(sys.version_info < (3, 8), "not implemented for <3.8")
jakkdl marked this conversation as resolved.
Show resolved Hide resolved
def test_b907(self):
filename = Path(__file__).absolute().parent / "b907.py"
bbc = BugBearChecker(filename=str(filename))
Expand Down