Skip to content

Fix spurious nosec warning on f-strings with specific test IDs#1362

Open
worksbyfriday wants to merge 2 commits intoPyCQA:mainfrom
worksbyfriday:fix-nosec-fstring-warning
Open

Fix spurious nosec warning on f-strings with specific test IDs#1362
worksbyfriday wants to merge 2 commits intoPyCQA:mainfrom
worksbyfriday:fix-nosec-fstring-warning

Conversation

@worksbyfriday
Copy link
Copy Markdown
Contributor

Summary

  • When # nosec B608 successfully suppressed an issue in an f-string, bandit emitted a spurious warning: nosec encountered (B608), but no failed test on line N
  • This happened because f-strings are parsed as JoinedStr containing multiple Constant sub-nodes. B608 intentionally only checks the first sub-node (to avoid duplicate issues), but the "unused nosec" warning fired for the other sub-nodes that returned no result
  • Fixed by tracking which (test_id, line) pairs have been suppressed, and skipping the warning when the same test was already suppressed on lines in the same range

Fixes #1204

Test plan

  • All 79 functional tests pass
  • Single-line f-string: f"SELECT * FROM {table}" # nosec B608 — no spurious warning
  • Multiline f-string: no spurious warning
  • Genuinely unused nosec B608 on non-SQL string — warning still fires correctly
  • ruff check and ruff format pass

Friday and others added 2 commits February 17, 2026 09:18
When a `# nosec B608` comment successfully suppressed an issue in an
f-string, bandit emitted a spurious warning "nosec encountered (B608),
but no failed test on line N" for the other Constant sub-nodes of the
JoinedStr that intentionally returned no result.

Track which (test_id, line) pairs have been suppressed and skip the
warning when the same test was already suppressed on the same line range.

Fixes PyCQA#1204

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

f-strings marked with a # nosec BXXX show an incorrect warning about no failed tests appears

1 participant