Skip to content

Feature: --remove-unused-type-ignores flag to clean up unused # type: ignore comments #3984

Description

@tobyh-canva

Is your feature request related to a problem?

pyrefly suppress --remove-unused (and pyrefly check --remove-unused-ignores) removes unused # pyrefly: ignore comments, but it does not touch unused # type: ignore comments, even though Pyrefly honors # type: ignore as a valid suppression.

This means that after I clean up my suppressions, any stale # type: ignore comments are left behind and I have to find and delete them by hand. For a large codebase that adopted # type: ignore (for example, coming from another type checker), there is no convenient way to clean these up with Pyrefly.

I understand the default likely excludes # type: ignore on purpose, since those comments are shared with other type checkers and removing them could affect another tool. So I am not asking to change the default behavior.

Describe the solution you'd like

Add an opt-in flag that also removes unused # type: ignore comments:

  • pyrefly suppress --remove-unused-type-ignores
  • pyrefly check --remove-unused-type-ignores

This would work like the existing --remove-unused / --remove-unused-ignores, but additionally clean up # type: ignore comments that are not suppressing anything. Users who share # type: ignore with other tools simply would not pass the flag, so the default stays safe.

Reproduction of current behavior

Given a file where every suppression is unused (no error on the line):

a = 1  # pyrefly: ignore
b = 2  # type: ignore

Running pyrefly suppress --remove-unused removes only the first:

a = 1
b = 2  # type: ignore

There is currently no flag that would also remove the unused # type: ignore.

Additional context

The documentation for --remove-unused describes it only in terms of # pyrefly: ignore comments and does not mention # type: ignore handling, so it would be worth documenting the new flag and the default behavior together.

Related

Metadata

Metadata

Assignees

Labels

No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions