Skip to content

B914: add check for implicit string concatenation inside brackets - #642

Open
DeviousCardi wants to merge 3 commits into
PyCQA:mainfrom
DeviousCardi:feat/b914-implicit-string-concat
Open

DeviousCardi wants to merge 3 commits into
PyCQA:mainfrom
DeviousCardi:feat/b914-implicit-string-concat

Conversation

@DeviousCardi

@DeviousCardi DeviousCardi commented Nov 18, 2026 •

Copy link
Copy Markdown

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:

__all__ = ["alpha", "beta" "gamma"]   # B914, a comma is probably missing

The AST doesn't record implicit concatenation, so the check reads the tokenize stream, 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 are tests/eval_files/b914.py and b914_py314.py, option tests and unit tests for the tokenizer helper. I also added a README entry and a changelog line. pytest tests passes 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.py and rstcheck README.rst are 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.

@cooperlees cooperlees left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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:

  1. The error points at a line that is fine, which may be well above the actual mistake.
  2. # noqa: B914 only 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
        pass

This 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_tokens as an __init__ parameter and get the token list for free, without running generate_tokens again. On bugbear.py itself (about 4.6k lines), _implicit_string_concatenations takes 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_warn returns True, so API users who don't pass options will pay this cost on every file.
  • The hasattr(self.options, "select") guard in gen_token_based_checks is a bit surprising when you read it. A one-line comment saying it protects should_warn from options objects that don't have select would help.

Happy to approve once the location issue is fixed.

…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.
@DeviousCardi

Copy link
Copy Markdown
Author

Thanks for the review. Pushed e082cd8 and merged upstream main (76bf829, the only conflict was the README changelog).

  • Location: B914 now reports the start of the first string in the first same-line pair, so the error and # noqa: B914 sit on the line with the missing comma. I added cases to b914.py and the unit test where the pair is not on the run's first line, plus a flake8 run that checks # noqa on that line.
  • Soft keywords: match/case as the first token of a statement are no longer treated as calls, so match ("a" "b"): and case ("a" "b"): are not reported. The limitation is that a call to a function named match written as a statement of its own is not checked. That is noted in a comment.
  • Tokenizing twice: the checker takes file_tokens now and only tokenizes itself when they are not given (API use). I checked that flake8 passes them.
  • hasattr(self.options, "select"): added a comment.

Tests: full pytest tests on Python 3.11, 3.12, 3.13 and 3.14 passes (181, 182, 183, 188 passed, rest skipped), and mypy --strict bugbear.py is clean. I did not run 3.15.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants