wntrblm / nox

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

Session attributes to represent the state of `reuse-existing-virtualenvs` and `no-install` #710

Open MicaelJarniac opened 1 year ago

MicaelJarniac commented 1 year ago

How would this feature be useful?

Sometimes, it might be useful to know the state of reuse-existing-virtualenvs and no-install, for example, to skip some run actions.

In my case, I want to run mypy --non-interactive --install-types when it's creating new virtualenvs and installing dependencies, but I want to run just mypy when it's reusing virtualenvs and not installing dependencies.

I can use run_always to have mypy --non-interactive --install-types run only when it's installing dependencies, but I couldn't find a way to then have mypy not run (other than running with --install-only), so if it's also installing dependencies, it'll end up running Mypy twice.

# noxfile.py
import nox

python_versions = ["3.9", "3.10", "3.11"]
constraints = ("-c", "constraints.txt")

@nox.session(python=python_versions)
def type_check_code(session: nox.Session) -> None:
    session.install(*constraints, ".[dev]")

    session.run_always("mypy", "--non-interactive", "--install-types")
    session.run("mypy")

Describe the solution you'd like

I'd like there to be session.reuse_existing_virtualenvs, session.no_install and session.install_only attributes that would represent the state of the --reuse-existing-virtualenvs, --no-install, and --install-only flags, respectively.

In my case, this would allow me to do the following:

# noxfile.py
import nox

python_versions = ["3.9", "3.10", "3.11"]
constraints = ("-c", "constraints.txt")

@nox.session(python=python_versions)
def type_check_code(session: nox.Session) -> None:
    session.install(*constraints, ".[dev]")

    if session.no_install:
        session.run("mypy")
    else:
        session.run_always("mypy", "--non-interactive", "--install-types")

Describe alternatives you've considered

No response

Anything else?

It might be useful to also have other flags represented by attributes. They could also be inside a session.flags object, like session.flags.no_install, to keep them more organized.

webartifex commented 1 month ago

I ran into a situation where I could use @MicaelJarniac 's suggestion. So, if anyone is interested in building this, there would be at least two users :)

As of now, I am using this:

if session._runner.reuse_existing_venv():
    ...