Skip to content

Commit ca05a35

Browse files
kasiumCopilot
andauthored
B912: check if map calls have strict flag (#517)
* B912: check if map calls have strict flag * Fix * Update README.rst Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5962bbd commit ca05a35

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,11 @@ This is meant to be enabled by developers writing visitors using the ``ast`` mod
367367

368368
The ``strict=`` argument was added in Python 3.13, so don't enable this flag for code that should work on <3.13.
369369

370+
.. _B912:
371+
372+
**B912**: ``map()`` without an explicit `strict=` parameter set. ``strict=True`` causes the resulting iterator
373+
to raise a ``ValueError`` if the arguments are exhausted at differing lengths.
374+
370375
.. _B950:
371376

372377
**B950**: Line too long. This is a pragmatic equivalent of
@@ -480,6 +485,7 @@ UNRELEASED
480485
* B042: New check for reminding to call super().__init__ in custom exceptions
481486
* flake8-bugbear now requires at least Python 3.9, like flake8>=7.2.0
482487
* B028: Skip if skip_file_prefixes is used (#503)
488+
* Add B912: `map()` without an explicit `strict=` parameter. (#516)
483489

484490
24.12.12
485491
~~~~~~~~

bugbear.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ def visit_Call(self, node) -> None:
531531
self.check_for_b905(node)
532532
self.check_for_b910(node)
533533
self.check_for_b911(node)
534+
self.check_for_b912(node)
534535

535536
# no need for copying, if used in nested calls it will be set to None
536537
current_b040_caught_exception = self.b040_caught_exception
@@ -1536,6 +1537,16 @@ def check_for_b905(self, node) -> None:
15361537
if not any(kw.arg == "strict" for kw in node.keywords):
15371538
self.add_error("B905", node)
15381539

1540+
def check_for_b912(self, node) -> None:
1541+
if not (
1542+
isinstance(node.func, ast.Name)
1543+
and node.func.id == "map"
1544+
and len(node.args) > 2
1545+
):
1546+
return
1547+
if not any(kw.arg == "strict" for kw in node.keywords):
1548+
self.add_error("B912", node)
1549+
15391550
def check_for_b906(self, node: ast.FunctionDef) -> None:
15401551
if not node.name.startswith("visit_"):
15411552
return
@@ -2465,6 +2476,7 @@ def __call__(self, lineno: int, col: int, vars: tuple[object, ...] = ()) -> erro
24652476
"B911": Error(
24662477
message="B911 `itertools.batched()` without an explicit `strict=` parameter."
24672478
),
2479+
"B912": Error(message="B912 `map()` without an explicit `strict=` parameter."),
24682480
"B950": Error(message="B950 line too long ({} > {} characters)"),
24692481
}
24702482

@@ -2480,5 +2492,6 @@ def __call__(self, lineno: int, col: int, vars: tuple[object, ...] = ()) -> erro
24802492
"B909",
24812493
"B910",
24822494
"B911",
2495+
"B912",
24832496
"B950",
24842497
]

tests/eval_files/b912_py314.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
map(lambda x: x, "a", "b") # B912: 0
2+
map(lambda x: x, "a", "b", *map("c")) # B912: 0
3+
map(lambda x: x, "a", map(lambda x: x, "a"), strict=True) # B912: 22
4+
5+
map()
6+
map(lambda x: x, "a")
7+
map(lambda x: x, "a", "b", strict=False)
8+
map(lambda x: x, "a", "b", "c", strict=True)

0 commit comments

Comments
 (0)