I'm trying to add a test to my package in order to...
# general
p
I'm trying to add a test to my package in order to check if specific python (sub-)packages are namespace packages. For the top level package, it would look like this:
Copy code
mypkg/
  subpkg1/
    __init__.py
  subpkg2/
    __init__.py
Notice the missing
__init__.py
under
mypkg
, which makes it an implicit namespace package (PEP-420). Now the test I wrote looks like this:
Copy code
# tests/test_namespace.py
import mypkg

def test_namespace():
    assert mypkg.__file__ is None
While this test runs successfully in pytest, it fails in the hermetic environment of pants, because the dependency on
mypkg
does not include any files. The directory
mypkg
itself is not created in the local environment, so
mypkg
is not importable. I know there are a couple of trivial solutions to this (
import mypkg.subpkg1
in the test, specifying a dependency manually or checking for the absence of
mypkg/__init__.py
in pre-commit hooks), but is there any solution in pants that I'm missing, e.g. some configuration option?
e
I don't think you're missing anything. I'd personally go with pre-submit hooks for this one or else a more integration style test like packaging up the associated
python_distribution
s (assuming there are such things in your repo) and then checking those all have a PEP-420 implicit ns package by whatever means up to just actually extracting or installing them in a venv and checking the layout.
1
👍 1