Repository navigation
fix: report malformed ignore comments as invalid-ignore-comment - #5583
Aayushyaash wants to merge 2 commits into
Conversation
|
✅ 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 |
This comment has been minimized.
This comment has been minimized.
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
Co-authored-by: Mikko Leppänen <[email protected]>
2d10614 to
a0d70c2
Compare
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Mikko Leppänen <[email protected]>
a0d70c2 to
9fe6277
Compare
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
stroxler
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
Thanks @Aayushyaash for the work! There are still some significant improvements possible here. The biggest one is to reuse the existing diagnostic lifecycle to minimize duplication of logic. In the process, we can add tests and fix a few behaviors. You can see the commits stacked on top of the merge (once it is finished) if you're curious. I decided to go ahead and merge with fix-forward commits because we're excited to start on #3450, which has been waiting for this change. Once this merges (along with follow-up commits that make some significant refactors), you'll need to rebase your follow-up onto it. |
|
This pull request has been merged in 25a2470. |
|
Thanks @stroxler! Seeing the latest commits makes total sense, collecting these as syntax errors during the parse phase is so much cleaner than the post-check collation I tried to salvage from the original stale PR, which ended up duplicating existing logic. Good lesson for me on not just salvaging and splitting an older PR's plumbing as is, but stepping back to look at the overall architecture/codebase first. I'll rebase the #3450 follow-up onto the new |
Summary: A bare `# pyrefly: ignore` comment suppresses all errors on its line. In growing codebases, this can silently hide unrelated errors introduced during future refactoring. This PR introduces the `ignore-without-code` lint rule (default severity: `ignore`, opt-in, enabled in the `all` preset). It provides a Pyrefly equivalent of basedpyright's `reportIgnoreCommentWithoutRule` and mypy's `ignore-without-code`. When enabled, every `# pyrefly: ignore` directive must explicitly specify the error code(s) it suppresses. ```python # Flagged when enabled: x: int = "oops" # pyrefly: ignore # Fix: x: int = "oops" # pyrefly: ignore[bad-assignment] ``` This PR completes the second part of closed PR #3832 by mikeleppane, splitting it into an independent PR as requested by maintainers following the merge of PR #5583. Closes #3450 - [x] I am a human, and I fully understand the code I am submitting. Pull Request resolved: #5751 Test Plan: - **Unit tests** in `pyrefly/lib/state/errors.rs`: - `test_ignore_without_code_disabled_by_default`: Verifies the rule is disabled by default. - `test_ignore_without_code_emitted`: Verifies bare `# pyrefly: ignore` is flagged while coded directives like `# pyrefly: ignore[bad-assignment]` are not. - `test_ignore_without_code_scoped_to_pyrefly`: Verifies bare `# type: ignore`, `# pyright: ignore`, and `# pyre: ignore` are not flagged. - `test_ignore_without_code_survives_file_level_ignore_errors`: Verifies file-level `# pyrefly: ignore-errors` cannot suppress this lint. - **End-to-end Scrut tests** in `test/ignores.md`: - Verified silent by default (`[0]`). - Verified flagged when enabled via `errors = {ignore-without-code = true}` (`[1]`). - **Preset and documentation tests**: - `test_error_presets_json` passes (registered only in the `all` preset). - `test_doc_headers` passes. - Documentation and severity verified in `website/docs/error-kinds.mdx` and `website/docs/migrate/mypy/error-codes.mdx`. ### Validation ```bash cargo check cargo test -p pyrefly test_ignore_without_code cargo test -p pyrefly_config test_error_presets_json test_doc_headers python test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema --no-extension ``` Reviewed By: stroxler Differential Revision: D134246495 fbshipit-source-id: 8f798d56b9a6782193a64517d96a9fc25c9a72c1
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