PEP 484 specifies that: "A # type: ignore comment on a line by itself is equivalent to adding an inline # type: ignore to each line until the end of the current indented block. At top indentation level this has effect of disabling type checking until the end of file."
As far as I know, no type checker has fully implemented this behavior, and one has implemented a behavior that is directly incompatible with the text. A recent PR to mypy proposed to implement more of this specification and I became concerned that some of the behavior was unintuitive. I'm concerned that people could accidentally leave a "# type: ignore" on a line by itself and be surprised when that disables type checking for the rest of the file.
Here's what existing type checkers do:
- mypy: "# type: ignore" only ever ignores errors on the current line. Recently merged PRs added support for ignoring all errors in the current expression (6648) and for ignoring the whole file if the first line is "# type: ignore" (6830) and a still-open PR adds support in 3.8 only for ignoring errors from a top-level "# type: ignore" to the end of the file (6841).
- pyre: "# type: ignore" (and two other Pyre-specific ignore comments) can either be placed on the same line as the error being suppressed, or on a line by itself preceding the line with the error. The latter behavior directly contradicts the text of PEP 484. A separate comment at the top of the file can suppress all errors from a file. (https://pyre-check.org/docs/error-suppression.html#explicitly-suppressing-errors)
- pytype: In addition to "# type: ignore", supports a pytype-specific suppression comment that can be scoped over multiple lines. Users first disable errors with "# pytype: disable=attribute-error" on a line by itself, then re-enable them with "# pytype: enable=attribute-error", both on a line by itself. (https://github.com/google/pytype/blob/master/docs/user_guide.md#silencing-errors)
- PyCharm: doesn't support "# type: ignore" (https://youtrack.jetbrains.com/issue/PY-19917)
- pyright: doesn't support "# type: ignore" and rejected a feature request to add it (https://github.com/microsoft/pyright/issues/108)
mypy at least also supports theĀ @typing.no_type_check decorator, which suppresses type errors in a whole function and is therefore close in functionality to the block-scoped "# type: ignore" specified by PEP 484.
As a further point of comparison, I looked into how some other code quality tools deal with magic comments:
No type checker has fully implemented the PEP 484 specification and no tool that I know of interprets magical comments in the way PEP 484 envisions (checking is turned off until the end of the file with no way to turn it back on). I've seen no user requests for mypy to enable the full PEP 484 behavior, but people do ask for a way to disable type checking in the whole file.
I propose that we change PEP 484 to only specify that a "# type: ignore" at the very top of the file disables type checking for the whole file. This brings the text in closer alignment with existing type checker behavior and removes the potentially surprising behavior of "# type: ignore" in the middle of the file.