Skip to content

"# type: ignore" does not seem to work on multiline dicts #627

Closed

Description

@josephharrington

There seems to be inconsistent behavior between these two equivalent pieces of code.

mypy is happy with this:

import http.client
errors = {'not_found': http.client.NOT_FOUND}  # type: ignore

but this fails with "module" has no attribute "NOT_FOUND" (no matter where I put the annotation):

import http.client
errors = {                              # type: ignore
    'not_found': http.client.NOT_FOUND  # type: ignore
}                                       # type: ignore

Activity

  1. JukkaL commented on Mar 31, 2015

    @JukkaL
    Collaborator

    Thanks for reporting this! I think the reason is that mypy only recognizes ignore annotations on the last line of a statement, which is clearly broken.

    I think that there are also some other cases where # type: ignore might not work correctly. The feature is still going to need a bit more love :-) If you spot any other cases where it behaves strangely, please report them since this is a pretty important feature.

  2. JukkaL commented on Apr 5, 2015

    @JukkaL
    Collaborator

    Your example should now work like this:

    import http.client
    errors = {
        'not_found': http.client.NOT_FOUND  # type: ignore
    } 

    The # type: ignore comment should be put on the line that the error refers to. This way we can only ignore parts of a multi-line statement, which can be useful in cases such as large dict literals.

  3. gvanrossum commented on Apr 6, 2015

    @gvanrossum
    Member

    Do you think we should specify this in the PEP? Or is this a mypy "extra"?

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 wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions