Whether or not the behavior is kept in the PEP, I think that these two sentences would benefit from a rewording. Here's my comment from the recent PR:

In my opinion, the above wording is a bit too general. For one, it technically allows placing these ignores in weird places, like blank lines in the middle of expressions, or between a decorator and a function/class def. Taken literally, it also makes `--warn-unused-ignores` behavior super annoying, since *every single line* in the block now has a virtual `# type: ignore` comment.

On Thu, May 16, 2019 at 10:26 AM Rebecca Chen <rechen@google.com> wrote:
+1 to this proposal. pytype does actually implement the full current PEP 484 behavior, but we were concerned enough about it being unintuitive that we added a special late-directive warning (https://google.github.io/pytype/errors.html#late-directive) to inform users that they are disabling for the rest of the file.

One question I'd have is - how will # type: ignore on its own line in the middle of a file be interpreted? Wil it be an error? Undefined behavior?

From: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Date: Thu, May 16, 2019 at 8:11 AM
To: <typing-sig@python.org>, Guido van Rossum, <brandtbucher@gmail.com>

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.


_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/