Skip to content

Fix spurious nosec warnings on multiline statements#1359

Open
worksbyfriday wants to merge 2 commits intoPyCQA:mainfrom
worksbyfriday:fix-nosec-multiline-warnings
Open

Fix spurious nosec warnings on multiline statements#1359
worksbyfriday wants to merge 2 commits intoPyCQA:mainfrom
worksbyfriday:fix-nosec-multiline-warnings

Conversation

@worksbyfriday
Copy link
Copy Markdown
Contributor

Fixes #1352.

When a # nosec B105 comment appears on one line of a multiline statement (e.g., a dict literal spanning lines 1-17), bandit checks the entire statement's line range for nosec comments via utils.get_nosec(). This means every line of the multiline statement is treated as having a nosec comment, and for each line where the test doesn't fire, bandit warns:

[tester] WARNING nosec encountered (B105), but no failed test on line 1
[tester] WARNING nosec encountered (B105), but no failed test on line 2
...

The fix changes the "no result" warning path to check only nosec_lines.get(temp_context["lineno"]) (the specific line being checked) rather than searching the entire linerange. This way, the warning is only emitted for the line that actually has the # nosec comment, not for every line in the multiline statement.

Friday and others added 2 commits February 17, 2026 07:18
When a `# nosec` comment appears on one line of a multiline statement
(e.g., a dict literal spanning multiple lines), bandit was checking
the entire statement's line range for nosec comments. This caused
"nosec encountered but no failed test" warnings on every other line
of the same statement.

The fix checks only the specific line being tested for a nosec comment,
not the full linerange of the multiline node.

Fixes PyCQA#1352
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.

Suppressing B105 on a multiline dict assignment

1 participant