You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect "Unused 'type: ignore' comment" on top-level ignore[error-code] #12076
When there's a top-level type-ignore with error code, like # type: ignore[no-untyped-defs], and --warn-unused-ignores is passed, this type comment always shows up as an unused ignore, even though the type checking would fail without that comment.
To Reproduce
Create this file:
# type: ignore[no-untyped-def]defuntyped(): pass
Run using mypy test.py --strict. (which includes --warn-unused-ignores)
Expected Behavior
This should pass, because removing the type comment causes the no-untyped-def error to appear.
Actual Behavior
$ mypy test.py --warn-unused-ignores
test.py:1: error: Unused "type: ignore" comment
Found 1 error in 1 file (checked 1 source file)
I am not sure if this is a bug or a missing features.
The documentation states only to use type: ignore to ignore the whole module (without any specific error code). That also matches with the code (I just looked it up):
# A "# type: ignore" comment before the first statement of a module
# ignores the whole module:
if (ismodule and stmts and self.type_ignores
and min(self.type_ignores) < self.get_lineno(stmts[0])):
self.errors.used_ignored_lines[self.errors.file][min(self.type_ignores)].append(
codes.MISC.code)
block = Block(self.fix_function_overloads(self.translate_stmt_list(stmts)))
mark_block_unreachable(block)
return [block]
(Note that I am not a mypy developer, so I might be wrong)
You can use a special comment # type: ignore[code, ...] to only ignore errors with a specific error code (or codes) on a particular line. This can be used even if you have not configured mypy to show error codes. Currently it’s only possible to disable arbitrary error codes on individual lines using this comment.
The best solution would be to add support for error codes on ignores at the top of a file, to ignore all errors of that code in the whole file.
If that isn't possible, I think it should give an error more like (whether used with --warn-unused-ignores or not):
test.py:1: error: Error code not supported on file level ignore
Activity
KotlinIsland commentedon Jan 27, 2022
You have a small typo:
Should be:
# type: ignore[no-untyped-def]But the issue is still completely valid.
jonathanslenders commentedon Jan 28, 2022
Thanks @KotlinIsland, I fixed the code snippet.
kreathon commentedon Jan 29, 2022
I am not sure if this is a bug or a missing features.
The documentation states only to use
type: ignoreto ignore the whole module (without any specific error code). That also matches with the code (I just looked it up):(Note that I am not a mypy developer, so I might be wrong)
Zeckie commentedon Mar 12, 2022
It is ignoring the supplied error code, and treating it as though it is just
# type: ignore, pretty much the opposite of what the error message says.Example showing that it ignores errors with a different code:
https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-codes%2Cstrict%2Cwarn-unused-ignores&gist=a864a4660d66062af67f85f49e89096f
The documentation currently says:
And on a different page:
The best solution would be to add support for error codes on ignores at the top of a file, to ignore all errors of that code in the whole file.
If that isn't possible, I think it should give an error more like (whether used with
--warn-unused-ignoresor not):KotlinIsland commentedon Mar 13, 2022
Put this at the top of the file and you will get the desired behavior:
# mypy: allow-untyped-defsplayground
It's using a feature called inline configuration
The following doesn't work (invalid module level config), but is silently ignored due to #12342:
# mypy: disable-error-code=no-untyped-defWarn on module level type ignore with error code
Warn on module level type ignore with error code (#13512)
mypy(shell): Allow untyped calls
mypy(shell): Allow untyped calls