-
Notifications
You must be signed in to change notification settings - Fork 765
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
Allow line level suppression of specific errors #196
Comments
For type checkers like Pylance, the standard way to suppress diagnostics is to use a "# type: ignore" comment at the end of the line. This behavior is documented in PEP 484. |
You can also disable or enable individual diagnostic rules within a file by placing a comment at the top of the file:
|
So the correct way to suppress the improt warnings would be to do the below: |
Yes, that will suppress all Pylance diagnostics on that line. Or if you'd prefer to suppress a diagnostic rule throughout the file, you could use:
|
Ok, that's great - thanks. |
I thought it was Perhaps it could help to have a note in the README? (even if it's from a standard) Anyway, thanks! Very useful! |
|
|
Are there any plans to implement line-level suppression of individual error types? I imagine something like # type: reportMissingImports=false, reportUnusedVariable=warning which would behave like |
We currently don't have plans for this, but I will reopen this issue and mark it as an enhancement so that others can vote on the issue if they want to see something like this implemented. |
Is this 100% supported? I've just tried to add VSCode version 1.55.0 |
If that doesn't work, then that's a bug with our current ignore support and would be worth a new issue if you can reproduce it. |
+1 for line level suppression of individual errors! For compatibility with mypy, please consider If there's a proposal to unify error codes with MyPy, that would also be great! |
Let'me add to the discussion with a current use case of mine: try:
import orjson as json
except ImportError:
import json
return json.dumps Pylance complains that:
Adding a |
Using import statements within a from typing import TYPE_CHECKING
try:
import orjson as json
except ImportError:
if not TYPE_CHECKING:
import json By including the |
That's a good tip, in particular because adding support for I ended up returning from inside the try/except because I needed a closure to decode orjson's dumps anyway, which also works if anyone is curious |
With pylint and other linters that I use, you can use a comment on a previous line to just suppress one specific instance of a report. Putting a comment at the end of a line will likely cause a line to then trip a line-too-long issue.
This does not seem to work with pylance. Disabling a setting is global no matter where I put it, at the end of a line, above the line, or below the line. Event this below does not work because the true setting causes these issues to be flagged. pyright: reportMissingModuleSource=falsefrom six.moves.configparser import ConfigParser pyright: reportMissingModuleSource=trueI am converting a project from python2/GTK-2 to python3/gtk-3 and need to be able to disable linting messages for issues that I will be fixing later so that I can get reports of more important issues or new issues that are being introduced in the conversion. |
Pylance isn't a "linter", at least by the traditional definition. Linters are concerned with code style issues like line length and naming conventions. Pylance doesn't concern itself with code style issues. If you want to enforce those types of issues, then pylint is a good choice. Pylance is built on pyright, which is a static type checker, so it follows the type checking standards laid out in PEP 484 and related specifications. PEP 484 specifies that I'll point out that there's almost always a better way to eliminate a type error than silencing it with a |
The "#type: ignore" at the end of the line is not working on a single line only as described earlier in this issue. It is operating on the entire file no matter where the comment is. This is on the pylance bundled with Visual Studio code version 1.58.2 using the python program. I know there is almost always a better way. In my case it would cause to much of a delay to do that as many of the issues will be resolved either by dropping python2 support or by future planed changes. |
I'm not able to repro this. Could you provide a code sample that exhibits this bug? Or if the code in question is available in a public github repo, I can take a look at it there. Another technique you might find useful... you can disable specific classes of errors at the project or file level. |
+1 for this feature When using a third-party library, i want that line to be ignored by pyright if it is partially Unknown |
Can you provide an example of a line of code where you would find valuable to ignore a specific type check error while still reporting others ? |
Not the OP, but I have an example:
These are specific for Blender. When using the python interpreter that comes with Blender, and setting some extra module paths using Kind of a niche use case, but still a valid one in my opinion (and I can imagine other users having similar problems every now and then). |
@javl, you can place a Another potential workaround that you might want to consider is to use local type stub files. If you create a file called from typing import Any
def __getattr__(name: str) -> Any: ... |
@erictraut |
I found out you can combine
The |
I'm trying to get rid of an "unused import" warning on a single line, and none of the solutions mentioned above work:
I have The reason I need this is because I'm using |
The message If it really bothers you that the text is grayed out, you could add a meaningless reference to the module, perhaps under an from typing import TYPE_CHECKING
if TYPE_CHECKING:
# Reference grequests so it is not flagged as unused
print(grequests) |
This is now implemented in the form of |
I've been waiting for this to be released. Very good news! Thanks for your hard work! |
This issue has been fixed in version 2022.3.4, which we've just released. You can find the changelog here: CHANGELOG.md |
|
It still does not support multi-comment ignores like most other linters. e.g. # pyright: ignore[reportPrivateImportUsage] # noqa: F401
Actually, the ignore works if on the same line as the offending line. Sometimes |
I am maybe missing something but I don't see a clear reference to the single-line suppression feature in that changelog. |
Line-level diagnostic suppression is documented here: https://microsoft.github.io/pyright/#/comments?id=line-level-diagnostic-suppression I also don't see an obvious reference to it in that changelog, but I don't think it's worth tracking it down or fixing the changelog since it was more than a year ago. |
What would you suggest about the imports or calls to libraries code that are un-typed by birth and don't have py.typed files etc? |
If you want the benefits of static type checking, then you'll need accurate type information for the libraries that you import. In some cases, you can find separate type stubs for that library. If they don't exist, you have the option of creating your own for the subset of the library that your program uses. If you don't want to do that, then you're limited to pyright's type inference. That may be sufficient in some cases. In other cases, you may need to give up on static type checking (set |
When using PyLint i can put a comment after an import to ignore an import failure of that specific import as below:
I can't find a way to do the same using PyLance. I've seen information on how to do it at a project/workspace level, but it's something that i'd only like to do for specific edge cases (such as for airflow imports on a windows machine without airflow installed)
Apologies if this is already implemented and i've missed the docs on the syntax!
The text was updated successfully, but these errors were encountered: