Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog/2640.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add ``py_dot_ver`` and ``py_impl`` constants to environments to show the current Python implementation and dot version
(e.g. ``3.11``) for the current environment. These can be also used as substitutions in ``tox.ini`` - by
:user:`gaborbernat`.
10 changes: 10 additions & 0 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ tox 4 - removed tox.ini keys
| ``distdir`` | Use the ``TOX_PACKAGE`` environment variable.|
+--------------------------+----------------------------------------------+

tox 4 - basepython not resolved
+++++++++++++++++++++++++++++++
The base python configuration is no longer resolved to ``pythonx.y`` format, instead is kept as ``py39``, and is
the virtualenv project that handles mapping that to a Python interpreter. If you were using this variable we recommend
moving to the newly added ``py_impl`` and ``py_dot_ver`` variables, for example:

.. code-block:: ini

deps = -r{py_impl}{py_dot_ver}-req.txt

tox 4 - substitutions removed
+++++++++++++++++++++++++++++
- The ``distshare`` substitution has been removed.
Expand Down
2 changes: 1 addition & 1 deletion src/tox/tox_env/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def environment_variables(self) -> dict[str, str]:

result = self._load_pass_env(pass_env)
# load/paths_env might trigger a load of the environment variables, set result here, returns current state
self._env_vars, self._env_vars_pass_env, set_env.changed = result, pass_env, False
self._env_vars, self._env_vars_pass_env, set_env.changed = result, pass_env.copy(), False
# set PATH here in case setting and environment variable requires access to the environment variable PATH
result["PATH"] = self._make_path()
for key in set_env:
Expand Down
12 changes: 12 additions & 0 deletions src/tox/tox_env/python/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def version_no_dot(self) -> str:
def impl_lower(self) -> str:
return self.implementation.lower()

@property
def version_dot(self) -> str:
return f"{self.version_info.major}.{self.version_info.minor}"


class Python(ToxEnv, ABC):
def __init__(self, create_args: ToxEnvCreateArgs) -> None:
Expand Down Expand Up @@ -81,6 +85,14 @@ def validate_base_python(value: list[str]) -> list[str]:
desc="python executable from within the tox environment",
value=lambda: self.env_python(),
)
self.conf.add_constant("py_dot_ver", "<python major>.<python minor>", value=self.py_dot_ver)
self.conf.add_constant("py_impl", "python implementation", value=self.py_impl)

def py_dot_ver(self) -> str:
return self.base_python.version_dot

def py_impl(self) -> str:
return self.base_python.impl_lower

def _default_pass_env(self) -> list[str]:
env = super()._default_pass_env()
Expand Down
9 changes: 9 additions & 0 deletions tests/session/cmd/test_show_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ def test_show_config_unused(tox_project: ToxProjectCreator) -> None:
assert "\n# !!! unused: magic, magical\n" in outcome.out


def test_show_config_py_ver_impl_constants(tox_project: ToxProjectCreator) -> None:
tox_ini = "[testenv]\npackage=skip\ndeps= {py_impl}{py_dot_ver}"
outcome = tox_project({"tox.ini": tox_ini}).run("c", "-e", "py", "-k", "py_dot_ver", "py_impl", "deps")
outcome.assert_success()
py_ver = ".".join(str(i) for i in sys.version_info[0:2])
impl = sys.implementation.name
assert outcome.out == f"[testenv:py]\npy_dot_ver = {py_ver}\npy_impl = {impl}\ndeps = {impl}{py_ver}\n"


def test_show_config_exception(tox_project: ToxProjectCreator) -> None:
project = tox_project(
{
Expand Down