Skip to content

Add unused-type-ignore rule#3784

Open
aodihis wants to merge 4 commits into
facebook:mainfrom
aodihis:feat/3766
Open

Add unused-type-ignore rule#3784
aodihis wants to merge 4 commits into
facebook:mainfrom
aodihis:feat/3766

Conversation

@aodihis

@aodihis aodihis commented Jun 12, 2026

Copy link
Copy Markdown

Summary

Adds a new unused-type-ignore rule that reports # type: ignore comments that are not suppressing any error.

Fixes #3766

This rule is off by default to avoid breaking projects using multiple type checkers (e.g. mypy + pyrefly). Enable it in pyrefly.toml:

[errors]
unused-type-ignore = "error"  # or "warn"

Test Plan

  • Unit tests for unused/used # type: ignore detection
  • LSP integration tests verifying the diagnostic appears when enabled and is silent by default
  • Manually verified that the diagnostic shows when the rule is set to "error" and is silent when set to "ignore

aodihis added 3 commits June 12, 2026 10:45
* Projects using multiple type checkers put `# type: ignore` for mypy without
  wanting pyrefly to flag them, so this rule is a separate opt-in (off by
  default) distinct from the existing `unused-ignore` rule which covers
  `# pyrefly: ignore` comments
* Added `UnusedTypeIgnore` error kind with `Severity::Ignore` default; extended
  `collect_unused_ignore_errors` to detect `Tool::Type` suppressions that cover
  no pyrefly errors on their line, treating any mypy-style codes as blanket
  since they are not pyrefly codes
* Fixed `collect_unused_ignore_errors_for_display` to look up severity by each
  error's own kind rather than hardcoding `UnusedIgnore`, so the two rules can
  be configured independently
* Added three unit tests and documented the new rule in error-kinds.mdx
* Mirrors the existing unused-ignore LSP test pattern with two tests:
  one verifying the diagnostic appears when the rule is enabled in config,
  and one verifying it is silent by default (off-by-default behavior)
Comment thread pyrefly/lib/state/errors.rs Outdated
continue;
}

// For Tool::Type (`# type: ignore`), codes are mypy codes (not pyrefly

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.

codes are mypy codes

I'm not sure this is true, I don't think type: ignore as written in the typing spec has official codes
that said, if someone does use it with a code we proabbly can't shouldn't assume we understand it

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I see, sure, I will remove these unnecessary comments.

@yangdanny97

Copy link
Copy Markdown
Contributor

I would have thought we could use simpler tests for this, but I checked and the other unused-ignore is tested the same way. I think this is fine for now.

This can probably be merged after some editing of the comments @NathanTempest

* Replace inaccurate "mypy codes" description with a simpler statement
  of the actual behavior
@github-actions github-actions Bot added size/l and removed size/l labels Jun 12, 2026
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 - unused # type: ignore comment

3 participants