-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add option to provide a reason to --add-noqa
#21294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to provide a reason to --add-noqa
#21294
Conversation
6cf87b0 to
9b93e14
Compare
Implements the ability to add custom reason/comment text when using the --add-noqa command. This allows users to distinguish auto-generated noqa comments from manual ones and add contextual information. Usage: ruff check --add-noqa --add-noqa-reason "TODO: fix later" The reason is appended after the rule codes in the noqa comment: # noqa: F401, F841 TODO: fix later Changes: - Add --add-noqa-reason CLI flag that requires --add-noqa - Thread reason parameter through add_noqa command chain - Update NoqaEdit struct to include optional reason field - Modify write() method to append reason before line ending - Update all test calls to pass None for backward compatibility Fixes #10070 Sanitize newlines in --add-noqa-reason to prevent syntax errors If a user passes a reason containing newline characters (e.g., via bash $'line1\nline2'), it would break Python syntax by creating actual line breaks in the noqa comment: import os # noqa: F401 line1 line2 # <- This becomes code, not a comment! This commit sanitizes the reason text by replacing all \n and \r characters with spaces, ensuring the noqa comment stays on one line. Added comprehensive test coverage for: - Unix newlines (\n) - Windows newlines (\r\n) - Multiple consecutive newlines Validate --add-noqa-reason for newlines instead of sanitizing Changed approach from silently sanitizing newlines to explicitly rejecting them with a clear error message. This follows the "fail fast" principle - it's better to alert users to problematic input rather than silently modifying it. The CLI now validates the reason text upfront and aborts with: "error: --add-noqa-reason cannot contain newline characters" This prevents syntax errors from malformed Python comments while being explicit about what input is accepted. Changes: - Add validation in lib.rs that checks for \n and \r characters - Remove sanitization code from noqa.rs write() method - Update test to verify normal reason text works correctly - Document that validation happens at CLI level Refactor --add-noqa to accept optional reason value directly Changed from two separate flags (--add-noqa and --add-noqa-reason) to a single flag with an optional value for better ergonomics. Before: ruff check --add-noqa --add-noqa-reason "TODO: fix" After: ruff check --add-noqa "TODO: fix" ruff check --add-noqa # still works without reason Implementation: - Changed `add_noqa: bool` to `add_noqa: Option<String>` in args - Use clap's default_missing_value to handle flag without value - Empty string means flag present without value (no reason) - Non-empty string is the reason text to append - Removed separate add_noqa_reason field entirely This provides a cleaner, more intuitive CLI interface while maintaining full backward compatibility with the original --add-noqa behavior. Add comprehensive CLI tests for --add-noqa feature Created new test file tests/cli/add_noqa.rs with end-to-end tests: - add_noqa_without_reason: Verify original behavior still works - add_noqa_with_reason: Test appending custom reason text - add_noqa_with_short_reason: Test with short marker like "auto" - add_noqa_no_violations: Verify no changes when no violations - add_noqa_with_existing_comment: Test merging with existing noqa - add_noqa_multiple_violations_same_line: Test multiple codes - add_noqa_with_newline_error: Verify newline validation - add_noqa_multiple_files: Test bulk operations across files These tests verify: 1. Files are correctly modified in-place 2. Reason text is properly appended after rule codes 3. Multiple violations on same line are combined 4. Newlines are rejected with clear error message 5. Works correctly with multiple files Tests use insta snapshots for both command output and file contents. Simplify tests to only essential cases for --add-noqa reason Removed excessive integration tests and unit tests. Added only 2 focused tests to existing lint.rs test suite: 1. add_noqa_with_reason: Tests appending reason text to noqa directives 2. add_noqa_with_newline_in_reason: Validates newline rejection Also removed unit test from noqa.rs since integration tests are sufficient. Since --add-noqa is already well-tested with 7 existing tests covering base functionality, these 2 new tests are enough to verify the reason parameter feature. Fix test formatting to match existing test patterns - Remove leading whitespace from test input strings - Change snapshot format from @r###" to @r" to match existing tests - Input strings should not have indentation since dedent is applied This should fix CI test failures. Refactor tests to match existing --add-noqa test patterns Changes to follow the exact pattern of existing tests: - Use CliTest::new() + write_file() instead of with_file() - Use .check_command() which includes standard flags - Use separate .arg() calls instead of .args() - Pass specific filename instead of current directory This should fix remaining CI test failures. Fix CLI argument handling: require equals syntax for optional value Changes: - Add require_equals = true to --add-noqa argument definition - Update tests to use --add-noqa=value syntax instead of --add-noqa value - This makes the optional value syntax unambiguous Usage: ruff check --add-noqa # no reason ruff check --add-noqa="TODO: fix" # with reason This should fix CI test, clippy, and formatting failures. Simplify code to use .then() instead of if/else for clippy Fix formatting: use then_some instead of then for better idiomaticity Run cargo fmt to fix formatting Fix test snapshot indentation Remove leading spaces from inline snapshot expectations in add_noqa tests. The actual file content doesn't have leading indentation, so the snapshots shouldn't either. Fix add_noqa integration tests - Update add_noqa_with_reason test to trigger both F401 and F841 violations by using a function (F841 only applies to local variables, not module-level) - Fix error message snapshot for newline validation to match actual output format Fix clippy warning for inline format arg Nits, clippy, generate all
e853ee0 to
7208ec5
Compare
|
| value_name = "REASON", | ||
| default_missing_value = "", | ||
| num_args = 0..=1, | ||
| require_equals = true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want require_equals = true? I usually prefer not to use the = (like in the PR summary), but I'm fine either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might have to but I'm curious to hear your opinion too. I, honestly, didn't realize that claude added it until you pointed it out. So I tried to remove it but then run into issues when using --add-noqa with stdin filenames:
ruff check --add-noqa -
clap no incorrectly parses the - as the noqa comment.
Or running
ruff cehck --add-noqa file.py
Will use file.py as the noqa reason rather than only adding noqa comments to file.py. That's why I think we should keep it as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah that makes sense! Seems fine as-is then.
Summary
This PR adds an optional reason to
--add-noqathat will be added to every insertednoqacomment.How to use:
Fixes #10070
Test Plan
Added CLI tests