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

BUG: Incorrect Warnings for 'Unnecessary "# type: ignore" comment' #2943

Closed
adam-grant-hendry opened this issue Jun 15, 2022 · 2 comments
Closed

Comments

@adam-grant-hendry
Copy link

adam-grant-hendry commented Jun 15, 2022

Environment data

  • Language Server version: v2022.6.11 (Pre-Release)
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.10 (CPython implementation)

Code Snippet

After placing a type ignore on the following pylance error

image

pylance incorrectly warns that the type ignore is redundant:

image

The same behavior occurs with all errors attempted to be ignored, regardless of whether the syntax is # pyright: ignore or # type: ignore.

Repro Steps

mypy 0.961 is being run in vscode 1.68.1. The project uses a pyproject.toml with the following mypy settings:

[tool.mypy]
python_executable = ".venv/Scripts/python.exe"
python_version = "3.8"
disallow_untyped_defs = true
warn_return_any = true
warn_unused_configs = true
warn_unused_ignores = true
warn_redundant_casts = true
show_error_codes = true
exclude = [
    '.venv/',
    'logs',
    'build',
    'dist'
]
files = [
    'myproj',
    'docs',
    'resources',
    'tests'
]
no_pretty = true
show_column_numbers = true

and a user (global) level settings.json configured to run the dmypy.exe daemon:

{
    "mypy.dmypyExecutable": "${workspaceFolder}/.venv/Scripts/dmypy.exe",
    "python.analysis.diagnosticSeverityOverrides": {
        "reportPrivateImportUsage": "information",
        "reportUnnecessaryTypeIgnoreComment": "information"
    },
    "python.defaultInterpreterPath": "${workspaceFolder}/.venv/Scripts/python.exe",
    "python.linting.mypyArgs": [
        "--config-file",
        "pyproject.toml"
    ],
    "python.linting.mypyEnabled": true,
    "python.linting.mypyPath": "${workspaceFolder}/.venv/Scripts/mypy.exe",
}

Expected behavior

pylint only warns about unnecessary type ignores when removing them will actually not yield an error (i.e. they are actually unnecessary).

Update

This may not be a bug if this is expected behavior when

"python.analysis.diagnosticSeverityOverrides": {
    "reportUnnecessaryTypeIgnoreComment": "information"
},

is used. However, perhaps these are conflicting settings when used in conjunction?

@erictraut
Copy link
Contributor

This is working as designed. As with pylint, pyright emits reportUnnecessaryTypeIgnoreComment diagnostics only when removing that comment produces no diagnostics by pyright. Pyright has no way of knowing whether mypy will produce an error on a particular line, nor should it.

If you are running both mypy and pyright on your code base, you will need to contend with the inevitable differences between the two. Pyright supports a pyright-specific diagnostic suppression mechanism through the use of # pyright: ignore comments. Mypy unfortunately doesn't have an equivalent. Ideally, you would be able to use # mypy: ignore, but this is technically difficult for mypy to support because of its reliance on the Python parser. See this issue for details. Because of this, you will need to decide whether you want to use pyright or mypy as a type checker (rather than both), or you can disable reportUnnecessaryTypeIgnoreComment.

In any case, this is not a bug. Pyright and pylance are working as expected here.

@adam-grant-hendry
Copy link
Author

Thank you. I will disable reportUnnecessaryTypeIgnoreComment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants