When using PyLint i can put a comment after an import to ignore an import failure of that specific import as below:
from airflow.models import BaseOperator # pylint: disable=import-error
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!
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:
# pyright: reportMissingImports=false, reportUnusedVariable=warning, reportUntypedBaseClass=error
So the correct way to suppress the improt warnings would be to do the below:
from airflow import DAG # type: ignore
If so, apologies!
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:
# pyright: reportMissingImports=false
Ok, that's great - thanks.
I didn't know if there was a feature for suppressing the exact warning/error on a particular line, but for my particular use case this covers it.
Thanks again for your help!
Josh
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.
I thought it was # noqa, at least it's what I've seen in a lot of projects (but I'm not that familiar with Python).
Perhaps it could help to have a note in the README? (even if it's from a standard)
Anyway, thanks! Very useful!
noqa is something we supported in MPLS as it was used by a few linters, but as Pylance is essentially a type checker, it started with # type: ignore comments and we haven't ported over # noqa support quite yet (but is in the backlog to get to parity).
noqa is not part of the Python standard whereas # type: ignore is. I think we should stick to standards and encourage their use.
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 # pyright: but for just that line.
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 # type: ignore at the end of the line on which Pylance reports MissingImport error, but it is still reported. Tried to reload VSCode, but it didn't help. Pylance is installed on remote development server and I use Remote SSH to connect.
VSCode version 1.55.0
Pylance 2021.3.4
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.
Most helpful comment
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.