I'm importing pytest in `conftest.py`, and I get a...
# general
b
I'm importing pytest in
conftest.py
, and I get a warning that pants cannot infer owners for pytest fixtures (tried
from pytest import fixture
as well as
import pytest
) shouldn't that be built-in?
f
Do you have a `python_test_utils` target in the
BUILD
file in that directory?
If not, run
pants tailor
to create it.
(
conftest.py
is handled by that target type)
b
I do, looks like this:
Copy code
__defaults__(all=dict(skip_mypy=True))

python_tests()

python_test_utils(name="test_utils", dependencies=[":fixtures"])

files(name="fixtures", sources=["fixtures/**/*"])
f
You may need to use
resources
instead of
files
.
b
If it helps, the warning shows up when I try to see dependencies of one of the test files in the same directory
b
reading these docs, it seems that files is the way to go for fixtures. Tried resources nevertheless, still getting the warning
f
could you share the full error message?
b
If you're asking for the full stdout I'd need to anonymize it first. But here's the relevant bit at the very beginning:
Copy code
$ pants test python/cli/tests:
23:22:29.88 [WARN] Pants cannot infer owners for the following imports in the target python/cli/tests/conftest.py:test_utils:

  * pytest.fixture (line: 6)

If you do not expect an import to be inferrable, add # pants: no-infer-dep to the import line. Otherwise, see <https://www.pantsbuild.org/v2.16/docs/troubleshooting#import-errors-and-missing-dependencies> for common problems.
f
Are you using a resolve? And does the resolve have
pytest
in it?
b
for test, I'm just specifying a lockfile. Guess I'm using the default then?
f
And how is the lockfile generated?
And does the lockfile have
pytest
in it?
That error is occurring because Pants cannot find the
pytest
dependency.
b
Copy code
[GLOBAL]
pants_version = "2.16.0"

backend_packages = [
 "pants.backend.build_files.fmt.black",
 "pants.backend.python",
 "pants.backend.python.lint.black",
 "pants.backend.python.lint.isort",
 "pants.backend.python.lint.bandit",
 "pants.backend.python.typecheck.mypy",
]

[anonymous-telemetry]
enabled = false

[python]
interpreter_constraints = ["==3.9.*"]
enable_resolves = true

[python-bootstrap]
search_path = ["<PYENV>"]

[pytest]
version = "pytest~=7.3.1"
extra_requirements = ["pytest-mock~=3.10.0", "pytest-cov~=4.1.0"]
lockfile = "build_support/pytest.lock"

[black]
version = "black[jupyter]~=22.12.0"
lockfile = "build_support/black.lock"

[bandit]
config = "pyproject.toml"
version = "bandit[toml]~=1.7.5"
lockfile = "build_support/bandit.lock"
this is my pants.toml file, I'm then generating lockfiles with
pants generate-lockfiles
so the default lockfile (3rdparty/python/default.lock) does not contain pytest
f
that would be the issue then
note from https://github.com/pantsbuild/example-python/ that the user lockfile has pytest in it
if you want to import pytest in your first-party code it needs to be in the lockfile used for your user code
the pytest lockfile specified in your pants.toml is for running the pytest binary
(tool lockfile versus user lockfile)
b
ok, I'm getting there 😅
f
To be fair, this is a somewhat unnecessary and confusing distinction. Which is why 2.17 will allow Pants to use the version of pytest from your user lockfile for the tool invocation.
I think I saw that as a feature but would have to go check the changelog to confirm.
so 2.17 will get rid of the distinction and be simpler
but for now, you will need to add pytest to the user lockfile
since it is being imported in user code
b
fair enough. Actually pytest is only used in tests, it should be a dev dependency IMHO
feels weird to add it as a main dependency, even if pants ignores it for packaging etc
f
So when Pants build a binary it only uses the applicable subset of the resolve.
So yeah it feels weird but just envision the lockfile as the "entire universe" of dependencies and Pants will subset it as necessary.
b
tried it, seems to work (weird or not :D). Many thanks for the help and explanations 🙇
I finally found where in the docs the issue is discussed (somewhat)
although discussed indirectly
b
right, I saw this earlier but clearly it didn't land 😄
f
well it only discusses merging tool/user lockfiles, it doesn't call out that any imports must be in the user lockfile.
I vaguely recall that 2.17 may make the warning message more informative.
You can also set a Pants option to make that warning into an error.
b
that warning led to failed tests and misleading error messages down the line, it deserves to be an error if you ask me