Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mypy error "Source file found twice under different module names" when using editable install

Tags:

python

mypy

mypy throws an error when I have an editable installation (pip install -e .) of my library. It works fine with the non-editable installation (pip install .).

I was able to reproduce it with a toy example, so here are the files:

.
├── src
│   └── my_ns
│       └── mylib
│           ├── __init__.py
│           ├── main.py
│           ├── py.typed
│           └── second.py
├── mypy.ini
└── pyproject.toml

main.py

def something() -> None:
    print("I am something")

second.py

from my_ns.mylib.main import something


def something_else() -> None:
    something()
    print("I am something else")

pyproject.toml

[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
name = "mylib"
requires-python = ">=3.10"
version = "0.1.0"

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
"*" = ["*py.typed"]

mypy.ini

[mypy]
namespace_packages = True
explicit_package_bases = True
exclude = (?x)(
    ^tests/  # ignore everything in tests directory
    | ^test/  # ignore everything in test directory
    | ^setup\.py$ # ignore root's setup.py
  )

my_ns is a namespace package, so it does by intention not include a __init__.py (and must remain a namespace).

This is the result when running mypy 1.10.0:

$ mypy --config-file mypy.ini .
src/my_ns/mylib/main.py: error: Source file found twice under different module names: "src.my_ns.mylib.main" and "my_ns.mylib.main"
Found 1 error in 1 file (errors prevented further checking)

How can I make mypy work with an editable install and support namespace packages?

like image 488
GenError Avatar asked Oct 13 '25 01:10

GenError


1 Answers

Add the following to your mypy.ini:

mypy_path = src

(Credit goes to Mario Ishac, who got this from a GitHub issue comment.)

like image 134
2 revsInSync Avatar answered Oct 14 '25 18:10

2 revsInSync