Skip to content

Type ignore comment has no effect after argument type comment #5967

Closed

Description

@JukkaL

This program generates an error on the first line even though there is a # type: ignore comment:

def f(x,  # type: x  # type: ignore
      ):
    # type: (...) -> None
    pass

Similarly, this generates an error even though it shouldn't:

def f(x=y,  # type: int  # type: ignore  # Name 'y' not defined
      ):
    # type: (...) -> None
    pass

Activity

  1. added
    bugmypy got something wrong
    false-positivemypy gave an error on correct code
    on Nov 28, 2018
  2. emmatyping commented on Nov 28, 2018

    @emmatyping
    Member

    For your first example, here is the AST as parsed by typed_ast:

    Module(
        body=[
            FunctionDef(
                lineno=2,
                col_offset=0,
                name='f',
                args=arguments(
                    args=[arg(lineno=2, col_offset=6, arg='x', annotation=None, type_comment='x  # type: ignore')],
                    vararg=None,
                    kwonlyargs=[],
                    kw_defaults=[],
                    kwarg=None,
                    defaults=[],
                ),
                body=[Pass(lineno=5, col_offset=4)],
                decorator_list=[],
                returns=None,
                type_comment='(...) -> None',
            ),
        ],
        type_ignores=[],
    )
    

    Note the type_comment='x # type: ignore'.
    We generally ignore comments after the first type comment, but it should be easy to make an exception for #\s+type:\s+ignore and add that line to the list of lines to ignore.

  3. JukkaL commented on Nov 28, 2018

    @JukkaL
    CollaboratorAuthor

    This happens also with variable type comments:

    x = 1 # type: str # type: ignore
  4. JukkaL commented on Nov 28, 2018

    @JukkaL
    CollaboratorAuthor

    Increasing priority since it's not at all obvious how to work this around in general.

  5. emmatyping commented on Nov 28, 2018

    @emmatyping
    Member

    I believe I have hacked together a general implementation in fastparse. I just do a regex search for a # type: ignore when parsing the type comment and keep track of these additional type ignores in the Ast converter class, then add them to the set of ignores when visiting the Module node.

    Though this likely should be implemented in typed_ast itself...
    Edit: looking over the typed_ast code, it does not seem simple to implement this, maybe putting this in fastparse is ok?

  6. self-assigned this
    on Mar 26, 2019
  7. added a commit that references this issue on Mar 27, 2019
    d1e597d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions