"# type: ignore" to ignore more than one line
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 <https://github.com/python/mypy/pull/6648>) and for ignoring the whole file if the first line is "# type: ignore" (6830 <https://github.com/python/mypy/pull/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 <https://github.com/python/mypy/pull/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-er... ) - *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-er... ) - *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: - *flake8*: magical comment ignores errors on current line; separate comment ignores the whole file ( http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html) - *pylint*: magical comment ignores errors on the current line or from the beginning of a block to the end ( http://pylint.pycqa.org/en/stable/user_guide/message-control.html) - *Black*: magical comment turns off formatting, and a separate comment turns it back on (https://github.com/python/black#the-black-code-style) - *coverage*: magical comment excludes the current line, or the line and all the lines in the rest of the clause (e.g., the body of an if statement) (https://coverage.readthedocs.io/en/coverage-4.3.4/excluding.html) 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.
+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 <https://github.com/python/mypy/pull/6648>) and for ignoring the whole file if the first line is "# type: ignore" ( 6830 <https://github.com/python/mypy/pull/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 <https://github.com/python/mypy/pull/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-er... ) - *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-er... ) - *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:
- *flake8*: magical comment ignores errors on current line; separate comment ignores the whole file ( http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html) - *pylint*: magical comment ignores errors on the current line or from the beginning of a block to the end ( http://pylint.pycqa.org/en/stable/user_guide/message-control.html) - *Black*: magical comment turns off formatting, and a separate comment turns it back on ( https://github.com/python/black#the-black-code-style) - *coverage*: magical comment excludes the current line, or the line and all the lines in the rest of the clause (e.g., the body of an if statement) ( https://coverage.readthedocs.io/en/coverage-4.3.4/excluding.html)
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/
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 <https://github.com/python/mypy/pull/6648>) and for ignoring the whole file if the first line is "# type: ignore" ( 6830 <https://github.com/python/mypy/pull/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 <https://github.com/python/mypy/pull/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-er... ) - *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-er... ) - *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:
- *flake8*: magical comment ignores errors on current line; separate comment ignores the whole file ( http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html) - *pylint*: magical comment ignores errors on the current line or from the beginning of a block to the end ( http://pylint.pycqa.org/en/stable/user_guide/message-control.html) - *Black*: magical comment turns off formatting, and a separate comment turns it back on ( https://github.com/python/black#the-black-code-style) - *coverage*: magical comment excludes the current line, or the line and all the lines in the rest of the clause (e.g., the body of an if statement) ( https://coverage.readthedocs.io/en/coverage-4.3.4/excluding.html)
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/
I support Jelle's proposal. Let's only support `# type: ignore` on a line by itself at the top of the file. To answer Rebecca's question, I propose to leave the decision up to individual type checkers whether to report stray `# type: ignore` lines. For example, mypy has a flag for this (`--warn-unused-ignores`). Jelle noted that mypy has relaxed its behavior of `# type: ignore` lines pertaining to multi-line statements. Perhaps we can update PEP 484 to mention that behavior as well? It would make placing `# type: ignore` lines on long lines more flexible. Jelle, do you want to draft a PR to update PEP 484? 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 <https://github.com/python/mypy/pull/6648>) and for ignoring the whole file if the first line is "# type: ignore" ( 6830 <https://github.com/python/mypy/pull/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 <https://github.com/python/mypy/pull/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-er... ) - *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-er... ) - *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:
- *flake8*: magical comment ignores errors on current line; separate comment ignores the whole file ( http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html) - *pylint*: magical comment ignores errors on the current line or from the beginning of a block to the end ( http://pylint.pycqa.org/en/stable/user_guide/message-control.html) - *Black*: magical comment turns off formatting, and a separate comment turns it back on ( https://github.com/python/black#the-black-code-style) - *coverage*: magical comment excludes the current line, or the line and all the lines in the rest of the clause (e.g., the body of an if statement) ( https://coverage.readthedocs.io/en/coverage-4.3.4/excluding.html)
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/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him/his **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
El lun., 20 may. 2019 a las 12:39, Guido van Rossum (<guido@python.org>) escribió:
I support Jelle's proposal. Let's only support `# type: ignore` on a line by itself at the top of the file.
To answer Rebecca's question, I propose to leave the decision up to individual type checkers whether to report stray `# type: ignore` lines. For example, mypy has a flag for this (`--warn-unused-ignores`).
I agree.
Jelle noted that mypy has relaxed its behavior of `# type: ignore` lines pertaining to multi-line statements. Perhaps we can update PEP 484 to mention that behavior as well? It would make placing `# type: ignore` lines on long lines more flexible.
mypy only does this in 3.8+, because the feature relies on new changes to the AST in 3.8. It seems premature to me to specify this behavior in the PEP and expect all type checkers to support it.
Jelle, do you want to draft a PR to update PEP 484?
https://github.com/python/peps/pull/1072
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 <https://github.com/python/mypy/pull/6648>) and for ignoring the whole file if the first line is "# type: ignore" ( 6830 <https://github.com/python/mypy/pull/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 <https://github.com/python/mypy/pull/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-er... ) - *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-er... ) - *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:
- *flake8*: magical comment ignores errors on current line; separate comment ignores the whole file ( http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html) - *pylint*: magical comment ignores errors on the current line or from the beginning of a block to the end ( http://pylint.pycqa.org/en/stable/user_guide/message-control.html) - *Black*: magical comment turns off formatting, and a separate comment turns it back on ( https://github.com/python/black#the-black-code-style) - *coverage*: magical comment excludes the current line, or the line and all the lines in the rest of the clause (e.g., the body of an if statement) ( https://coverage.readthedocs.io/en/coverage-4.3.4/excluding.html)
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/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him/his **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
I feel worth pointing out that top of the file does not mean first non-empty line (even with a small example), for example, the encoding header will probably need to be before this disable ignore the comment. Perhaps we should also ignore all other comments before the first of instance of any of the trio: docstring/import/expression. Note both docstring and import are optional, so the current definition leaves it vague what is acceptable when neither is present. On Wed, May 22, 2019 at 4:45 AM Jelle Zijlstra <jelle.zijlstra@gmail.com> wrote:
El lun., 20 may. 2019 a las 12:39, Guido van Rossum (<guido@python.org>) escribió:
I support Jelle's proposal. Let's only support `# type: ignore` on a line by itself at the top of the file.
To answer Rebecca's question, I propose to leave the decision up to individual type checkers whether to report stray `# type: ignore` lines. For example, mypy has a flag for this (`--warn-unused-ignores`).
I agree.
Jelle noted that mypy has relaxed its behavior of `# type: ignore` lines pertaining to multi-line statements. Perhaps we can update PEP 484 to mention that behavior as well? It would make placing `# type: ignore` lines on long lines more flexible.
mypy only does this in 3.8+, because the feature relies on new changes to the AST in 3.8. It seems premature to me to specify this behavior in the PEP and expect all type checkers to support it.
Jelle, do you want to draft a PR to update PEP 484?
https://github.com/python/peps/pull/1072
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 <https://github.com/python/mypy/pull/6648>) and for ignoring the whole file if the first line is "# type: ignore" ( 6830 <https://github.com/python/mypy/pull/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 <https://github.com/python/mypy/pull/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-er... ) - *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-er... ) - *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:
- *flake8*: magical comment ignores errors on current line; separate comment ignores the whole file ( http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html) - *pylint*: magical comment ignores errors on the current line or from the beginning of a block to the end ( http://pylint.pycqa.org/en/stable/user_guide/message-control.html) - *Black*: magical comment turns off formatting, and a separate comment turns it back on ( https://github.com/python/black#the-black-code-style) - *coverage*: magical comment excludes the current line, or the line and all the lines in the rest of the clause (e.g., the body of an if statement) ( https://coverage.readthedocs.io/en/coverage-4.3.4/excluding.html)
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/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him/his **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________ 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/
Thanks Bernat! I clarified my proposed PEP change in https://github.com/python/peps/pull/1072/commits/91bb4c229545fc956c1209bdc97... . El mié., 22 may. 2019 a las 2:26, Bernat Gabor (<gaborjbernat@gmail.com>) escribió:
I feel worth pointing out that top of the file does not mean first non-empty line (even with a small example), for example, the encoding header will probably need to be before this disable ignore the comment. Perhaps we should also ignore all other comments before the first of instance of any of the trio: docstring/import/expression. Note both docstring and import are optional, so the current definition leaves it vague what is acceptable when neither is present.
On Wed, May 22, 2019 at 4:45 AM Jelle Zijlstra <jelle.zijlstra@gmail.com> wrote:
El lun., 20 may. 2019 a las 12:39, Guido van Rossum (<guido@python.org>) escribió:
I support Jelle's proposal. Let's only support `# type: ignore` on a line by itself at the top of the file.
To answer Rebecca's question, I propose to leave the decision up to individual type checkers whether to report stray `# type: ignore` lines. For example, mypy has a flag for this (`--warn-unused-ignores`).
I agree.
Jelle noted that mypy has relaxed its behavior of `# type: ignore` lines pertaining to multi-line statements. Perhaps we can update PEP 484 to mention that behavior as well? It would make placing `# type: ignore` lines on long lines more flexible.
mypy only does this in 3.8+, because the feature relies on new changes to the AST in 3.8. It seems premature to me to specify this behavior in the PEP and expect all type checkers to support it.
Jelle, do you want to draft a PR to update PEP 484?
https://github.com/python/peps/pull/1072
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 <https://github.com/python/mypy/pull/6648>) and for ignoring the whole file if the first line is "# type: ignore" ( 6830 <https://github.com/python/mypy/pull/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 <https://github.com/python/mypy/pull/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-er... ) - *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-er... ) - *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:
- *flake8*: magical comment ignores errors on current line; separate comment ignores the whole file ( http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html) - *pylint*: magical comment ignores errors on the current line or from the beginning of a block to the end ( http://pylint.pycqa.org/en/stable/user_guide/message-control.html) - *Black*: magical comment turns off formatting, and a separate comment turns it back on ( https://github.com/python/black#the-black-code-style) - *coverage*: magical comment excludes the current line, or the line and all the lines in the rest of the clause (e.g., the body of an if statement) ( https://coverage.readthedocs.io/en/coverage-4.3.4/excluding.html)
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/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him/his **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________ 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/
Okay, PEP 484 update has been merged. Per Jelle's advice I'll leave the extended syntax for `# type: ignore` for another time. On Wed, May 22, 2019 at 5:49 AM Jelle Zijlstra <jelle.zijlstra@gmail.com> wrote:
Thanks Bernat! I clarified my proposed PEP change in https://github.com/python/peps/pull/1072/commits/91bb4c229545fc956c1209bdc97... .
El mié., 22 may. 2019 a las 2:26, Bernat Gabor (<gaborjbernat@gmail.com>) escribió:
I feel worth pointing out that top of the file does not mean first non-empty line (even with a small example), for example, the encoding header will probably need to be before this disable ignore the comment. Perhaps we should also ignore all other comments before the first of instance of any of the trio: docstring/import/expression. Note both docstring and import are optional, so the current definition leaves it vague what is acceptable when neither is present.
On Wed, May 22, 2019 at 4:45 AM Jelle Zijlstra <jelle.zijlstra@gmail.com> wrote:
El lun., 20 may. 2019 a las 12:39, Guido van Rossum (<guido@python.org>) escribió:
I support Jelle's proposal. Let's only support `# type: ignore` on a line by itself at the top of the file.
To answer Rebecca's question, I propose to leave the decision up to individual type checkers whether to report stray `# type: ignore` lines. For example, mypy has a flag for this (`--warn-unused-ignores`).
I agree.
Jelle noted that mypy has relaxed its behavior of `# type: ignore` lines pertaining to multi-line statements. Perhaps we can update PEP 484 to mention that behavior as well? It would make placing `# type: ignore` lines on long lines more flexible.
mypy only does this in 3.8+, because the feature relies on new changes to the AST in 3.8. It seems premature to me to specify this behavior in the PEP and expect all type checkers to support it.
Jelle, do you want to draft a PR to update PEP 484?
https://github.com/python/peps/pull/1072
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 <https://github.com/python/mypy/pull/6648>) and for ignoring the whole file if the first line is "# type: ignore" (6830 <https://github.com/python/mypy/pull/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 <https://github.com/python/mypy/pull/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-er... ) - *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-er... ) - *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:
- *flake8*: magical comment ignores errors on current line; separate comment ignores the whole file ( http://flake8.pycqa.org/en/3.1.1/user/ignoring-errors.html) - *pylint*: magical comment ignores errors on the current line or from the beginning of a block to the end ( http://pylint.pycqa.org/en/stable/user_guide/message-control.html) - *Black*: magical comment turns off formatting, and a separate comment turns it back on ( https://github.com/python/black#the-black-code-style) - *coverage*: magical comment excludes the current line, or the line and all the lines in the rest of the clause (e.g., the body of an if statement) ( https://coverage.readthedocs.io/en/coverage-4.3.4/excluding.html)
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/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him/his **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________ 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/
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him/his **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
Teilnehmer (5)
-
Bernat Gabor
-
Brandt Bucher
-
Guido van Rossum
-
Jelle Zijlstra
-
Rebecca Chen