ty icon indicating copy to clipboard operation
ty copied to clipboard

unused suppression for `type: ignore`

Open MichaReiser opened this issue 9 months ago • 1 comments

https://github.com/astral-sh/ruff/pull/17305 revealed an interesting case where Red Knot raised an unused suppression diagnostic when the user intended to suppress it:

text_encoding = (
    io.text_encoding  # type: ignore[unused-ignore, attr-defined]
    if sys.version_info > (3, 10)
    else _text_encoding
)

Source: https://github.com/jaraco/zipp/blob/main/zipp/compat/py310.py

type: ignore currently acts as a wildcard suppression (we disregard anything coming in [...]) and suppresses anything except unused suppression issues. The result is that users aren't able to suppress unused suppression diagnostics with type: ignore and are forced to use a red knot specific suppression instead.

Different options are:

  • Change type: ignore to suppress all diagnostics (including unused suppression)
  • Accept that users have to use a red knot specific suppression for unused suppression diagnostics (this is what Ruff does)
  • Add support for type: ignore[unused-ignore]
  • ..?

MichaReiser avatar Apr 10 '25 16:04 MichaReiser

I think asking users to use ty: ignore[unused-ignore-comment] seems reasonable.

An alternative to this is to introduce a separate rule for unused type: ignore comments, which a user could disable.

MichaReiser avatar Dec 24 '25 14:12 MichaReiser