Hi there! Just a quick question. Let's say I want ...
# general
n
Hi there! Just a quick question. Let's say I want my code to support Python 3.8-3.10. I set up GitHub workflow to run with these versions. What should I set in
pants.toml
to get all tools (most importantly mypy) and tests running with the specific version of Python? Is that possible? I would like to catch problems that are specific for a specific version of Python. From docs I'm not sure whether this is possible with mypy. The repo is https://github.com/5G-ERA/era-5g-client
So far, I've used Pants only for repo, supporting just one Python version. That was easy... :-)
r
Hey if you search for “pants python multiple version” on this channel, you see some previous discussions e.g. https://pantsbuild.slack.com/archives/C046T6T9U/p1625087433097500 https://pantsbuild.slack.com/archives/C046T6T9U/p1670362194370779
👍 1
n
I defined multiple
python_sources
with different interpreter_constraints, which, as I believe, works fine. But I'g getting warnings like "Pants cannot safely infer a dependency because more than one target owns this module, so it is ambiguous which to use". Is it safe to ignore, or should I do it differently?
Copy code
python_sources(
    name="lib",
    skip_mypy=True,
    skip_black=True,
    skip_flake8=True,
)

python_sources(name="lib_py38", interpreter_constraints=["==3.8.*"])
python_sources(name="lib_py39", interpreter_constraints=["==3.9.*"])
python_sources(name="lib_py310", interpreter_constraints=["==3.10.*"])
r
Hey as posted in the shared thread, you need to do this with the tests.
Copy code
# pants-plugins/macros.py
def python_tests_global(name, **kwargs):
    kwargs.pop("interpreter_constraints", None)

    python_tests(
        name=f"{name}_py369",
        interpreter_constraints=["==3.6.9"],
        **kwargs,
    )

    python_tests(
        name=f"{name}_py385",
        interpreter_constraints=["==3.8.5"],
        **kwargs,
    )
You can first test it without macro like you have done with the
python_sources
currently and later move it to a macro
Although after reading this, I think what you are doing should work also. I think you need to remove this and leave the rest as it.
Copy code
python_sources(
    name="lib",
    skip_mypy=True,
    skip_black=True,
    skip_flake8=True,
)
Currently your sources are being owned by
lib
and one or more of
lib_py38, lib_py39, lib_py310
based on whatever is your default
interpreter_constraints
Although doing it at test level makes more since that’s how you are going to check if the code works with different versions of python or not.
c
fwiw you can use
parametrize
instead of the macro https://www.pantsbuild.org/docs/targets#parametrizing-targets Snippet from the docs:
Copy code
# Creates four targets:
#
#    example:tests@interpreter_constraints=py2,resolve=lock-a
#    example:tests@interpreter_constraints=py2,resolve=lock-b
#    example:tests@interpreter_constraints=py3,resolve=lock-a
#    example:tests@interpreter_constraints=py3,resolve=lock-b

python_test(
    name="tests",
    source="tests.py",
    interpreter_constraints=parametrize(py2=["==2.7.*"], py3=[">=3.6"]),
    resolve=parametrize("lock-a", "lock-b"),
)
💯 1
h
Yeah, you probably want to parameterize here
I don't recall what happens with dep inference under parametrization
How it treats the ownership ambiguity, that is
n
I tried
parametrize
, and it works, but... 🙂 I probably need to specify dependencies everywhere then...
c
no, not dependencies, but resolve(s).. which you can do using
__defaults__
https://www.pantsbuild.org/docs/targets#field-default-values
👍 1