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

Getting Invalid "type: ignore" comment reports from 3rd party library code #12299

Closed
Fogapod opened this issue Mar 6, 2022 · 4 comments
Closed
Labels
bug mypy got something wrong topic-type-ignore # type: ignore comments

Comments

@Fogapod
Copy link

Fogapod commented Mar 6, 2022

Bug Report

3rd party library I am using recently added pyright style type: ignore comments. I have no control on those. Example: https://github.com/Rapptz/discord.py/blob/1c4dca431db375a24820abd23f91796dcf01ad32/discord/app_commands/namespace.py#L124

For some reason mypy complains about these instead of analyzing my code. Shouldnt bad comments in 3rd party code be treated as just type: ignore?

To Reproduce

  1. python -m venv .venv && source .venv/bin/activate
  2. pip install mypy 'git+https://github.com/Rapptz/discord.py@a909c1ff069b1bdb332b7f3b01ef1c4b8a3a3a15'
  3. create file main.py:
import discord

1 + "2"
  1. mypy main.py

Expected Behavior

mypy should report errors in my code

Actual Behavior

mypy ignores errors in my code and instead complains about bad type: ignore comments in 3rd party library.

.venv/lib/python3.10/site-packages/discord/client.py:799: error: Invalid "type: ignore" comment
.venv/lib/python3.10/site-packages/discord/client.py:1162: error: Invalid "type: ignore" comment
Found 2 errors in 1 file (errors prevented further checking)

Only if I remove import discord line, mypy reports error in file.

Your Environment

  • Mypy version used: 0.931
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.2
  • Operating system and version: Arch Linux
@Fogapod Fogapod added the bug mypy got something wrong label Mar 6, 2022
@hauntsaninja
Copy link
Collaborator

Agreed that this isn't a great user experience. mypy treats invalid type ignore comments like it would syntax errors. Maybe we should make these non-blocking, so a per-module ignore_errors could work.

What do you mean by pyright-style type ignore comments? Is there a reference for that?

Your best workaround for now is something like:

[mypy-discord.*]
follow_imports = skip

@erictraut
Copy link

I think that "pyright-style" is meant to imply "a form that pyright accepts".

Since PEP 484 isn't very specific about what forms of # type: ignore are accepted, I made some (somewhat arbitrary) assumptions when implementing the support in pyright. In particular, I accept any comment starting with # type: ignore as a valid ignore comment. Anything after the # type: ignore is effectively ignored by pyright. This allows for additional information that may be interpreted by other tools such as # type: ignore; noqa or # type: ignore[misc]. It also allows for additional human-readable comments like type: ignore -- Key is there. This library is apparently taking advantage of the latter.

I think we have a couple of options here:

  1. Mypy could be updated so it's "looser" in how it interprets # type: ignore comments.
  2. We could agree on a "tighter" specification, and pyright (and other type checkers) could enforce that specification.

I don't have a strong opinion, but I have a slight preference for option 1 because I think there's utility in allowing additional information after the # type: ignore comment.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Mar 15, 2022

Note that mypy allows the # type: ignore # Key is there syntax mentioned in PEP 484 for additional human-readable comments. If you're coming here as an author of discord.py, I'd encourage use of this style. Since it's mentioned in PEP 484, it's likely to work with all type checkers and should help avoid confusion with things like type ignore error codes.

Changing mypy to be less strict here makes sense to me; user intention is clear modulo typos like # type: ignore[misc. Certainly not worth a blocking error like it is right now.

@JelleZijlstra JelleZijlstra added the topic-type-ignore # type: ignore comments label Mar 19, 2022
hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Aug 25, 2022
Blocking errors are a bad user experience and there's no reason for this
one to be one / there is another code path for invalid type ignores that
is non-blocking.

Fixes half of python#12299. The half it doesn't fix is that ideally users
shouldn't be getting these warnings from third party libraries.
Also see python#12162 (comment)
But that's for another PR
hauntsaninja added a commit that referenced this issue Aug 25, 2022
Blocking errors are a bad user experience and there's no reason for this
one to be one / there is another code path for invalid type ignores that
is non-blocking.

Fixes half of #12299. The half it doesn't fix is that ideally users
shouldn't be getting these warnings from third party libraries.
Also see #12162 (comment)
But that's for another PR
@hauntsaninja
Copy link
Collaborator

I fixed half of this (that it's a blocking error) in #13506. Let's track other half (hide parsing errors from 3rd party better) at #12162

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

No branches or pull requests

4 participants