Skip to content
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

Incorrect "Unused 'type: ignore' comment" on top-level ignore[error-code] #12076

Closed
jonathanslenders opened this issue Jan 26, 2022 · 5 comments · Fixed by #13512
Closed

Incorrect "Unused 'type: ignore' comment" on top-level ignore[error-code] #12076

jonathanslenders opened this issue Jan 26, 2022 · 5 comments · Fixed by #13512
Labels
bug mypy got something wrong topic-type-ignore # type: ignore comments

Comments

@jonathanslenders
Copy link

jonathanslenders commented Jan 26, 2022

Bug Report

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]

def untyped(): 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)

Your Environment

Mypy 0.931, Python 3.9.9, Ubuntu 20.04

@jonathanslenders jonathanslenders added the bug mypy got something wrong label Jan 26, 2022
@KotlinIsland
Copy link
Contributor

You have a small typo:

# type: ignore[no-untyped-defs]

Should be: # type: ignore[no-untyped-def]

But the issue is still completely valid.

@jonathanslenders
Copy link
Author

Thanks @KotlinIsland, I fixed the code snippet.

@kreathon
Copy link
Contributor

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)

@Zeckie
Copy link

Zeckie commented 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:

... you can also ignore all mypy checks in a file, by adding a # type: ignore at the top of the file:

And on a different page:

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

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Mar 13, 2022

Put this at the top of the file and you will get the desired behavior:

# mypy: allow-untyped-defs

playground
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-def

@AlexWaygood AlexWaygood added the topic-type-ignore # type: ignore comments label Mar 26, 2022
hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Aug 25, 2022
Per-module error codes were added in python#13502, let's recommend using them.

The existing type ignore behaviour is pretty unintuitive; I think most
people actually want `# mypy: ignore-errors`. There are probably people
depending on the current behaviour though.

Fixes python#13435, fixes python#12076, fixes python#11999, fixes python#11027, fixes python#9318,
fixes python#7839
hauntsaninja added a commit that referenced this issue Aug 26, 2022
Per-module error codes were added in #13502, let's recommend using them.

The existing type ignore behaviour is pretty unintuitive; I think most
people actually want `# mypy: ignore-errors`. There are probably people
depending on the current behaviour though.

Fixes #13435, fixes #12076, fixes #11999, fixes #11027, fixes #9318,
fixes #7839
tony added a commit to tmux-python/tmuxp that referenced this issue Jun 1, 2024
I do not think this handled the solution surgically, it seems to force losing
safety for the rule across a *whole file* rather than surgically at the offending
line.

See also: python/mypy#12076, python/mypy#13512
tony added a commit to tmux-python/tmuxp that referenced this issue Jun 1, 2024
I do not think this handled the solution surgically, it seems to force losing
safety for the rule across a *whole file* rather than surgically at the offending
line.

See also: python/mypy#12076, python/mypy#13512
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-type-ignore # type: ignore comments
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants