Skip to content

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

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

Closed
JukkaL opened this issue Nov 28, 2018 · 4 comments
Closed

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

JukkaL opened this issue Nov 28, 2018 · 4 comments
Assignees
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 28, 2018

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
@JukkaL JukkaL added bug mypy got something wrong false-positive mypy gave an error on correct code labels Nov 28, 2018
@emmatyping
Copy link
Member

emmatyping commented Nov 28, 2018

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.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Nov 28, 2018

This happens also with variable type comments:

x = 1 # type: str # type: ignore

@JukkaL
Copy link
Collaborator Author

JukkaL commented Nov 28, 2018

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

@emmatyping
Copy link
Member

emmatyping commented Nov 28, 2018

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?

emmatyping added a commit to emmatyping/mypy that referenced this issue Mar 26, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
This is a bit of a hack in fastparse, but allows for the following to pass typechecking:

`x = 1  # type: str  # type: ignore`

Fixes python#5967
@emmatyping emmatyping self-assigned this Mar 26, 2019
emmatyping added a commit that referenced this issue Mar 27, 2019
This is a hack in fastparse, but allows for the following to pass typechecking:

`x = 1 # type: str # type: ignore`

This also handles the edge case where there is a `# type: ignore` in a comment, which we don't want to pick up. See the tests for more examples.

Fixes #5967
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high
Projects
None yet
Development

No branches or pull requests

2 participants