quaint-telephone-89068
01/03/2023, 4:10 PMtests/conftest.py file MyPy will not run with the following error:
❯ ./pants check ::
14:58:16.01 [ERROR] Completed: Typecheck using MyPy - mypy failed (exit code 2).
packages/package-2/tests/conftest.py: error: Duplicate module named "conftest" (also at "packages/package-1/tests/conftest.py")
packages/package-2/tests/conftest.py: note: See <https://mypy.readthedocs.io/en/stable/running_mypy.html#mapping-file-paths-to-modules> for more info
packages/package-2/tests/conftest.py: note: Common resolutions include: a) using `--exclude` to avoid checking one of them, b) adding `__init__.py` somewhere, c) using `--explicit-package-bases` or adjusting MYPYPATH
Found 1 error in 1 file (errors prevented further checking)
AFAICT, both files occupy the same namespace (tests.conftest) which prevents all MyPy type checks from running. Making any of the changes recommended by MyPy (creating __init__.py files / pass a combination of --explicit-package-bases / --package-namespace) yields no results. Only once you change the names of one of these files will the type checks actually run.
This issue prevents us from using the magic of conftest.py unless we change our our monorepo's directory structure.
I've also confirmed the same issue with a duplicate test file name of packages/package-1/tests/test_generic_name.py and packages/package-2/tests/test_generic_name.py.
Example Directory Structure (also see the example repo):
├── .gitignore
├── 3rdparty
│ ├── BUILD
│ ├── python
│ │ └── default.lock
│ └── requirements.txt
├── README.md
├── packages
│ ├── package-1
│ │ ├── BUILD
│ │ ├── package_1
│ │ │ ├── __init__.py
│ │ │ └── foo.py
│ │ └── tests
│ │ ├── conftest.py
│ │ └── test_foo.py
│ └── package-2
│ ├── BUILD
│ ├── package_2
│ │ ├── __init__.py
│ │ └── baz.py
│ └── tests
│ ├── conftest.py
│ └── test_baz.py
├── pants
└── pants.toml
Example BUILD file:
python_sources(
name="lib",
sources=[
"package_1/**/*.py",
"package_1/*.py",
],
)
python_test_utils(name="conftest", sources=["tests/conftest.py"])
python_tests(
name="tests",
sources=[
"tests/**/test_*.py",
"tests/test_*.py",
],
dependencies=[
":lib",
":conftest",
],
)
Example Pants Roots:
❯ ./pants roots
packages/package-1
packages/package-2
Pants version
2.14.0
OS
MacOS
Additional info
• Slack Thread
pantsbuild/pants