20

In my python project, after upgrading mypy from 0.770 to 0.782 an error is received in files where there were previously no type errors:

my_pkg_name\__init__.py: error: Source file found twice under different module names: 'top_pkg.my_pkg_name' and 'my_pkg_name'
Found 1 error in 1 file (checked 1 source file)

I'm pretty sure this is related to Issue #8944 on mypy and the way which vscode-python executes mypy on the open files. I've tried adding various mypy flags (e.g. --namespace-packages, --no-namespace-packages) but this did not resolve the issue.

my_pkg_name does contain an __init__.py and so does top_pkg. With mypy==0.770 this was not a problem.

Looking at the extension's output this is how mypy is invoked:

> ~\.virtualenvs\OfflineSystem.38\Scripts\python.exe `
   c:\Users\***\.vscode\extensions\ms-python.python-2020.8.108011\pythonFiles\pyvsc-run-isolated.py mypy `
   --ignore-missing-imports --follow-imports=silent --show-column-numbers `
   d:\***\top_pkg\my_pkg_name\sub_pkg\my_file.py

Should change something in the mypy-related vscode settings for this to work?

stav
  • 1,497
  • 2
  • 15
  • 40

2 Answers2

21

I had a similar issue, but not via VSCode. The fix in my case was to remove an __init__.py file from a directory that was being included by adding it to the MYPYPATH, and so wasn't actually being treated as a module (so it shouldn't really have had the __init__.py file).

You said you tried adding the --namespace-packages flag, but I think you would need --no-namespace-packages to disable the new checker which might be causing your problem.

dshepherd
  • 4,989
  • 4
  • 39
  • 46
  • 1
    Thanks @dshepherd, unfortunately the `--no-namespace-packages` did not work either in my case. The exact same issue persists. I'll try and see if the new information you're giving here can help me out in any way and will post a solution if I find one. – stav Nov 10 '20 at 10:39
  • 7
    remove __init__.py from root directory worked for me. – Ziur Olpa Jun 07 '22 at 11:28
  • 3
    `--no-namespace-packages` worked for me. (Who can just delete their package's root `__init__.py` without consequence?) – conner.xyz Nov 21 '22 at 22:56
  • Much better answer than the original issue and the maintainer's message via link. – dijonkitchen Jul 12 '23 at 18:00
1

In my case adding an __init__.py helped as it was missing from the particular module.