hatch icon indicating copy to clipboard operation
hatch copied to clipboard

Environment "path" does not support context formatting

Open conallprendergast opened this issue 1 year ago • 16 comments

When defining the "path" setting in an environment, it should I think be possible to use context formatting. For example, the {env_name} and {python} variables are not interpolated.

Steps to reproduce:

pyproject.toml

[tool.hatch.envs.venv]
path = ".venv/{python}"

[[tool.hatch.envs.venv.matrix]]
python = ["3.7", "3.8", "3.9", "3.10", "3.11"]
hatch env find venv.py3.8

Expected:

/path/to/project/dir/.venv/3.8

Actual

/path/to/project/dir/{python}

conallprendergast avatar Jun 15 '23 21:06 conallprendergast

{matrix:python} https://hatch.pypa.io/latest/config/environment/advanced/#context-formatting

ofek avatar Jun 15 '23 22:06 ofek

This is not working with my current configuration, with hatch 1.7.0

Oddly this does work in the scripts section

[tool.hatch.envs.venv.scripts]
display = "echo {matrix:python}"

conallprendergast avatar Jun 15 '23 22:06 conallprendergast

oh that option I think just doesn't support context formatting. can you explain what you are trying to do?

ofek avatar Jun 16 '23 00:06 ofek

I am trying to set the default location for virtual environments to be in the project dir, so that I can easily use hatch environments with IDEs I understand this can be set using the [dirs.env] in the hatch configuration, but I wanted to be able to set this on a project level

conallprendergast avatar Jun 16 '23 08:06 conallprendergast

What is the process for overriding config parameters that are shown in hatch config show ? Oddly enough adding various parameters to a hatch.toml in user home, and also in project root dir, is not working. Similarly, setting the following in pyproject.toml is also not working for me?

[tool.hatch]
shell = "foo"

conallprendergast avatar Jun 16 '23 09:06 conallprendergast

there is no way to do that at the project level, which I think makes sense because developers would want control over where potentially large directories are located

I can add this as a feature if you really want

ofek avatar Jun 16 '23 14:06 ofek

Ah I see. Makes sense yeah. I think it would make more sense to support context formatting in the env path option. What do you think?

conallprendergast avatar Jun 16 '23 14:06 conallprendergast

It would be nice if we can use home:uri variable as well

aeb-dev avatar Dec 18 '23 10:12 aeb-dev

That works already.

ofek avatar Dec 18 '23 14:12 ofek

This is not working, am I doing something wrong?

[tool.hatch.envs.default]
path = "{home:uri}/.virtualenvs"

aeb-dev avatar Dec 18 '23 15:12 aeb-dev

Sorry, I meant to say for options that support context formatting it works. This option still does not but I will fix that soon.

ofek avatar Dec 18 '23 15:12 ofek

Am I understanding correctly that this is a particular case of the environment config dictionary not supporting context formatting?

If that is so, I have a different use case where context formatting would be helpful. I am trying to use hatch-conda, which has a command option where you specify one of "conda", "mamba", or "micromamba". It would be helpful to be able to use context formatting here so I could use "mamba" for local development and "micromamba" for CI, controlled by environment variables.

jayqi avatar Feb 10 '24 17:02 jayqi

Implementing that particular request would be up to the plugin author.

ofek avatar Feb 10 '24 18:02 ofek

@ofek from following the code, it seems like plugin-specific config options still go through EnvironmentInterface.config.

https://github.com/pypa/hatch/blob/cfa05386f9dc4a029462a23a38d0885176efbaac/src/hatch/env/plugin/interface.py#L158-L165

Wouldn't both path and these other plugin-specific config options all end up supporting context formatting if the config property there returns config values with context formatting?

jayqi avatar Feb 10 '24 18:02 jayqi

True I guess I could do every option, although I'm thinking about if that is desirable in every circumstance. What do you think?

ofek avatar Feb 10 '24 19:02 ofek

Also looking for support of conditional formatting for path key. Currently I have to do like this:

[tool.hatch.envs.py38]
path = ".venvs/py38"
python = "3.8"

[tool.hatch.envs.py39]
path = ".venvs/py39"
python = "3.9"

[tool.hatch.envs.py310]
path = ".venvs/py310"
python = "3.10"

mikita-sakalouski avatar May 08 '24 13:05 mikita-sakalouski