I’m running into an issue where having multiple fi...
# general
g
I’m running into an issue where having multiple files @
<pants_respective_source>/tests/conftest.py
prevents MyPy checks from running - I’m wondering if this is a pants bug / mypy issue / user error. More details and git example in 🧡
Copy code
packages/package-2/tests/conftest.py: error: Duplicate module named "conftest" (also at "packages/package-1/tests/conftest.py")
Directory Structure:
Copy code
β”œβ”€β”€ .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:
Copy code
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",
    ],
)
Error:
Copy code
❯ ./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)
b
We've structured our code to have an extra layer within the
tests
directory, e.g.
package-1/tests/_*test_package_1*_/conftest.py
(plus an adjacent
__init__.py
), so that mypy sees
test_package_1.conftest
and
test_package_2.conftest
rather than just
conftest
. This is a bit clunky though, and we may change it. I believe the pants project itself puts tests in with source, e.g. within
package-1/package_1/
there's
foo.py
,
foo_test.py
,
conftest.py
h
What is the relevant source root? (see
./pants roots
for a list of them as currently configured)
g
Copy code
packages/package-1
packages/package-2
so AFAICT there’s two duplicate modules:
tests.conftest
regardless of whether I place an
__init__.py
or pass a combination of
--explicit-package-bases
/
--package-namespace
h
Yes, I'm wondering if this is causing issues
To confirm, what happens if you rename one of them to
tests1.conftest
?
g
Once you change the name of one of the conftest files there’s no longer a namespace collision and mypy runs fully without being blocked. I also confirmed with a duplicate test file name of
packages/package-1/tests/test_generic_name.py
and
packages/package-2/tests/test_generic_name.py
h
Cool, so we know what the issue is