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

--ignore-missing-imports not working for all imports #208

Open
danparizher opened this issue Nov 3, 2023 · 14 comments
Open

--ignore-missing-imports not working for all imports #208

danparizher opened this issue Nov 3, 2023 · 14 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug needs PR

Comments

@danparizher
Copy link
Contributor

Using settings:

"mypy-type-checker.args": ["--ignore-missing-imports"],

Error still occurs for the requests package in this code snippet:

import requests
from bs4 import BeautifulSoup

response = requests.get("https://www.google.com", timeout=5)
soup = BeautifulSoup(response.text, "html.parser")

In IDE interface:

image

When executing from the CLI with the same flag, functions as expected:

C:\DT\General Programming>mypy scratchpad.py --ignore-missing-imports 
Success: no issues found in 1 source file
@karthiknadig
Copy link
Member

@danparizher Check the logs (Output > mypy type checker), if it is using dmypy switch it to using mypy by setting mypy-type-checker.preferDaemon setting to false. Make sure that the --ignore-missing-imports is being passed to mypy in the logs. If it is still getting reported then this is likely a bug on mypy itself.

@karthiknadig karthiknadig self-assigned this Nov 3, 2023
@danparizher
Copy link
Contributor Author

danparizher commented Nov 3, 2023

@karthiknadig It looks like --ignore-missing-imports is being passed into mypy in the logs, but you can see that the [import-untyped] error still appears.

Here are my logs upon copy + pasting the above code snippet into my editor:

2023-11-03 12:35:42.530 [info] [Trace - 12:35:42 PM] Sending notification 'textDocument/didChange'.
2023-11-03 12:35:43.583 [info] [Trace - 12:35:43 PM] Sending notification 'textDocument/didSave'.
2023-11-03 12:35:43.591 [info] [Trace - 12:35:43 PM] Received notification 'window/logMessage'.
2023-11-03 12:35:43.591 [info] c:\Users\dpari\AppData\Local\Programs\Python\Python312\python.exe -m mypy --no-color-output --no-error-summary --show-absolute-path --show-column-numbers --show-error-codes --no-pretty --ignore-missing-imports --show-error-end c:\DT\General Programming\scratchpad.py
2023-11-03 12:35:43.591 [info] [Trace - 12:35:43 PM] Received notification 'window/logMessage'.
2023-11-03 12:35:43.591 [info] CWD Server: c:\DT\General Programming
2023-11-03 12:35:44.058 [info] [Trace - 12:35:44 PM] Received notification 'window/logMessage'.
2023-11-03 12:35:44.058 [info] file:///c%3A/DT/General%20Programming/scratchpad.py :
c:\DT\General Programming\scratchpad.py:1:1:1:1: error: Library stubs not installed for "requests"  [import-untyped]
c:\DT\General Programming\scratchpad.py:1:1:1:1: note: Hint: "python3 -m pip install types-requests"
c:\DT\General Programming\scratchpad.py:1:1:1:1: note: (or run "mypy --install-types" to install all missing stub packages)
c:\DT\General Programming\scratchpad.py:1:1:1:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

2023-11-03 12:35:44.058 [info] [Trace - 12:35:44 PM] Received notification 'window/logMessage'.
2023-11-03 12:35:44.058 [info] file:///c%3A/DT/General%20Programming/scratchpad.py :
c:\DT\General Programming\scratchpad.py:1:1:1:1: error: Library stubs not installed for "requests"  [import-untyped]
c:\DT\General Programming\scratchpad.py:1:1:1:1: note: Hint: "python3 -m pip install types-requests"
c:\DT\General Programming\scratchpad.py:1:1:1:1: note: (or run "mypy --install-types" to install all missing stub packages)
c:\DT\General Programming\scratchpad.py:1:1:1:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

2023-11-03 12:35:44.059 [info] [Trace - 12:35:44 PM] Received notification 'textDocument/publishDiagnostics'.

@karthiknadig
Copy link
Member

This looks like a bug in mypy itself. We just run mypy as a subprocess.

@blakew
Copy link

blakew commented Nov 6, 2023

Had the same issue, but was able to resolve by setting "mypy-type-checker.reportingScope": "workspace" since I had a mypy.ini config file in my workspace folder that wasn't being honored since this is set to "file" by default.

@danparizher
Copy link
Contributor Author

Had the same issue, but was able to resolve by setting "mypy-type-checker.reportingScope": "workspace" since I had a mypy.ini config file in my workspace folder that wasn't being honored since this is set to "file" by default.

This fixed it for me, thank you so much!

@Mutantpenguin
Copy link
Contributor

Mutantpenguin commented Nov 7, 2023

I want to add something to this discussion because we have a similar problem:

For us, mypy behaves in a strange way too when run through this extension, but not when running from the command line. In our case it has to do with allow_untyped_calls and the usage of type stubs. Both get completely ignored when mypy is used through the extension, but not when invoked from the command line.

Now comes the strange part: in both cases it really uses our own pyproject.toml. We know this because we use --verbose and see that it uses our file.
When run through the extension it seems to ignore only some of its settings. Some (like follow_imports for example) have an effect though. I don't have a complete list of what works and what not, I only know that some work and some not.

Setting "mypy-type-checker.reportingScope": "workspace" does not work for us.

@danparizher
Copy link
Contributor Author

I have also confirmed that "mypy-type-checker.reportingScope" : "workspace" works in some instances and not others.

@karthiknadig
Copy link
Member

The only thing this extension does is capture the output and parse it in a way that we can display in the UI. If running mypy from terminal results in the same error then this should be filed on mypy.

@karthiknadig
Copy link
Member

@Mutantpenguin Are you using dmypy or mypy? I recommedn disabling dmypy as it does not pickup settings the same way as mypy.

Another thing you could try is setting the config path via the mypy-type-checker.args and see if that helps. Yet, another option is to set mypy-type-checker.cwd to ${fileDirname}, which allows us to run mypy in the same directory as the file being linted.

@Mutantpenguin
Copy link
Contributor

I already use mypy and not dmypy (would love to use the latter but can't as it doesn't support follow_imports = "silent").

Specifying a config via --config-file=../nyx/pyproject.toml in mypy-type-checker.args doesn't make a difference. Especially since it's using the file already.

When using ${fileDirname} in mypy-type-checker.cwd the server can't be started:

2023-11-07 22:48:50.635 [error] Server: Start failed: Launching server using command...

@danparizher
Copy link
Contributor Author

I already use mypy and not dmypy (would love to use the latter but can't as it doesn't support follow_imports = "silent").

Specifying a config via --config-file=../nyx/pyproject.toml in mypy-type-checker.args doesn't make a difference. Especially since it's using the file already.

When using ${fileDirname} in mypy-type-checker.cwd the server can't be started:

2023-11-07 22:48:50.635 [error] Server: Start failed: Launching server using command...

I can confirm this issue. When using ${fileDirname} in mypy-type-checker.cwd the server can't be started

@karthiknadig karthiknadig added bug Issue identified by VS Code Team member as probable bug needs PR and removed triage-needed labels Nov 7, 2023
@karthiknadig
Copy link
Member

karthiknadig commented Nov 7, 2023

I just pushed 2023.7.13112243 (pre-release) which should have the fix for ${fileDirname}.

I am not really sure what is the difference between how the extension runs vs CLI. The extension uses the workspace directory as the CWD, and launches mypy as a subprocess using subprocess.run. The only difference I can think of can be from MYPYPATH or PYTHONPATH.

@danparizher If you copy paste the command from the logs and run from your terminal from the same CWD as in the logs do you still see the difference?

> c:\Users\dpari\AppData\Local\Programs\Python\Python312\python.exe -m mypy --no-color-output --no-error-summary --show-absolute-path --show-column-numbers --show-error-codes --no-pretty --ignore-missing-imports --show-error-end "c:\DT\General Programming\scratchpad.py"

@danparizher
Copy link
Contributor Author

@karthiknadig running that command with the above code snippet yields the following output:

c:\DT\General Programming\scratchpad.py:1:1:1:1: error: Library stubs not installed for "requests"  [import-untyped]
c:\DT\General Programming\scratchpad.py:1:1:1:1: note: Hint: "python3 -m pip install types-requests"
c:\DT\General Programming\scratchpad.py:1:1:1:1: note: (or run "mypy --install-types" to install all missing stub packages)
c:\DT\General Programming\scratchpad.py:1:1:1:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

If I only have "mypy-type-checker.args": ["--ignore-missing-imports"] for my args, the error is not reported:

2023-11-07 18:40:11.024 [info] [Trace - 6:40:11 PM] Sending notification 'textDocument/didChange'.
2023-11-07 18:40:11.024 [info] [Trace - 6:40:11 PM] Sending notification 'textDocument/didChange'.
2023-11-07 18:40:12.030 [info] [Trace - 6:40:12 PM] Sending notification 'textDocument/didSave'.
2023-11-07 18:40:12.031 [info] [Trace - 6:40:12 PM] Sending notification 'textDocument/didSave'.
2023-11-07 18:40:12.033 [info] [Trace - 6:40:12 PM] Received notification 'window/logMessage'.
2023-11-07 18:40:12.033 [info] c:\Users\dpari\AppData\Local\Programs\Python\Python311\python.exe -m mypy --no-color-output --no-error-summary --show-absolute-path --show-column-numbers --show-error-codes --no-pretty --ignore-missing-imports --show-error-end c:\DT\General Programming\scratchpad.py
2023-11-07 18:40:12.033 [info] [Trace - 6:40:12 PM] Received notification 'window/logMessage'.
2023-11-07 18:40:12.033 [info] CWD Server: c:\DT\General Programming
2023-11-07 18:40:12.033 [info] [Trace - 6:40:12 PM] Received notification 'window/logMessage'.
2023-11-07 18:40:12.034 [info] c:\Users\dpari\AppData\Local\Programs\Python\Python311\python.exe -m mypy --no-color-output --no-error-summary --show-absolute-path --show-column-numbers --show-error-codes --no-pretty --ignore-missing-imports --show-error-end c:\DT\General Programming\scratchpad.py
2023-11-07 18:40:12.034 [info] [Trace - 6:40:12 PM] Received notification 'window/logMessage'.
2023-11-07 18:40:12.034 [info] CWD Server: c:\DT\General Programming
2023-11-07 18:40:12.806 [info] [Trace - 6:40:12 PM] Received notification 'window/logMessage'.
2023-11-07 18:40:12.807 [info] file:///c%3A/DT/General%20Programming/scratchpad.py :

2023-11-07 18:40:12.807 [info] [Trace - 6:40:12 PM] Received notification 'window/logMessage'.
2023-11-07 18:40:12.807 [info] file:///c%3A/DT/General%20Programming/scratchpad.py :

2023-11-07 18:40:12.807 [info] [Trace - 6:40:12 PM] Received notification 'textDocument/publishDiagnostics'.
2023-11-07 18:40:12.807 [info] [Trace - 6:40:12 PM] Received notification 'textDocument/publishDiagnostics'.

@Mutantpenguin
Copy link
Contributor

I think I fixed the problem for me, but I don't think this is a general fix.

In my case I am using a multiroot workspace where the other projects are also installed as editable packages in the corresponding venv.

I was using python.analysis.extraPaths for these others projects (I don't think this was ever necessary at all) because I expected that it only has an effect on Pylance (since the setting belongs to this extension). This is not the case.

When I am using python.analysis.extraPaths, the mypy extension behaves differently and I can reproduce this at will.

I am unsure if this is a good idea, but if it stays it needs to be explained somewhere that it has an effect on mypy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug needs PR
Projects
None yet
Development

No branches or pull requests

4 participants