Is Pants able to infer dependancies between python...
# general
c
Is Pants able to infer dependancies between python test files? I have the structure like this:
src
|_ python
|_ package
tests
|_python
|_package
- test_1.py
- test_2.py
- BUILD
With a import in test_2.py like
from test_1 import function
However the test fails this import unless I explicitly provide python_tests the source for
tests/python/package
.
python_tests(name=“tests”, sources=[“tests/python/package”])
Strangely while the above works, it gives a Unmatched glob warning ([WARN] Unmatched glob from tests/python/package:tests’s
sources
field: “tests/python/package/tests/python/package”
r
I think you need to define a
python_test_utils
and then make that as dependency
👍 1
But yeah you still need explicit dependency it seems
c
Thanks @refined-addition-53644 An explicit dependancy on
python_tests
back to itself (
tests/python/package:tests
) did the trick. Imports now work, and no more warnings. python_test_utils looks more like it would be more appropriate for test utility files such as stub files (*.pyi) and conftest.py than actual test files.
b
👋 Howdy! OK a few things here 😅 The
sources
field defines which files the generator target should use for generating targets (It's also relative to the build dir). So
sources=["tests/python/package"]
is saying there is one Python file named
tests/python/package
. Then it warns because there is no file at that path. Usually you don't need to change it, as the default https://www.pantsbuild.org/docs/reference-python_tests#codesourcescode is usually correct
According to the code, I think Pants should be able to infer a dependency from one test to another. But I also wanna say I've seen similar behavior 🤔
Lastly, instead of adding a dependency from the tests generator to itself (what you're doing is making every test file depend on every other test file, which loses you fine-grained invalidation, and some perf) you should use the
overrides
field to augment the dependencies of the single test file. See https://www.pantsbuild.org/docs/reference-python_tests#codeoverridescode
👍 1
h
Yeah I think Pants should be able to infer this dep, so I am curious why it isn't, but importing from another test is not a common pattern. Is
function
an actual test function or a helper function?
c
It’s a helper function. I’ve got another problem that might be related. The code being tested is in a regular package structure (has a init.py). The test code is just a bunch of modules. If I make it a regular package by dropping in a init.py into the directory it breaks the imports of actual code.
src
|_ python
|_ package
|_file_3.py
- init.py
tests
|_python
|_package
- test_1.py
- test_2.py
- BUILD
test_1.py contains a wrapper around a function imported
from package.file_3
in test_2.py I’m trying to import that wrapper
Now I think this is expected right since the package name for both the tests and the code will become
package
. Should the tests/python/package directory name be changed?
h
So generally we'd recommend breaking a reusable helper function out into its own file, which is owned by a
python_test_utils
target, rather than have it be owned by a
python_tests
, since you're not using it as a test itself
Yeah, you've introduced a namespace package, so the two `__init__.py`s have to account for that. This isn't a Pants thing, but a Python thing (google "python namespace package" for details)
It's common to name
package
something like
package_tests
to avoid this problem, since introducing a namespace package has its own complications, and isn't worth doing just for tests I think
👍 1
c
I thought that might be the case, thanks for confirming!