Repository navigation
B914: add check for implicit string concatenation inside brackets - #642
DeviousCardi wants to merge 3 commits into
Conversation
cooperlees
left a comment
There was a problem hiding this comment.
This review was written by Claude (Anthropic's AI assistant) on behalf of @cooperlees.
Thanks @DeviousCardi. This is a nice way past the libcst objection on #435. Doing it with tokenize keeps bugbear pure Python, B914 is opt-in like the other B9xx checks, and the eval files cover a lot of ground: f-strings, t-strings on 3.14, nested brackets, the ("a" "b") exemption, and source that doesn't tokenize. Finding two real missing commas in pygments is a good sign the same-line rule is the right trade-off.
On the PR branch, pytest tests -k "not selfclean" gives 176 passed, 3 skipped, and CI is green. One thing needs fixing before merge, though.
Requested change: report the line where the strings are concatenated
_end_run returns run[0][0], the first string of the whole run, even when the same-line pair comes several lines later. I ran it through flake8 with the plugin loaded:
values = [
"first part of a long string " # <- B914 is reported here (2:5)
"second part "
"more "
"x" "y" # <- the missing comma is here
]
other = [
"first part of a long string " # <- still reported (8:5)
"second part "
"more "
"x" "y" # noqa: B914 # <- this noqa does not suppress it
]t.py:2:5: B914 Implicit string concatenation inside brackets. ...
t.py:8:5: B914 Implicit string concatenation inside brackets. ...
That causes two problems:
- The error points at a line that is fine, which may be well above the actual mistake.
# noqa: B914only works on the first line of the run, not on the line a user would naturally annotate. (flake8's noqa line mapping joins multi-line strings, not bracketed expressions.)
Could you report the start of the first string in the first same-line pair instead? Something like:
for first, second in itertools.pairwise(run):
if first[1][0] == second[0][0]:
return first[0]Please also update the "b" "c" # B914: 4 case in b914.py and the 'x = ["a" "b"\n "c" "d"]' unit test so they pin this down, and add a case where the pair is not on the run's first line.
Smaller issue: soft keywords are treated as calls
_follows_expression uses iskeyword(), which doesn't include the soft keywords. So these are taken as calls and reported:
match ("a" "b"): # B914 at 1:7
case ("a" "b"): # B914 at 2:10
passThis is unlikely to matter much in real code, and match(...) can also really be a function call. But if there's a cheap way to handle match/case at the start of a statement, it would be worth doing. Otherwise a comment noting the limitation is fine.
Non-blocking suggestions
- Tokenizing twice: flake8 has already tokenized the file. A plugin can ask for
file_tokensas an__init__parameter and get the token list for free, without runninggenerate_tokensagain. Onbugbear.pyitself (about 4.6k lines),_implicit_string_concatenationstakes about 37 ms per run. That's fine since it only runs when B914 is selected, but reusing flake8's tokens would avoid the cost entirely. Without options (options is None),should_warnreturnsTrue, so API users who don't pass options will pay this cost on every file. - The
hasattr(self.options, "select")guard ingen_token_based_checksis a bit surprising when you read it. A one-line comment saying it protectsshould_warnfrom options objects that don't haveselectwould help.
Happy to approve once the location issue is fixed.
# Conflicts: # README.rst
…tokens Report the first string of the first same-line pair instead of the first string of the run, so the error and a # noqa are on the line with the missing comma. Treat match and case at the start of a statement as keywords, not calls. Use the tokens flake8 passes as file_tokens, and tokenize only when they are not given.
|
Thanks for the review. Pushed e082cd8 and merged upstream main (76bf829, the only conflict was the README changelog).
Tests: full |
Refs #156. Replaces #435, which was closed because it needed
libcst.This adds B914, an opt-in check (off by default like the other B9xx codes) for two string literals concatenated on the same line inside brackets or call arguments:
The AST doesn't record implicit concatenation, so the check reads the
tokenizestream, and only when B914 is selected. That keeps bugbear pure Python, which was the objection on #435. It only reports same-line concatenation, to stay quiet. Strings that continue on the next line, concatenation outside brackets, and a lone("a" "b")inside grouping parentheses are not reported.I ran it over the site-packages of a venv (flake8, docutils, pygments, tomli-w) and it found 10 places. Two are real missing commas in pygments. The rest are deliberate.
The code is in
bugbear.py. Tests aretests/eval_files/b914.pyandb914_py314.py, option tests and unit tests for the tokenizer helper. I also added a README entry and a changelog line.pytest testspasses on Python 3.11 (171 passed, 10 skipped), 3.12 (172, 9), 3.13 (173, 8) and 3.14 (178, 3). The skips are existing version-gated eval files.black --preview --check,isort --check,flake8,mypy bugbear.pyandrstcheck README.rstare clean.Not checked: Python 3.15, and the other pre-commit hooks.
AI-assisted: I wrote this with an AI agent and checked it by running the tests and checks listed above.