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

Is a module-level # type: ignore[...] always invalid? #14628

Closed
elisw93 opened this issue Feb 7, 2023 · 2 comments
Closed

Is a module-level # type: ignore[...] always invalid? #14628

elisw93 opened this issue Feb 7, 2023 · 2 comments
Labels

Comments

@elisw93
Copy link

elisw93 commented Feb 7, 2023

And if so, is it possible to error and/or issue a diagnostic when it is parsed?

It's been noted in the past that # type: ignore behavior is inconsistent between line- and module-level contexts, and that inconsistency can be confusing for users who only wanted mypy to ignore errors in their module, but unintentionally asked it to ignore the module entirely. This tends to cause tricky failure cases. Issue #14340 goes into some more detail about this, and resulted in a documentation change in #14342, which is good.

However, I recently ran into a related issue where a developer moved a line-level # type: ignore[list-item] statement to the module-level scope, (reasonably) assuming this would ask mypy to ignore only list-item validation errors in the module. Instead, this silently and surprisingly caused the whole module to be ignored. I looked in the relevant documentation and could find no indication that # type: ignore[...] should be valid at the module level. Therefore, unlike the failure case described in #14340, I think this error is possible to catch without changing the behavior of module-level # type: ignore, because the extra [list-item] specifier at the end has no meaning in the module-level context.

If a module-level # type: ignore[...] is never valid, I propose that it should be treated as an error or warning, and mypy should emit a diagnostic and/or halt processing if it encounters such a statement.

Note: While researching this issue, I noticed that the mypy documentation for # type: ignore refers to a statement applied to the whole file as a top-level comment, but this seems to conflict with the definition of top-level code environment given in the Python documentation (What is the “top-level code environment”?). Because of this, I have opted to use the term module-level instead, as it refers to a statement that applies to the entire Python module. If I am wrong about this, please understand that I am referring to what is termed the top-level comment in mypy docs.

@elisw93 elisw93 added the feature label Feb 7, 2023
@hauntsaninja
Copy link
Collaborator

Instead, this silently and surprisingly caused the whole module to be ignored

This is no longer true, I changed this in #13512 . This change was included in the last couple mypy releases (IIRC since 0.990)

@elisw93
Copy link
Author

elisw93 commented Feb 7, 2023

Awesome, I thought I had re-tested my repro case with mypy 1.0.0, but it turns out the version in my $PATH was still 0.950 😞

Upgrading to 1.0.0 produces exactly the behavior I had asked for. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants