Repository navigation
fix: report malformed ignore comments as invalid-ignore-comment - #5583
Aayushyaash wants to merge 2 commits into
Conversation
Co-authored-by: Mikko Leppänen <mleppan23@gmail.com>
Co-authored-by: Mikko Leppänen <mleppan23@gmail.com>
|
✅ This PR meets all requirements from our pull request checklist. Optional recommendations:
Note: this comment updates automatically after changes to the PR. Updates may take up to half an hour. |
|
This pull request has been imported. If you are a Meta employee, you can view this in D130518155. (Because this pull request was imported automatically, there will not be any future comments.) |
|
Pr targeting #3450 builds on top of this pr. Once this pr is merged, I'll rebase the changes onto |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
connernilsen
left a comment
There was a problem hiding this comment.
Hey @Aayushyaash, thanks for working on this. A few comments on things to look at. Let me know if you have any questions or disagree with any of the comments I've made
| // before (a blanket for tools whose codes we ignore, a | ||
| // no-op for Pyrefly) so the diagnostic stays additive. | ||
| ParsedIgnore::Malformed { | ||
| suppression: Some(suppression), |
There was a problem hiding this comment.
An empty list suppresses [""], which suppresses nothing. This causes two diagnostics to be produced here, such as:
x = 1 # pyrefly: ignore[]
# ERROR 1:8-27: Unused `# pyrefly: ignore` comment for code(s): [unused-ignore]
# WARN 1:8-27: Empty error-code list in `# pyrefly: ignore[]` [invalid-ignore-comment]
Can we update this to skip unused ignore errors when they come from bad content?
Also, suppression.kind.is_empty() can't be true here, because parse_error_codes always returns at least one element. The .all(..) check is enough on its own, but up to you if you want to remove it or not
| fn collect_invalid_ignore_comment_errors_for_module( | ||
| module: &Module, | ||
| enabled_ignores: &SmallSet<Tool>, | ||
| ) -> Vec<Error> { |
There was a problem hiding this comment.
This collects diagnostics outside ErrorCollector::collect_sorted, so it skips the ignore-errors-in-generated-code filter applied there. With that option on, a generated file still gets a warning:
# @generated
x: int = 1 # type: ignore1 # WARN invalid-ignore-comment; expected: nothing
Please also return early when malformed() is empty, so the common case does no extra work.
|
|
||
| /// Collects `invalid-ignore-comment` diagnostics for all loaded modules. | ||
| /// Skips files we are told never to report errors on. | ||
| pub fn collect_invalid_ignore_comment_errors(&self) -> Vec<Error> { |
There was a problem hiding this comment.
This entry point skips ErrorStyle::Never modules, but the per-module call in collect_error_refs_with_ignore_diagnostics (around line 432, used by the CLI check and LSP diagnostics) doesn't. Could we have a single per-module function with the filters (error style, generated code) in one place, and have both paths call it?
Also, the other function calls config.enabled_ignores(path) a second time for the same module. That does glob matching, so please reuse the first result.
| if err.error_kind().is_unsuppressable() { | ||
| return SuppressionEffect::None; | ||
| } |
There was a problem hiding this comment.
invalid-ignore-comment errors are synthesized after collection (state/errors.rs:448). They never pass through this function or through Error::suppression_effect, so for the new kind these checks never run. Only the is_suppressable change, which keeps --suppress-errors from writing these comments, has any effect. Item 6 of the summary claims more than that.
Could you drop the checks that don't take effect, or explain what they protect?
| // so the fix is to move or remove the comment rather than to suppress | ||
| // the error. `suppression_effect` already returns `None` for them, but | ||
| // keep the explicit short-circuit so their intent stays local. | ||
| let effect = if error.error_kind().is_unsuppressable() { |
There was a problem hiding this comment.
As the comment says, suppression_effect already returns None for these kinds. Please keep only one of the two checks.
| } | ||
|
|
||
| #[test] | ||
| fn test_invalid_ignore_comment_is_unsuppressable() { |
There was a problem hiding this comment.
This test and test_ignore_comment_lints_survive_file_level_ignore_errors call collect_invalid_ignore_comment_errors(), which applies no suppression at all. They pass even with every is_unsuppressable change reverted. To test what users see, go through the display path, e.g. collect_display_errors_with_ignore_diagnostics().
| self.is_unused_ignore() | ||
| || matches!( | ||
| self, | ||
| ErrorKind::MisplacedIgnore | ErrorKind::InvalidIgnoreComment |
There was a problem hiding this comment.
Adding MisplacedIgnore here changes how misplaced-ignore behaves, which the summary doesn't mention and no test covers:
- A
# pyrefly: ignore[misplaced-ignore]no longer counts as used, so it becomes an unused-ignore under strict. - File-level
ignore-errorsno longer hidesmisplaced-ignore. --suppress-errorsstops writing comments for it.
That may be the right call, but could you split it into its own PR with a test? If you think it's necessary to keep as part of this diff, please let me know here and update the summary
| // suppression. (A trailing identifier char, e.g. `ignore-errorss`, | ||
| // falls through to the typo branch below.) | ||
| ParsedIgnore::NotADirective | ||
| } else { |
There was a problem hiding this comment.
With mypy ignores enabled (permissive-ignores = true), mypy inline config is flagged:
# mypy: ignore-missing-imports # WARN: did you mean `# mypy: ignore`?
Consider not treating a - or _ right after ignore as a typo for tools other than Pyrefly. Also, # mypy: ignore isn't a real mypy per-line form, so that suggestion is misleading.
| } | ||
|
|
||
| /// The lowercase name used in ignore comments, e.g. `pyrefly`. | ||
| pub fn as_str(self) -> &'static str { |
There was a problem hiding this comment.
This copies the from_comment function a few lines above. Tool already derives ValueEnum and kebab-case serde, so could one direction be derived from the other, or both come from a single table?
Summary
Malformed Pyrefly ignore comments were previously silently discarded without suppressing errors or producing a diagnostic. As a result, users had no indication that their ignore directives contained typos or syntax errors.
This PR introduces the
invalid-ignore-commentlint rule (default severity:warn), enabled in thedefault,strict, andallpresets.It detects three malformed directive patterns:
Background and additional changes
This PR builds on the implementation by @mikeleppane in closed PR #3832, splitting the work into two PRs as requested by maintainers.
Additional changes and improvements:
main: Removed stale diffs inbinding/expr.rs.# pyrefly: ignore[,]and# pyrefly: ignore[ , ]are now correctly recognized asMalformedReason::EmptyBracket.# pyre-ignoreand# pyre-fixme.ignore[code1, code2) and whitespace/comma-only lists (ignore[,],ignore[ , ]).vec![""]blanket suppression for non-Pyrefly tools using directives such as# type: ignore[].ErrorKind::is_unsuppressable(), preventing the diagnostic from being hidden by same-line ignore comments, file-level# pyrefly: ignore-errors, or automated--suppress-errorscomment generators.Closes #3752
Test Plan
Unit tests in
crates/pyrefly_python/src/ignore.rs:test_parse_ignore_commenttest_malformed_ignores_are_additive# type: list[int]and# pyrefly: see ticketare not flagged.State tests in
pyrefly/lib/state/errors.rs:test_invalid_ignore_comment_emitted: Verifies the diagnostic is emitted while the underlying type error remains.test_invalid_ignore_comment_variants: Covers unterminated and empty brackets.test_empty_bracket_ignore_is_additive: Verifies that# type: ignore[]retains its existing suppression behavior.test_invalid_ignore_comment_is_unsuppressable: Verifies that same-line ignore comments cannot suppress the lint.test_ignore_comment_lints_survive_file_level_ignore_errors: Verifies that file-level ignore directives cannot suppress the lint.End-to-end tests: Added Scrut coverage in
test/ignores.md.Documentation and preset tests:
test_doc_headers,test_doc_severities, andtest_preset_doc.Validation