Skip to content

Commit 12f6268

Browse files
authored
Add py_dot_ver and py_impl (#2716)
Resolves #2640
1 parent b8b0803 commit 12f6268

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

docs/changelog/2640.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add ``py_dot_ver`` and ``py_impl`` constants to environments to show the current Python implementation and dot version
2+
(e.g. ``3.11``) for the current environment. These can be also used as substitutions in ``tox.ini`` - by
3+
:user:`gaborbernat`.

docs/faq.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ tox 4 - removed tox.ini keys
7979
| ``distdir`` | Use the ``TOX_PACKAGE`` environment variable.|
8080
+--------------------------+----------------------------------------------+
8181

82+
tox 4 - basepython not resolved
83+
+++++++++++++++++++++++++++++++
84+
The base python configuration is no longer resolved to ``pythonx.y`` format, instead is kept as ``py39``, and is
85+
the virtualenv project that handles mapping that to a Python interpreter. If you were using this variable we recommend
86+
moving to the newly added ``py_impl`` and ``py_dot_ver`` variables, for example:
87+
88+
.. code-block:: ini
89+
90+
deps = -r{py_impl}{py_dot_ver}-req.txt
91+
8292
tox 4 - substitutions removed
8393
+++++++++++++++++++++++++++++
8494
- The ``distshare`` substitution has been removed.

src/tox/tox_env/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def environment_variables(self) -> dict[str, str]:
323323

324324
result = self._load_pass_env(pass_env)
325325
# load/paths_env might trigger a load of the environment variables, set result here, returns current state
326-
self._env_vars, self._env_vars_pass_env, set_env.changed = result, pass_env, False
326+
self._env_vars, self._env_vars_pass_env, set_env.changed = result, pass_env.copy(), False
327327
# set PATH here in case setting and environment variable requires access to the environment variable PATH
328328
result["PATH"] = self._make_path()
329329
for key in set_env:

src/tox/tox_env/python/api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def version_no_dot(self) -> str:
4040
def impl_lower(self) -> str:
4141
return self.implementation.lower()
4242

43+
@property
44+
def version_dot(self) -> str:
45+
return f"{self.version_info.major}.{self.version_info.minor}"
46+
4347

4448
class Python(ToxEnv, ABC):
4549
def __init__(self, create_args: ToxEnvCreateArgs) -> None:
@@ -81,6 +85,14 @@ def validate_base_python(value: list[str]) -> list[str]:
8185
desc="python executable from within the tox environment",
8286
value=lambda: self.env_python(),
8387
)
88+
self.conf.add_constant("py_dot_ver", "<python major>.<python minor>", value=self.py_dot_ver)
89+
self.conf.add_constant("py_impl", "python implementation", value=self.py_impl)
90+
91+
def py_dot_ver(self) -> str:
92+
return self.base_python.version_dot
93+
94+
def py_impl(self) -> str:
95+
return self.base_python.impl_lower
8496

8597
def _default_pass_env(self) -> list[str]:
8698
env = super()._default_pass_env()

tests/session/cmd/test_show_config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def test_show_config_unused(tox_project: ToxProjectCreator) -> None:
7272
assert "\n# !!! unused: magic, magical\n" in outcome.out
7373

7474

75+
def test_show_config_py_ver_impl_constants(tox_project: ToxProjectCreator) -> None:
76+
tox_ini = "[testenv]\npackage=skip\ndeps= {py_impl}{py_dot_ver}"
77+
outcome = tox_project({"tox.ini": tox_ini}).run("c", "-e", "py", "-k", "py_dot_ver", "py_impl", "deps")
78+
outcome.assert_success()
79+
py_ver = ".".join(str(i) for i in sys.version_info[0:2])
80+
impl = sys.implementation.name
81+
assert outcome.out == f"[testenv:py]\npy_dot_ver = {py_ver}\npy_impl = {impl}\ndeps = {impl}{py_ver}\n"
82+
83+
7584
def test_show_config_exception(tox_project: ToxProjectCreator) -> None:
7685
project = tox_project(
7786
{

0 commit comments

Comments
 (0)