glamorous-cpu-22971
01/17/2023, 3:59 PMpytest
in the dependencies
parameter of a python_tests
target?
python_tests(
dependencies = [
"tests/my/special/test:lib",
"3rdparty/python:pytest", # <-- I have test sources that `import pytest` so I think the type checker wants to see an explicit dependency stated here…
],
)
polite-garden-50641
01/17/2023, 4:05 PM./pants dependencies <path to python test file>
polite-garden-50641
01/17/2023, 4:06 PMhigh-yak-85899
01/17/2023, 4:18 PMpolite-garden-50641
01/17/2023, 4:19 PMhigh-yak-85899
01/17/2023, 4:22 PMpolite-garden-50641
01/17/2023, 4:25 PMhappy-kitchen-89482
01/17/2023, 4:28 PMhappy-kitchen-89482
01/17/2023, 4:28 PMglamorous-cpu-22971
01/17/2023, 4:29 PMglamorous-cpu-22971
01/17/2023, 4:30 PMglamorous-cpu-22971
01/17/2023, 4:31 PMglamorous-cpu-22971
01/17/2023, 4:33 PMpolite-garden-50641
01/17/2023, 4:36 PMrequirements.txt
and when creating deploy-able targets (like wheels, pex files, etc...) pants will only include the needed deps. (because of dep inference).
This is not how most stuff works in the python eco-system, since in a lot of projects you have multiple reqs files trying to manage the different things that needed to be done with each (generating docs, tests, linters, etc...) it is a PITA to manage those and Pants makes it so much easier.
one reqs file for the entire repo and pants only takes the actual things that are needed from the file.glamorous-cpu-22971
01/17/2023, 4:37 PMglamorous-cpu-22971
01/17/2023, 4:38 PMpants
2.14 does seem to bundle in pytest
at version 7.0.1 to run the tests, it’s not possible to reference this bundled version when you’re explicitly doing import pytest
inside your test sources (we’re doing it to import an assertion or something)glamorous-cpu-22971
01/17/2023, 4:39 PMpytest
if you use it in your test sources.polite-garden-50641
01/17/2023, 4:39 PMpolite-garden-50641
01/17/2023, 4:42 PMimport pytest
does if you need a specific version pytest (since you need specific APIs) you may need to also specify that version under the pytest options here: https://www.pantsbuild.org/docs/reference-pytest#versionpolite-garden-50641
01/17/2023, 4:43 PMglamorous-cpu-22971
01/17/2023, 4:43 PMdependencies
field of my python_tests
target.glamorous-cpu-22971
01/17/2023, 4:44 PMpolite-garden-50641
01/17/2023, 4:44 PMglamorous-cpu-22971
01/17/2023, 4:45 PMimport
is getting resolved successfully when the tests run - my type checker is complaining, that’s the issue.glamorous-cpu-22971
01/17/2023, 4:45 PMimport
statement with no corresponding dependency in the graph (or I assume that’s whats happening)polite-garden-50641
01/17/2023, 4:45 PM./pants dependencies
does pants see the pytest dep ?polite-garden-50641
01/17/2023, 4:46 PMglamorous-cpu-22971
01/17/2023, 4:46 PMglamorous-cpu-22971
01/17/2023, 4:46 PMpolite-garden-50641
01/17/2023, 4:47 PMpolite-garden-50641
01/17/2023, 4:47 PMglamorous-cpu-22971
01/17/2023, 4:48 PMglamorous-cpu-22971
01/17/2023, 4:48 PMpolite-garden-50641
01/17/2023, 4:49 PMglamorous-cpu-22971
01/17/2023, 4:50 PMglamorous-cpu-22971
01/17/2023, 4:50 PMglamorous-cpu-22971
01/17/2023, 4:50 PMglamorous-cpu-22971
01/17/2023, 4:50 PMglamorous-cpu-22971
01/17/2023, 4:51 PMglamorous-cpu-22971
01/17/2023, 4:56 PMpytest
in requirements.txt fixed the type checker warning.glamorous-cpu-22971
01/17/2023, 4:59 PMpants
scans the Python sources, if it finds an import xyz
statement, it creates a child node on the dependency graph with the label xyz
and then it looks in its python_requirements
to see if there are modules that map to xyz
? Is that correct?happy-kitchen-89482
01/17/2023, 5:09 PMrequirements-dev.txt
if you want (just wrap it in a python_requirements() target), but you don't need to - generally it's idiomatic to have a single requirements.txt with everything in it, since Pants will subset that for you. At test time it'll only take the dependencies needed for that test, at pex-building time it'll only bundle what is actually used into the pex, and so on.happy-kitchen-89482
01/17/2023, 5:09 PMhappy-kitchen-89482
01/17/2023, 5:10 PMimport xyz
to whatever provides xyz
. That can either be an internal .py file in your repo, or an external requirement.happy-kitchen-89482
01/17/2023, 5:10 PMpytest
in your requirements.txt
, pants will notice that it now has something that provides the pytest
module, and do the mapping.happy-kitchen-89482
01/17/2023, 5:11 PMhappy-kitchen-89482
01/17/2023, 5:11 PMhappy-kitchen-89482
01/17/2023, 5:11 PMhappy-kitchen-89482
01/17/2023, 5:12 PMhappy-kitchen-89482
01/17/2023, 5:32 PMglamorous-cpu-22971
01/17/2023, 5:55 PMDoes that all make sense?It really does make sense. I’m coming from recent context in Bazel so these concepts map pretty well.
glamorous-cpu-22971
01/17/2023, 5:56 PMwhen you say “adding the explicit dependency on pytest in requirements.txt fixed the type checker warning” do you mean just adding pytest to requirements.txt, or also adding an explicit dep on it from the python_tests target?It was just adding
pytest
to the requirements.txt file, once that resolved the issue I got an inkling of how the dependency inferencing must be working.glamorous-cpu-22971
01/17/2023, 5:56 PMpython_tests
target.glamorous-cpu-22971
01/17/2023, 5:56 PMhappy-kitchen-89482
01/17/2023, 6:57 PM