Skip to content

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

Closed

Description

@jonathanslenders

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

Activity

  1. KotlinIsland commented on Jan 27, 2022

    @KotlinIsland
    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.

  2. jonathanslenders commented on Jan 28, 2022

    @jonathanslenders
    Author

    Thanks @KotlinIsland, I fixed the code snippet.

  3. kreathon commented on Jan 29, 2022

    @kreathon
    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)

  4. Zeckie commented on Mar 12, 2022

    @Zeckie

    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
    
  5. KotlinIsland commented on Mar 13, 2022

    @KotlinIsland
    Contributor

    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
  6. added a commit that references this issue on Aug 25, 2022
    9dd525d
  7. added a commit that references this issue on Aug 26, 2022
    da56c97
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-ignore# type: ignore comments

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions