Skip to content

feat(ignore): add invalid-ignore-comment and ignore-without-code rules - #3832

Closed
mikeleppane wants to merge 1 commit into
pyrefly-org:mainfrom
mikeleppane:feat/ignore-comment-lints-3752-3450
Closed

mikeleppane wants to merge 1 commit into
pyrefly-org:mainfrom
mikeleppane:feat/ignore-comment-lints-3752-3450

Conversation

@mikeleppane

@mikeleppane mikeleppane commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes /#3752
Closes /#3450

Two new ignore-comment lint rules, mirroring ty's
invalid-ignore-comment
and basedpyright's
reportIgnoreCommentWithoutRule.

What & why

invalid-ignore-comment (default warn). A comment that looks like an
ignore directive but is malformed used to be silently discarded: it produced
neither a suppression nor any warning, so you never learned it was wrong. Three
shapes are now flagged:

x: int = "oops"  # pyrefly: ignoree              # typo'd keyword
x: int = "oops"  # pyrefly: ignore[bad-assignment # unterminated bracket
x: int = "oops"  # pyrefly: ignore[]             # empty code list

ignore-without-code (default ignore, opt-in). A bare # pyrefly: ignore
suppresses any error on its line, so it can silently swallow a later, unrelated
error. Enable the rule to require an explicit code:

# flagged when enabled:
x: int = "oops"  # pyrefly: ignore
# fix:
x: int = "oops"  # pyrefly: ignore[bad-assignment]

Key design property: purely additive

invalid-ignore-comment changes no suppression behavior. The ignore-comment
parser now returns a single recognizer result with three disjoint outcomes
(Valid, Malformed { suppression, .. }, NotADirective), so exactly one fires
per comment, and a non-directive such as # type: list[int] (which never reaches
the ignore stem) can never be flagged. A Malformed result still carries
the exact suppression it produced before, so the diagnostic is layered on top:

  • typo: suppresses nothing, as before
  • unterminated [: keeps the lenient parse, still suppresses
  • empty []: still suppresses for tools whose codes aren't checked
    (# type: ignore[] remains a blanket suppression), a no-op for Pyrefly

How

  • crates/pyrefly_python/src/ignore.rs: parse_ignore_comment returns the
    new ParsedIgnore enum; Ignore records a Vec<MalformedIgnore> alongside
    its suppression map. ignore-errors / ignore-all-errors are excluded via a
    word-boundary check so genuine file-level directives aren't flagged.
  • crates/pyrefly_config/src/error_kind.rs: two new ErrorKinds in
    lexicographic position (InvalidIgnoreComment = warn, IgnoreWithoutCode =
    ignore) plus ErrorKind::is_unsuppressable(), the shared predicate for the
    three ignore-comment diagnostics.
  • pyrefly/lib/state/errors.rs: collect_ignore_comment_lint_errors()
    emits both diagnostics (gated on the tool being enabled), folded into the
    ignore-diagnostics display path so configured severity is respected.
  • Unsuppressable enforcement: centralized in error/collector.rs so the
    ignore-all, per-line, and f-string paths are covered uniformly; also excluded
    from --suppress-errors so it can't write a comment that could never take
    effect.
  • Docs: website/docs/error-kinds.mdx gains both rule sections.

Presets are unchanged: all promotes both automatically; strict is left as-is
to keep ignore-without-code opt-in.

Test plan

Test Protects
ignore::tests::test_parse_ignore_comment recognizer: all three malformed shapes plus valid/non-directive negatives
ignore::tests::test_malformed_ignores_are_additive malformed comments still suppress exactly as before
state::errors::tests::test_invalid_ignore_comment_{emitted,variants} diagnostic emitted for typo / unterminated / empty; real error still fires
state::errors::tests::test_empty_bracket_ignore_is_additive # type: ignore[] still suppresses (no regression for non-Pyrefly tools)
state::errors::tests::test_ignore_without_code_{emitted,scoped_to_pyrefly} bare ignore flagged; # type: ignore not (Pyrefly-scoped)
test/ignores.md (scrut) end-to-end: warn-on-by-default and additive, opt-in behavior, never-flag cases
  • Full lib suite: 5641 pass / 0 fail; pyrefly_config 219; clippy and fmt clean.
  • mypy_primer over 56 real-world projects (mypy, sphinx, scrapy, pydantic,
    cryptography, and more): zero diagnostic diff, confirming no existing
    diagnostic changes on real code.

Malformed ignore comments were silently discarded: a typo'd directive
(`# pyrefly: ignoree`), an unterminated `[`, or an empty `[]` produced
neither a suppression nor any warning, so a developer never learned the
comment was wrong. Separately, a bare `# pyrefly: ignore` suppresses any
error on its line, which can silently swallow a later, unrelated error.

Turn the ignore-comment parser into a single recognizer with three
disjoint outcomes (valid / malformed / not-a-directive) so exactly one
fires and a non-directive such as `# type: list[int]`, which never
reaches the `ignore` stem, can never be flagged. A malformed result
still carries the suppression it produced before, so the new
invalid-ignore-comment diagnostic (default `warn`) is purely additive
and no existing suppression behavior changes. ignore-without-code is
opt-in (default off) and scoped to the Pyrefly tool, the only one whose
per-line codes Pyrefly honors. Both kinds are unsuppressable, enforced
centrally so an ignore comment cannot silence the lint about itself.

Refs: pyrefly-org#3752, pyrefly-org#3450
@meta-cla meta-cla Bot added the cla signed label Jun 16, 2026
meta-codesync Bot pushed a commit that referenced this pull request Jul 8, 2026
Summary:
- Support `# pyrefly: ignore-errors[code]` as a file-level suppression for selected Pyrefly error codes.
- Reuse the existing `Suppression` representation for file-level directives so whole-file and line-level Pyrefly code matching share the same semantics, including parent suppression names.
- Keep non-Pyrefly `ignore-errors` directives as blanket-only and document the new Pyrefly form.

Fixes #3723

Import note (maintainer changes on top of the PR, addressing Devmate review nits):
- Dedupe the `[code, code]` parse into a shared `parse_error_codes` helper used by both the line-level and file-level parsers.
- Reject malformed file-level directives with a missing closing `]` (previously silently accepted as valid).
- Drop empty entries so `# pyrefly: ignore-errors[]` (and trailing commas) act as a blanket ignore rather than a directive matching nothing.
- Update the `parse_ignore_all` docstring to reflect its `Vec<Suppression>` return shape.

## Notes
This is scoped to file-level code filtering for `pyrefly: ignore-errors`. Related suppression lint/reporting PRs (#3832 and #3797) address separate diagnostics and unused-ignore behavior.

Pull Request resolved: #3944

Test Plan: buck test pyrefly:pyrefly_library -- test_pyrefly_top_level_ignore (6 pass); buck test pyrefly_python -- test_parse_ignore (4 pass, incl. empty-bracket + missing-bracket cases).

Reviewed By: kinto0

Differential Revision: D110063592

Pulled By: NathanTempest

fbshipit-source-id: 734d71d546e8d39f696b31af3eaeb0a53d6f094f
@yangdanny97 yangdanny97 self-assigned this Jul 21, 2026
@yangdanny97

Copy link
Copy Markdown
Collaborator

It would be better to fix these separately since that's easier to review, but since it's done already I'll try to review it all together

@meta-codesync

meta-codesync Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

@yangdanny97 has imported this pull request. If you are a Meta employee, you can view this in D113082971.

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity for more than 2 weeks.

If you are still working on this this pull request, please add a comment or push new commits to keep it active. Otherwise, please unassign yourself and allow someone else to take over.

Thank you for your contributions!

@github-actions github-actions Bot added the stale label Aug 5, 2026
avokin2 pushed a commit to avokin2/pyrefly that referenced this pull request Aug 12, 2026
Summary:
- Support `# pyrefly: ignore-errors[code]` as a file-level suppression for selected Pyrefly error codes.
- Reuse the existing `Suppression` representation for file-level directives so whole-file and line-level Pyrefly code matching share the same semantics, including parent suppression names.
- Keep non-Pyrefly `ignore-errors` directives as blanket-only and document the new Pyrefly form.

Fixes pyrefly-org#3723

Import note (maintainer changes on top of the PR, addressing Devmate review nits):
- Dedupe the `[code, code]` parse into a shared `parse_error_codes` helper used by both the line-level and file-level parsers.
- Reject malformed file-level directives with a missing closing `]` (previously silently accepted as valid).
- Drop empty entries so `# pyrefly: ignore-errors[]` (and trailing commas) act as a blanket ignore rather than a directive matching nothing.
- Update the `parse_ignore_all` docstring to reflect its `Vec<Suppression>` return shape.

## Notes
This is scoped to file-level code filtering for `pyrefly: ignore-errors`. Related suppression lint/reporting PRs (pyrefly-org#3832 and pyrefly-org#3797) address separate diagnostics and unused-ignore behavior.

Pull Request resolved: pyrefly-org#3944

Test Plan: buck test pyrefly:pyrefly_library -- test_pyrefly_top_level_ignore (6 pass); buck test pyrefly_python -- test_parse_ignore (4 pass, incl. empty-bracket + missing-bracket cases).

Reviewed By: kinto0

Differential Revision: D110063592

Pulled By: NathanTempest

fbshipit-source-id: 734d71d546e8d39f696b31af3eaeb0a53d6f094f
@yangdanny97 yangdanny97 removed their assignment Aug 13, 2026
@connernilsen

connernilsen commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Hey @mikeleppane! I'm hoping to get this merged in soon, so will be ready to review soon. It looks like there are some merge conflicts (totally understandable without attention from us, we've been swamped with PR reviews).

If you're still interested in working on this, would you be able to let me know, then rebase and resolve those issues? If I don't hear anything back by next week, I'll assume you've moved on and will close this PR so others can work on it. Just a hello and quick status update is all I need, and then we can finish this up!

Also, like @yangdanny97 requested, could we split this into two separate PRs, one for each new error you're adding?

Thanks!

@connernilsen

Copy link
Copy Markdown
Contributor

Closing due to inactivity

meta-codesync Bot pushed a commit that referenced this pull request Nov 24, 2026
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:

```python
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

- [x] I am a human, and I fully understand the code I am submitting.

Pull Request resolved: #5583

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

```bash
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
```

Reviewed By: stroxler

Differential Revision: D130518155

fbshipit-source-id: 918a3222256ac93fd4ef0b9ce45bac3776ee11d0
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.

4 participants