-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Support mypy: ignore
inline comments to suppress mypy errors
#12358
Comments
This feature isn't likely to be implemented, since we use Python's One slightly verbose way of achieving the same effect would be using the
|
Sorry, I didn’t follow that. The goal here is to provide an alternate syntax for an ignore comment that targets mypy only, and not other type checkers. So adding support for “mypy: ignore” as an alternate spelling of “type: ignore” is a lot harder than it seems? If so, is there any other syntax you can propose that would be easier to implement and achieve the same effect? |
The first part of type checking is parsing a string into a syntax tree. mypy uses the stdlib's A thing that works today with current versions of mypy is:
A little verbose, but works today and could maybe made more concise depending on context. |
Thanks @hauntsaninja, now I see what you meant, and I appreciate the additional explanation and the proposed workaround. That said, I think an important goal for any workaround here is to not impose such a high maintenance burden. It's a smell when a lot of extra code has to be introduced and maintained in a codebase just to work around false positive bugs in a linter. I wonder if the best way forward here is to open a BPO feature request for >>> dc = ast.parse('foo() # mypy: ignore', directive_comments=True).directive_comments[0]
>>> dc.namespace
'mypy'
>>> dc.directive
'ignore' This would be more general than the current |
Yeah, if something like that was added to I'm sure you've considered this and found it inadequate, but I will note that turning off settings equivalent to mypy's |
I don't think we should introduce complexity into the |
That's a good suggestion, but we'd need to coordinate with other type checkers for OP's use case to be served (since e.g. currently Pyright treats |
I actually had the same idea a couple weeks ago. (Note this GitHub bug affects that comment link, in case you want to read it in the context of the full discussion, which is relevant to this issue. For now I'll just quote a small excerpt.)
Even though I proposed that syntax as an alternative to FWIW, the As long as we're brainstorming other solutions here, it's worth at least mentioning this one: A simple, heuristic-based (e.g. regex) search for any lines containing |
Given that there is not a standardized behavior for these in the PEP I don't think we should be restricted by what choices Pyright makes. It would be a simple PR on Pyright end to ignore |
I would be opposed to checking for errors that are specific to other type checkers within pyright. PEP 484 is pretty clear — a I think it would be better for mypy to support a mypy-specific ignore comment like |
Thanks for your input, @erictraut, helpful to have it here. Wanted to mention one more point here: The description of this issue only mentions cases where mypy disagrees with another type checker due to a bug in one of them. However, mypy may also disagree with another type checker due to a deliberately different design decision. (See microsoft/pyright#3260 vs. #12390 for a recent example.) Since bugs are fixed in new releases hopefully more often than design decisions are overturned, this may add to the need for a mypy-specific suppression syntax (that can still be targeted at a specific line where the disagreement occurs, rather than globally -- globally suppressing all errors in a particular category can cause too much collateral damage when the category is broad). |
|
@gvanrossum I believe this issue is about silencing MyPy in particular. Unfortunately |
In case anyone's interested, I opened a Python-ideas thread and corresponding CPython issue for expanding |
@jab Did you know that |
mypy: ignore
comments to suppress mypy errorsmypy: ignore
inline comments to suppress mypy errors
@davidhalter, this issue calls for a way to tell mypy (and not other type checkers) to ignore a specific line, i.e. via an inline comment. Currently (Just added "inline" to the title of the issue to make this clearer without reading the description.) |
Feature
Mypy should allow using inline
mypy: ignore
comments, treating them exactly the same as the equivalenttype: ignore
comment.Pitch
Currently, mypy supports
type: ignore
comments for suppressing errors only on a given line. These are an essential tool for users hitting a bug in mypy to work around the bug.However,
type: ignore
comments affect other type checkers as well. When a bug in mypy is causing it to flag a line that other type checkers are not flagging, the user can't add atype: ignore
to that line without causing their other type checkers to complain about an unused ignore.If users could add a
mypy: ignore
comment instead, this would not affect other type checkers, and would make it self-evident that the line is only being flagged by mypy, and not other type checkers in use.Furthermore, since other type checkers now support similar comments just for them (e.g. Pyright now supports
pyright: ignore
comments), if this feature were implemented for mypy, users using multiple type checkers would have a clearer way of indicating what's going on on a given line:type: ignore
: All type checkers agree there's an error heremypy: ignore
: Only mypy thinks this is an error (false positive in mypy or false negative in others)pyright: ignore
: Only pyright thinks this is an error (false positive in pyright or false negative in others)etc.
In summary, this feature would make it easier for users to use mypy with other type checkers on the same codebase, to discover new type checker bugs when they disagree (I've already found and reported several this way myself), and could ultimately lead to type checkers becoming less buggy more quickly and agreeing with one another more often.
The text was updated successfully, but these errors were encountered: