Skip to content

Support 'on'/'off' for type ignores #9483

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

Open
wadetregaskis-linkedin opened this issue Sep 25, 2020 · 4 comments
Open

Support 'on'/'off' for type ignores #9483

wadetregaskis-linkedin opened this issue Sep 25, 2020 · 4 comments
Labels

Comments

@wadetregaskis-linkedin
Copy link

Sometimes it's impossible to put # type: ignore on the offending line - e.g. because it's a multiline string, and there's nowhere you can put the comment outside the string that mypy will recognise. Or more generally, there might be a whole block of code for which mypy has a bunch of spurious complaints, so putting in dozens of # type: ignores is a bit noisy.

The only other option today is to have mypy ignore the entire file, but that has nasty ramifications - beyond just killing type checking for what might be 99% checkable, it also makes any imports of that file invalid, and so you have to spread ignore directives out like a messy spiderweb.

Thus, it'd be great if one could do e.g. # type: ignore on and # type: ignore off, so that the scope could be controlled precisely.

@wadetregaskis-linkedin wadetregaskis-linkedin added the bug mypy got something wrong label Sep 25, 2020
@wadetregaskis-linkedin
Copy link
Author

Related: #9484

@hauntsaninja
Copy link
Collaborator

Related, also mentions typing.no_type_check: #6948

@wadetregaskis-linkedin
Copy link
Author

Awesome, I didn't know no_type_check existed. That's a much better workaround than the others I knew of. It's not quite as nice as what I requested here, but it's close - I just have to wrap the code in question in a dummy, nested function in order to be able to apply the decorator. Silly enough that it requires a comment documenting it, but quite functional otherwise.

Sidenote: no_type_check in name sounds clear, but the documentation for it is super misleading. "Decorator to indicate that annotations are not type hints."… wot? That doesn't sound like what it actually does at all.

@gvanrossum
Copy link
Member

Could you send a PR that updates the docs to your liking? I agree that phrasing is odd -- it comes from the early days of discussion about PEP 484, when there appeared to be a substantial (or at least vocal) minority of Python users who didn't want to use annotations for types because they already had some other (ab)use in mind.

@hauntsaninja hauntsaninja added feature and removed bug mypy got something wrong labels Sep 28, 2020
@AlexWaygood AlexWaygood added topic-type-ignore # type: ignore comments documentation labels Apr 1, 2022
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

4 participants