Skip to content

fix: report malformed ignore comments as invalid-ignore-comment - #5583

Closed
Aayushyaash wants to merge 2 commits into
pyrefly-org:mainfrom
Aayushyaash:fix-invalid-ignore-comment
Closed

Aayushyaash wants to merge 2 commits into
pyrefly-org:mainfrom
Aayushyaash:fix-invalid-ignore-comment

Conversation

@Aayushyaash

Copy link
Copy Markdown
Contributor

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-comment lint rule (default severity: warn), enabled in the default, strict, and all presets.

It detects three malformed directive patterns:

x: int = "oops"  # pyrefly: ignoree             # Typo in directive keyword
x: int = "oops"  # pyrefly: ignore[bad-code     # Unterminated bracket
x: int = "oops"  # pyrefly: ignore[]            # Empty error-code list

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:

  1. Rebased on latest main: Removed stale diffs in binding/expr.rs.
  2. Fixed comma-only bracket lists: Comments such as # pyrefly: ignore[,] and # pyrefly: ignore[ , ] are now correctly recognized as MalformedReason::EmptyBracket.
  3. Aligned Pyre suggestions: Updated suggestions for unrecognized Pyre directives to use # pyre-ignore and # pyre-fixme.
  4. Expanded unit tests: Added coverage for multi-code unterminated brackets (ignore[code1, code2) and whitespace/comma-only lists (ignore[,], ignore[ , ]).
  5. Preserved existing suppression behavior:
    • Typos: Continue to suppress nothing.
    • Unterminated brackets: Retain the existing lenient parsing behavior while emitting a warning.
    • Empty brackets: Preserve vec![""] blanket suppression for non-Pyrefly tools using directives such as # type: ignore[].
  6. Made the lint unsuppressable: Centralized the behavior in ErrorKind::is_unsuppressable(), preventing the diagnostic from being hidden by same-line ignore comments, file-level # pyrefly: ignore-errors, or automated --suppress-errors comment generators.

Closes #3752

  • I am a human, and I fully understand the code I am submitting.

Test Plan

  • Unit tests in crates/pyrefly_python/src/ignore.rs:

    • test_parse_ignore_comment
    • test_malformed_ignores_are_additive
    • Cover typo, single-code and multi-code unterminated brackets, empty brackets, and comma-only lists.
    • Verify that regular comments such as # type: list[int] and # pyrefly: see ticket are 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, and test_preset_doc.

Validation

cargo check
cargo test -p pyrefly_python
cargo test -p pyrefly --lib test_invalid_ignore_comment
cargo test -p pyrefly --lib test_empty_bracket_ignore_is_additive
cargo test -p pyrefly --lib test_ignore_comment_lints_survive_file_level_ignore_errors

@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

✅ This PR meets all requirements from our pull request checklist.

Optional recommendations:

  • This PR is very large, which makes it slow and difficult to review. We strongly recommend splitting it into smaller PRs that can be merged independently. If that isn't possible, please structure it as a series of small commits. If you've already split it as far as is practical, no action is needed.

Note: this comment updates automatically after changes to the PR. Updates may take up to half an hour.

@meta-codesync

meta-codesync Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

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.)

@Aayushyaash

Copy link
Copy Markdown
Contributor Author

Pr targeting #3450 builds on top of this pr. Once this pr is merged, I'll rebase the changes onto main and open the follow up pr.

@github-actions

This comment has been minimized.

@connernilsen connernilsen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread crates/pyrefly_python/src/ignore.rs Resolved
Comment thread pyrefly/lib/state/errors.rs Resolved
Comment thread pyrefly/lib/state/errors.rs Resolved
Comment thread pyrefly/lib/error/collector.rs Resolved
Comment thread pyrefly/lib/error/collector.rs Resolved
Comment thread pyrefly/lib/state/errors.rs Resolved
Comment thread crates/pyrefly_config/src/error_kind.rs Resolved
Comment thread crates/pyrefly_python/src/ignore.rs Resolved
Comment thread crates/pyrefly_python/src/ignore.rs Resolved
@Aayushyaash
Aayushyaash force-pushed the fix-invalid-ignore-comment branch from 2d10614 to a0d70c2 Compare November 22, 2026 09:01
@github-actions github-actions Bot added size/xl and removed size/xl labels Nov 22, 2026
@github-actions

This comment has been minimized.

@Aayushyaash
Aayushyaash force-pushed the fix-invalid-ignore-comment branch from a0d70c2 to 9fe6277 Compare November 22, 2026 09:45
@github-actions github-actions Bot added size/xl and removed size/xl labels Nov 22, 2026
@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@stroxler stroxler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@stroxler

Copy link
Copy Markdown
Contributor

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.

@meta-codesync meta-codesync Bot closed this in 25a2470 Nov 24, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 24, 2026
@meta-codesync

meta-codesync Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 25a2470.

@Aayushyaash

Copy link
Copy Markdown
Contributor Author

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 main now and get that ready.

@Aayushyaash
Aayushyaash deleted the fix-invalid-ignore-comment branch November 24, 2026 12:54
meta-codesync Bot pushed a commit that referenced this pull request Nov 24, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new rule - invalid ignore comment

4 participants