wntrblm / nox

Flexible test automation for Python
https://nox.thea.codes
Apache License 2.0
1.31k stars 151 forks source link

Property to access venv_backend #796

Closed henryiii closed 7 months ago

henryiii commented 7 months ago

How would this feature be useful?

There are several ways to set the backend, like environment variable, command line, and with a fall-through list. However, it's hard to tell which backend you actually have inside the noxfile. It looks like (from #795) it might not even be possible to just check which("uv") to see if uv is present reliably. So I think being able to get the venv_backend string back out with the final selection would be useful.

Describe the solution you'd like

    @property
    def venv_backend(self) -> str:
        try:
            venv = self.virtualenv
        except ValueError:
            return "none"
        if isinstance(venv, PassthroughEnv):
            return "none"
        if isinstance(venv, CondaEnv):
            return venv.conda_cmd
        if isinstance(venv, VirtualEnv):
            return venv.venv_backend
        raise ValueError("Unsupported environment")

Or, actually, probably making it an abstract property would be better.

Describe alternatives you've considered

The .virtualenv property does give you the backend, but each backend is different and accessing the properties only defined on ProcessEnv subclasses doesn't really feel like public API.

I'm currently using:

    if getattr(session.virtualenv, "venv_backend", "") != "uv":
        session.install("uv")

Anything else?

It looks like prog @ . doesn't work for pip, so this will need to be based on the backend. :/

layday commented 7 months ago

I wanted this to skip my minimum version tests if another backend is used, since the arguments are uv-specific.