so, hey again everybody. I have no a example proje...
# general
f
so, hey again everybody. I have no a example project where I hit an exception when running tests. if I manually create a venv and run the tests with pytest, they work. the current error message from
pants test ::
is:
Copy code
pants test ::
09:00:12.68 [ERROR] Completed: Run Pytest - src/py/app/tests.py:tests - failed (exit code 2).
============================= test session starts ==============================
platform darwin -- Python 3.11.5, pytest-7.0.1, pluggy-1.0.0
rootdir: /private/var/folders/ll/98hnvg_n5l3by0224q_qybc40000gn/T/pants-sandbox-5tufoo
plugins: xdist-2.5.0, anyio-3.7.1, forked-1.6.0, cov-3.0.0
collected 0 items / 1 error

==================================== ERRORS ====================================
_____________________ ERROR collecting src/py/app/tests.py _____________________
ImportError while importing test module '/private/var/folders/ll/98hnvg_n5l3by0224q_qybc40000gn/T/pants-sandbox-5tufoo/src/py/app/tests.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
src/py/app/tests.py:1: in <module>
    from fastapi.testclient import TestClient
/Users/elmcrest/.cache/pants/named_caches/pex_root/venvs/ae0da83a0d64465b37a4d1ecad90e3717130f3f3/627db8b23588181684416f34c8d0a18060f35f65/lib/python3.11/site-packages/fastapi/testclient.py:1: in <module>
    from starlette.testclient import TestClient as TestClient  # noqa
/Users/elmcrest/.cache/pants/named_caches/pex_root/venvs/ae0da83a0d64465b37a4d1ecad90e3717130f3f3/627db8b23588181684416f34c8d0a18060f35f65/lib/python3.11/site-packages/starlette/testclient.py:16: in <module>
    import httpx
E   ModuleNotFoundError: No module named 'httpx'
- generated xml file: /private/var/folders/ll/98hnvg_n5l3by0224q_qybc40000gn/T/pants-sandbox-5tufoo/src.py.app.tests.py.tests.xml -
=========================== short test summary info ============================
ERROR src/py/app/tests.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.28s ===============================
you can find all the code here https://github.com/elmcrest/py_in_my_pants my goal with this project is to better understand how pants works 🙂
1
r
How are you defining fastapi in your requirements? I think you need
fastapi[all]
not just
fastapi
to access all the test related stuff and httpx
f
that might be an option, but I have added httpx in the requirements.txt
so there is no difference then
r
There is a difference. Pants reads import statements to infer dependencies. And if fastapi itself is importing httpx somewhere which isn't visible to pants, it won't infer those dependencies.
Same goes for any other transitive dependencies fastapi is importing internally
f
🤔 ah ok, that makes sense then I guess, thank you
let me try
great, that worked
ok, but this feels a little scary now to me ...
r
My explanation isn't 100% accurate. What I meant was pants can only infer those transitive dependencies which are explicitly mentioned in the package metadata. Now when there are extras like httpx/uvicorn that fastapi needs for some tests etc, you need to tell it pants that fastapi needs those extras using the extras syntax
f
ah, is this also the case mentioned in the docs where I can/have to add extra dependencies in BUILD
from the django example
r
Whenever you need sthing more than the minimal package, you need to use extra. You can test this without pants by creating a clean venv, install just fastapi and it should fail. Check the extra defined in pyproject.toml in the fastapi GitHub repo. https://github.com/tiangolo/fastapi/blob/master/pyproject.toml#L56 These are optional dependencies. So unless you explicitly ask for them, they won't be installed when you install fastapi locally. The screenshot you posted is about pytest related plugins though. It's similar but not the same. It's basically another package.
f
but locally things worked with a venv and just having
fastapi
in requirements.txt
probably as I use it also without
httpx
in the requirements at all
maybe not 😄
r
Pants feature is it only picks those dependencies and their transitive dependencies which are part of the import statements. So if you say
Copy code
from fastapi.testclient import TestClient
It will only use fastapi and the associated transitive dependencies when running this test file and it assumes that any other transitive dependencies in this package are included inside fastapi. If the package doesn't properly define transitive dependencies then pants will complain like the error you got. TestClient comes from httpx which pants doesn't know since it's not defined as transitive dependencies for
fastapi
but as extras
fastapi[all]
.
f
ok, I think I got that ... but how can I solve this if a package maintainer didn't properly define dependencies and there is no extras as well?
I run into this though
Copy code
pants test ::
12:13:45.29 [ERROR] 1 Exception encountered:

Engine traceback:
  in `test` goal

ValueError: The explicit dependency `src/py:reqs#httpx` of the target at `src/py:reqs#fastapi` does not provide enough address parameters to identify which parametrization of the dependency target should be used.
Target `src/py:reqs` can be addressed as:
  * src/py:reqs
  * src/py:reqs#fastapi
  * src/py:reqs#pytest
  * src/py/requirements.txt:reqs
maybe that's worth a new thread? ... I don't really understand the error message tbh ...
ok, got it ... the hint was hidden in the requirements tab in the docs. so adding`httpx` (again) to requirements.txt was needed. unexpected error message though 🤷
anyways, works! 🎉