Skip to content

Add opt-in --remove-unused-type-ignores flag#3990

Open
nitishagar wants to merge 1 commit into
facebook:mainfrom
nitishagar:nitishagar/issue-3984-remove-unused-type-ignores
Open

Add opt-in --remove-unused-type-ignores flag#3990
nitishagar wants to merge 1 commit into
facebook:mainfrom
nitishagar:nitishagar/issue-3984-remove-unused-type-ignores

Conversation

@nitishagar

Copy link
Copy Markdown
Contributor

Summary

pyrefly suppress --remove-unused and pyrefly check --remove-unused-ignores remove unused # pyrefly: ignore comments, but they leave unused # type: ignore comments behind — even though Pyrefly honors # type: ignore and already detects when one is unused. After cleaning up suppressions, any stale # type: ignore comments have to be found and deleted by hand, which is painful in codebases that adopted # type: ignore (e.g. coming from another type checker).

This adds an opt-in --remove-unused-type-ignores flag (on both check and suppress) that additionally removes unused # type: ignore comments, working just like the existing --remove-unused / --remove-unused-ignores flags. It is off by default, so # type: ignore comments — which are shared with other type checkers — are never touched unless the flag is explicitly passed.

The change threads a remove_type_ignores boolean into the existing removal path and adds a single gated arm that handles the UnusedTypeIgnore errors Pyrefly already produces; unused detection is unchanged.

$ pyrefly check --remove-unused-ignores --remove-unused-type-ignores repro.py
# removes both "# pyrefly: ignore" and unused "# type: ignore"
$ pyrefly check --remove-unused-ignores repro.py
# removes only "# pyrefly: ignore" (default, unchanged)

Fixes #3984.

Test plan

  • cargo build -p pyrefly
  • cargo test -p pyrefly remove_unused — 23 passed, including new test_remove_unused_type_ignore_inline / test_remove_unused_type_ignore_with_code and a regression test asserting the comment is kept when the flag is absent.
  • cargo clippy -p pyrefly --tests
  • Manual repro from the issue: with the flag both comments are removed; without it only the pyrefly one is removed.
Summary: suppress --remove-unused and check --remove-unused-ignores remove unused # pyrefly: ignore comments but leave unused # type: ignore comments behind, even though Pyrefly already detects them as unused. Add an opt-in --remove-unused-type-ignores flag (on both check and suppress) that additionally removes unused # type: ignore comments. It is off by default, so # type: ignore comments (which are shared with other type checkers) are never removed unless explicitly requested. The flag threads a remove_type_ignores boolean into the existing removal path and adds a gated arm to the message-prefix decision that handles the UnusedTypeIgnore errors Pyrefly already produces.

Fixes facebook#3984

Test Plan: cargo build -p pyrefly; cargo test -p pyrefly remove_unused (23 passed); cargo clippy -p pyrefly --tests; manual repro from the issue - with the flag both # pyrefly: ignore and unused # type: ignore are removed, without the flag only the pyrefly one is removed.
@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. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant