Skip to content

Fix #nosec with test ID not counted in Total lines skipped#1408

Open
jonasboos wants to merge 2 commits into
PyCQA:mainfrom
jonasboos:fix-1205-nosec-count
Open

Fix #nosec with test ID not counted in Total lines skipped#1408
jonasboos wants to merge 2 commits into
PyCQA:mainfrom
jonasboos:fix-1205-nosec-count

Conversation

@jonasboos
Copy link
Copy Markdown

Problem

When a # nosec comment includes a specific test ID (e.g., # nosec B608), the line is not counted in the "Total lines skipped (#nosec)" metric. Only bare # nosec comments increment the nosec counter, while specific # nosec BXXX comments only increment the skipped_tests counter.

This means if your code uses # nosec B608, the output shows:

Total lines skipped (#nosec): 0

even though the line was explicitly skipped by a nosec comment.

Fix

When a # nosec comment with a specific test ID is encountered, both note_nosec() and note_skipped_test() are now called. This accurately reflects that the line was skipped by a nosec comment, regardless of whether it was bare or specific.

The skipped_tests metric still tracks how many skips were test-specific, and the nosec metric now correctly counts all lines with any #nosec comment.

Additional changes

Resolves: #1205

When a #nosec comment includes a specific test ID (e.g., #nosec B608),
the line was only counted in skipped_tests but not in the nosec metric.
This meant 'Total lines skipped (#nosec)' showed 0 even when lines had
nosec comments, which was confusing.

Now both note_nosec() and note_skipped_test() are called when a
specific test ID nosec is encountered, so the total accurately reflects
all lines with any #nosec comment.

Also adds the skipped_tests metric display to the screen and HTML
formatters for consistency with the text formatter.

Resolves: PyCQA#1205
Copilot AI review requested due to automatic review settings May 10, 2026 19:13
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes Bandit’s Total lines skipped (#nosec) metric so that # nosec BXXX (test-specific) suppressions are counted in the nosec total, not only in the skipped_tests total. It also surfaces the skipped_tests metric in the screen and HTML formatters for consistency with the text formatter.

Changes:

  • Increment nosec for test-specific # nosec BXXX suppressions (in addition to skipped_tests) when a finding is actually skipped.
  • Display the skipped_tests metric in the screen and HTML reports.
  • Update functional/unit tests to reflect the new counting behavior and avoid missing metric keys in formatter tests.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
bandit/core/tester.py Counts test-specific nosec skips in both nosec and skipped_tests.
bandit/formatters/screen.py Adds skipped_tests to the on-screen metrics summary.
bandit/formatters/html.py Adds skipped_tests to the HTML metrics summary/template.
tests/functional/test_functional.py Updates expected nosec totals for the functional run.
tests/unit/formatters/test_screen.py Updates mocked metrics totals to include skipped_tests.
tests/unit/formatters/test_html.py Updates mocked metrics totals to include skipped_tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

get_issue_list.return_value = [issue_a, issue_b]

self.manager.metrics.data["_totals"] = {"loc": 1000, "nosec": 50}
self.manager.metrics.data["_totals"] = {
@mock.patch("bandit.core.manager.BanditManager.get_issue_list")
def test_report_contents(self, get_issue_list, get_code):
self.manager.metrics.data["_totals"] = {"loc": 1000, "nosec": 50}
self.manager.metrics.data["_totals"] = {
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.

#nosec BXXX does not contribute to Total lines skipped (#nosec)

2 participants