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

Top-level assert does not skip 'type: ignore' comments #8234

Closed
hashstat opened this issue Jan 3, 2020 · 3 comments
Closed

Top-level assert does not skip 'type: ignore' comments #8234

hashstat opened this issue Jan 3, 2020 · 3 comments
Labels
bug mypy got something wrong priority-1-normal topic-reachability Detecting unreachable code topic-type-ignore # type: ignore comments

Comments

@hashstat
Copy link

hashstat commented Jan 3, 2020

When using mypy with the --warn-unused-ignore option, type: ignore comments are still analyzed, and possibly reported, when occurring after assert sys.platform == ... statements.

Take the following module as an example:

# test.py

import sys

assert sys.platform == "win32"

class Foo:
    pass

foo = Foo()
foo.bar = 1  # type: ignore[attr-defined]

Running mypy --warn-unused-ignore --platform win32 test.py finds no errors, as expected. However, mypy --warn-unused-ignore --platform linux test.py reports the following error:

test.py:11: error: unused 'type: ignore' comment
Found 1 error in 1 file (checked 1 source file)

I would expect (by reading the documentation) that the assert should cause the rest of the file to be skipped, including type: ignore comments, since they also are likely platform-specific. The same issue likely occurs for sys.version_info asserts as well.

I am experiencing the issue with Python 3.7.5 and mypy 0.750, 0.760, 0.761, and master.

@msullivan msullivan added bug mypy got something wrong priority-1-normal labels Jan 6, 2020
@msullivan
Copy link
Collaborator

msullivan commented Jan 6, 2020

I would agree that this is a bug although I suspect it is might be more painful to fix than it is worth, since the unused type: ignore check is pretty divorced from everything else (and just looks at the errors reported).

It might be worth disabling this for your platform specific files, which you can do with # mypy: no-warn-unused-ignores at the top of the file [edit: ignore -> ignores]

@hashstat
Copy link
Author

@msullivan, I had worked around it by adding the following to mypy.ini:

[mypy-mypackage.exclusive.*]
# Work around mypy bug #8234: https://github.com/python/mypy/issues/8234
warn_unused_ignores = False

But I like your idea better as it's more targeted, but gave me an error at first until I added the missing 's' in ignores. Thanks for the reminder of the inline mypy configuration. This is the first time I've had to use it.

@AlexWaygood AlexWaygood added topic-type-ignore # type: ignore comments topic-reachability Detecting unreachable code labels Mar 29, 2022
@hauntsaninja
Copy link
Collaborator

See #8823 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal topic-reachability Detecting unreachable code topic-type-ignore # type: ignore comments
Projects
None yet
Development

No branches or pull requests

4 participants