-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Placement of # type: ignore on multi-line expression is iffy #1032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
(FWIW see #627 which seems to indicate that in the past the opposite was the case. :-) |
Yeah, it would make sense for mypy to allow |
@gvanrossum @JukkaL any chance this could be implemented? maybe, it's better to allow something like
|
The parsing of However here's an idea: starting with the Python 3.8 ast, the end line and column offset of AST nodes is recorded. This means that we could change mypy so that a Do we have a volunteer to attempt to implement this? The core mypy team is probably too busy with other projects for the rest of the year. |
@gvanrossum thanks for the explanation. I'll add this to my list of "things I want to do" as the first item. This could lead to me doing this sometime before the end of the year:) |
Yes, this should be not hard for an external contributor. |
How would you find the start and end line for an arbitrary error, since we only pass a "local" context such as a subexpression when generating errors? May we could have an extra pass where we record the spans of all statements. |
I think this should be sufficient. We should just always give the correct context for error. In the original example we should only use If somewhere we pass a wrong context that just happens to have the same start position then it should be fixed independently (I could imagine there is a bunch of such places in code currently). |
Fixes #1032. On Python 3.8, we use `end_lineno` information to scope `# type: ignore` comments to enclosing expressions. Auto-formatter users, rejoice! The `--warn-unused-ignores` semantics should be clear from the test cases. Basically, ignores in inner scopes take precedence over ignores in enclosing scopes, and the first of multiple ignores in a scope "wins". A few notes: - This adds a new slot, `Context.end_line`. It defaults to `None` in the absence of an `end_lineno` or if `Context` is not an expression. - `ErrorInfo.origin` now has a third member, representing the end line of the error context. It is used to determine the range of lines to search for `# type: ignore` comments. If unavailable, it defaults to the same value as the origin line. - Because this uses 3.8-only AST features, a new `check-38.test` file has been created, and is hidden behind a version guard in `testcheck.py`.
The placement of these comments got more flexible in 3.8 due to python/mypy#1032 Satisfying older Python and fitting in flake8's 79-character line limit was quite a challenge!
Squashed commit of the following: commit fa229f7 Merge: eaab3c6 c3e3499 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 20:33:06 2021 -0700 Merge branch 'develop' into mypy (and fixed types) commit eaab3c6 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 20:00:45 2021 -0700 reformat all multi-line function defs to one-arg-per-line commit 3fe2b69 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:58:48 2021 -0700 ccitt nit -- avoid casting needlessly commit 15983d8 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:58:36 2021 -0700 tweak CHANGELOG commit 13dc0ba Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:43:46 2021 -0700 add failing tests for dumppdf crash commit 6b509c5 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:24:23 2021 -0700 ccitt: apply misc PR feedback commit feb031b Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:18:26 2021 -0700 add missing None return type to all __init__ methods commit c0d62d6 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:13:08 2021 -0700 minor cleanup, remove a few more Any types commit b52a059 Author: Andrew Baumann <[email protected]> Date: Sun Sep 5 22:37:28 2021 -0700 tighten up types, avoid Any in favour of explicit casts commit e58fd48 Author: Andrew Baumann <[email protected]> Date: Sun Sep 5 14:10:49 2021 -0700 annotate ccitt.py, and fix one definite bug (array.tostring was renamed tobytes) commit 6052906 Author: Andrew Baumann <[email protected]> Date: Sat Sep 4 22:37:38 2021 -0700 python 3.7 back-compat commit 4dbcf87 Author: Andrew Baumann <[email protected]> Date: Sat Sep 4 22:32:43 2021 -0700 annotate pdfminer.jbig2 commit 0d40b7c Author: Andrew Baumann <[email protected]> Date: Sat Sep 4 22:31:33 2021 -0700 annotate pdf2txt.py commit 5f82eb4 Author: Andrew Baumann <[email protected]> Date: Sat Sep 4 09:16:31 2021 -0700 cleanup: make Plane generic commit 624fc92 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 23:16:51 2021 -0700 bluntly ignore calls to cryptography.hazmat commit 96b2043 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 23:01:06 2021 -0700 finish annotating, and disallow_untyped_defs for pdfminer.* _except_ ccitt and jbig2 commit 0ab5863 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 21:51:56 2021 -0700 annotate pdffont commit 4b689f1 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 18:30:02 2021 -0700 annotate a couple more scripts; document sketchy code commit 291981f Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 15:02:01 2021 -0700 pacify flake8 commit 45d2ce9 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 14:31:48 2021 -0700 annotate dumppdf, and comment likely bugs commit 7278d83 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 13:49:58 2021 -0700 enable mypy on tests and tools, fix one implicit reexport bug commit 4a83166 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 13:25:59 2021 -0700 pdfdocument: per dumppdf.py, get_dest accepts either bytes or str commit 43701e1 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 13:25:00 2021 -0700 layout: LAParams.boxes_flow may be None commit 164f816 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 09:45:09 2021 -0700 add whitespace, pacify flake8 commit 893b9fb Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 09:40:33 2021 -0700 support old Python without typing.Protocol commit dc24508 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 09:12:03 2021 -0700 Move "# type: ignore" comments to fix mypy on Python < 3.8 The placement of these comments got more flexible in 3.8 due to python/mypy#1032 Satisfying older Python and fitting in flake8's 79-character line limit was quite a challenge! commit da03afe Author: Andrew Baumann <[email protected]> Date: Thu Sep 2 22:59:58 2021 -0700 fix text output from HTMLConverter commit 5401276 Author: Andrew Baumann <[email protected]> Date: Thu Sep 2 22:40:22 2021 -0700 annotate high_level.py and the immediately-reachable internal APIs (mostly converters) commit cc49051 Author: Andrew Baumann <[email protected]> Date: Thu Sep 2 17:04:35 2021 -0700 * expand and improve annotations in cmap, encryption/decompression and fonts * disallow untyped calls; this way, we have a core set of typed code that can grow over time (just not for ccitt, because there's a ton of work lurking there) * expand "typing: none" comments to suppress a specific error code commit 92df54b Author: Andrew Baumann <[email protected]> Date: Wed Sep 1 20:50:59 2021 -0700 update CHANGELOG commit f72aaea Merge: ff787a9 8ea9f10 Author: Andrew Baumann <[email protected]> Date: Wed Sep 1 20:47:03 2021 -0700 Merge branch 'develop' into mypy commit ff787a9 Author: Andrew Baumann <[email protected]> Date: Sat Aug 21 21:46:14 2021 -0700 be more precise about types on ps/pdf stacks, remove most of the Any annotations commit be15501 Author: Andrew Baumann <[email protected]> Date: Sat Aug 21 10:13:58 2021 -0700 silence missing imports, (maybe?) hook to tox commit ff4b6a9 Author: Andrew Baumann <[email protected]> Date: Fri Aug 20 22:49:06 2021 -0700 turn on more strict checks, and untangle the layout mess with generics Status: $ mypy pdfminer pdfminer/ccitt.py:565: error: Cannot find implementation or library stub for module named "pygame" pdfminer/ccitt.py:565: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports pdfminer/pdfdocument.py:7: error: Skipping analyzing "cryptography.hazmat.backends": found module but no type hints or library stubs pdfminer/pdfdocument.py:8: error: Skipping analyzing "cryptography.hazmat.primitives.ciphers": found module but no type hints or library stubs pdfminer/pdfdevice.py:191: error: Argument 1 to "write" of "IO" has incompatible type "str"; expected "bytes" pdfminer/image.py:84: error: Cannot find implementation or library stub for module named "PIL" Found 5 errors in 4 files (checked 27 source files) pdfdevice.py:191 appears to be a real bug commit 5c9c0b1 Author: Andrew Baumann <[email protected]> Date: Fri Aug 20 17:22:41 2021 -0700 finish annotating layout commit 0e6871c Author: Andrew Baumann <[email protected]> Date: Fri Aug 20 16:54:46 2021 -0700 general progress on annotations * finish utils * annotate more of pdfinterp, pdfdevice * document reason for # type: ignore comments * fix cyclic imports * satisfy flake8 commit 17d59f4 Author: Andrew Baumann <[email protected]> Date: Thu Aug 19 21:38:50 2021 -0700 WIP on type annotations With the possible exception of psparser.py, this is far from complete. $ mypy pdfminer pdfminer/ccitt.py:565: error: Cannot find implementation or library stub for module named "pygame" pdfminer/ccitt.py:565: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports pdfminer/pdfdocument.py:7: error: Skipping analyzing "cryptography.hazmat.backends": found module but no type hints or library stubs pdfminer/pdfdocument.py:8: error: Skipping analyzing "cryptography.hazmat.primitives.ciphers": found module but no type hints or library stubs pdfminer/image.py:84: error: Cannot find implementation or library stub for module named "PIL"
Squashed commit of the following: commit fa229f7 Merge: eaab3c6 c3e3499 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 20:33:06 2021 -0700 Merge branch 'develop' into mypy (and fixed types) commit eaab3c6 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 20:00:45 2021 -0700 reformat all multi-line function defs to one-arg-per-line commit 3fe2b69 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:58:48 2021 -0700 ccitt nit -- avoid casting needlessly commit 15983d8 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:58:36 2021 -0700 tweak CHANGELOG commit 13dc0ba Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:43:46 2021 -0700 add failing tests for dumppdf crash commit 6b509c5 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:24:23 2021 -0700 ccitt: apply misc PR feedback commit feb031b Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:18:26 2021 -0700 add missing None return type to all __init__ methods commit c0d62d6 Author: Andrew Baumann <[email protected]> Date: Mon Sep 6 15:13:08 2021 -0700 minor cleanup, remove a few more Any types commit b52a059 Author: Andrew Baumann <[email protected]> Date: Sun Sep 5 22:37:28 2021 -0700 tighten up types, avoid Any in favour of explicit casts commit e58fd48 Author: Andrew Baumann <[email protected]> Date: Sun Sep 5 14:10:49 2021 -0700 annotate ccitt.py, and fix one definite bug (array.tostring was renamed tobytes) commit 6052906 Author: Andrew Baumann <[email protected]> Date: Sat Sep 4 22:37:38 2021 -0700 python 3.7 back-compat commit 4dbcf87 Author: Andrew Baumann <[email protected]> Date: Sat Sep 4 22:32:43 2021 -0700 annotate pdfminer.jbig2 commit 0d40b7c Author: Andrew Baumann <[email protected]> Date: Sat Sep 4 22:31:33 2021 -0700 annotate pdf2txt.py commit 5f82eb4 Author: Andrew Baumann <[email protected]> Date: Sat Sep 4 09:16:31 2021 -0700 cleanup: make Plane generic commit 624fc92 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 23:16:51 2021 -0700 bluntly ignore calls to cryptography.hazmat commit 96b2043 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 23:01:06 2021 -0700 finish annotating, and disallow_untyped_defs for pdfminer.* _except_ ccitt and jbig2 commit 0ab5863 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 21:51:56 2021 -0700 annotate pdffont commit 4b689f1 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 18:30:02 2021 -0700 annotate a couple more scripts; document sketchy code commit 291981f Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 15:02:01 2021 -0700 pacify flake8 commit 45d2ce9 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 14:31:48 2021 -0700 annotate dumppdf, and comment likely bugs commit 7278d83 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 13:49:58 2021 -0700 enable mypy on tests and tools, fix one implicit reexport bug commit 4a83166 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 13:25:59 2021 -0700 pdfdocument: per dumppdf.py, get_dest accepts either bytes or str commit 43701e1 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 13:25:00 2021 -0700 layout: LAParams.boxes_flow may be None commit 164f816 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 09:45:09 2021 -0700 add whitespace, pacify flake8 commit 893b9fb Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 09:40:33 2021 -0700 support old Python without typing.Protocol commit dc24508 Author: Andrew Baumann <[email protected]> Date: Fri Sep 3 09:12:03 2021 -0700 Move "# type: ignore" comments to fix mypy on Python < 3.8 The placement of these comments got more flexible in 3.8 due to python/mypy#1032 Satisfying older Python and fitting in flake8's 79-character line limit was quite a challenge! commit da03afe Author: Andrew Baumann <[email protected]> Date: Thu Sep 2 22:59:58 2021 -0700 fix text output from HTMLConverter commit 5401276 Author: Andrew Baumann <[email protected]> Date: Thu Sep 2 22:40:22 2021 -0700 annotate high_level.py and the immediately-reachable internal APIs (mostly converters) commit cc49051 Author: Andrew Baumann <[email protected]> Date: Thu Sep 2 17:04:35 2021 -0700 * expand and improve annotations in cmap, encryption/decompression and fonts * disallow untyped calls; this way, we have a core set of typed code that can grow over time (just not for ccitt, because there's a ton of work lurking there) * expand "typing: none" comments to suppress a specific error code commit 92df54b Author: Andrew Baumann <[email protected]> Date: Wed Sep 1 20:50:59 2021 -0700 update CHANGELOG commit f72aaea Merge: ff787a9 8ea9f10 Author: Andrew Baumann <[email protected]> Date: Wed Sep 1 20:47:03 2021 -0700 Merge branch 'develop' into mypy commit ff787a9 Author: Andrew Baumann <[email protected]> Date: Sat Aug 21 21:46:14 2021 -0700 be more precise about types on ps/pdf stacks, remove most of the Any annotations commit be15501 Author: Andrew Baumann <[email protected]> Date: Sat Aug 21 10:13:58 2021 -0700 silence missing imports, (maybe?) hook to tox commit ff4b6a9 Author: Andrew Baumann <[email protected]> Date: Fri Aug 20 22:49:06 2021 -0700 turn on more strict checks, and untangle the layout mess with generics Status: $ mypy pdfminer pdfminer/ccitt.py:565: error: Cannot find implementation or library stub for module named "pygame" pdfminer/ccitt.py:565: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports pdfminer/pdfdocument.py:7: error: Skipping analyzing "cryptography.hazmat.backends": found module but no type hints or library stubs pdfminer/pdfdocument.py:8: error: Skipping analyzing "cryptography.hazmat.primitives.ciphers": found module but no type hints or library stubs pdfminer/pdfdevice.py:191: error: Argument 1 to "write" of "IO" has incompatible type "str"; expected "bytes" pdfminer/image.py:84: error: Cannot find implementation or library stub for module named "PIL" Found 5 errors in 4 files (checked 27 source files) pdfdevice.py:191 appears to be a real bug commit 5c9c0b1 Author: Andrew Baumann <[email protected]> Date: Fri Aug 20 17:22:41 2021 -0700 finish annotating layout commit 0e6871c Author: Andrew Baumann <[email protected]> Date: Fri Aug 20 16:54:46 2021 -0700 general progress on annotations * finish utils * annotate more of pdfinterp, pdfdevice * document reason for # type: ignore comments * fix cyclic imports * satisfy flake8 commit 17d59f4 Author: Andrew Baumann <[email protected]> Date: Thu Aug 19 21:38:50 2021 -0700 WIP on type annotations With the possible exception of psparser.py, this is far from complete. $ mypy pdfminer pdfminer/ccitt.py:565: error: Cannot find implementation or library stub for module named "pygame" pdfminer/ccitt.py:565: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports pdfminer/pdfdocument.py:7: error: Skipping analyzing "cryptography.hazmat.backends": found module but no type hints or library stubs pdfminer/pdfdocument.py:8: error: Skipping analyzing "cryptography.hazmat.primitives.ciphers": found module but no type hints or library stubs pdfminer/image.py:84: error: Cannot find implementation or library stub for module named "PIL"
Consider (this stands in for a longer example :-):
This currently gives an error due to #984. So I want to add
# type: ignore
. Unfortunately this doesn't work:Instead I must use this:
which is a little unintuitive.
The text was updated successfully, but these errors were encountered: