Skip to content

feat: add opt-in ignore-without-code lint - #5751

Closed
Aayushyaash wants to merge 2 commits into
pyrefly-org:mainfrom
Aayushyaash:fix-ignore-without-code
Closed

Aayushyaash wants to merge 2 commits into
pyrefly-org:mainfrom
Aayushyaash:fix-ignore-without-code

Conversation

@Aayushyaash

Copy link
Copy Markdown
Contributor

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.

# 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

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

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

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

@github-actions

Copy link
Copy Markdown

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

Optional recommendations:

  • This PR is on the larger side. If parts of it could be merged on their own, consider splitting it into separate PRs for faster review. 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 24, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D134246495. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

This comment has been minimized.

@Aayushyaash
Aayushyaash force-pushed the fix-ignore-without-code branch from 5ac007d to 06c724f Compare November 24, 2026 14:56
@github-actions github-actions Bot added size/l and removed size/l labels Nov 24, 2026
@github-actions

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on open source code:

============================================================
SUMMARY
============================================================
Total: +5 new errors, -0 fixed errors
By preset: +0/-0 (default), +0/-0 (strict)

Projects with changes (2):
  numpy-stl: +3 -0
  ignite: +2 -0
============================================================

FULL DIFF DETAILS
------------------------------------------------------------

numpy-stl (https://github.com/WoLpH/numpy-stl)
+ ERROR stl/base.py:503:76-93: `# pyrefly: ignore` has no error code; specify the code(s) being suppressed, e.g. `# pyrefly: ignore[bad-return]` [ignore-without-code]
+ ERROR stl/base.py:557:71-88: `# pyrefly: ignore` has no error code; specify the code(s) being suppressed, e.g. `# pyrefly: ignore[bad-return]` [ignore-without-code]
+ ERROR stl/base.py:559:58-75: `# pyrefly: ignore` has no error code; specify the code(s) being suppressed, e.g. `# pyrefly: ignore[bad-return]` [ignore-without-code]

ignite (https://github.com/pytorch/ignite)
+ ERROR ignite/handlers/lr_finder.py:321:30-47: `# pyrefly: ignore` has no error code; specify the code(s) being suppressed, e.g. `# pyrefly: ignore[bad-return]` [ignore-without-code]
+ ERROR ignite/handlers/visdom_logger.py:179:28-45: `# pyrefly: ignore` has no error code; specify the code(s) being suppressed, e.g. `# pyrefly: ignore[bad-return]` [ignore-without-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.

@meta-codesync

meta-codesync Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 0e1f12d.

@Aayushyaash
Aayushyaash deleted the fix-ignore-without-code branch November 24, 2026 19:52
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.

option to report an error on # pyrefly: ignore comments without an error code

2 participants