Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MicaelJarniac opened this issue May 12, 2023 · 2 comments

Comments

@MicaelJarniac
Copy link
Contributor

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
Copy link

webartifex commented Jul 23, 2024

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():
    ...

@ichard26
Copy link

As someone would benefit from these attributes being exposed, I took a stab at writing a patch. However, I have a design problem. While it's simplest to simply expose the True/False presence of --no-install, --reuse-existing-virtualenvs, and --install-only (and their aliases), that wouldn't match how Nox actually interprets these flags.

For example, if --no-install is passed but there is no virtual environment to use (with the default backend), the session.install("six") will still run. This is good UX, but a simplistic session.no_install would still return True. If this attribute was used to gate some pre-install building, that wouldn't happen and further installation steps could fail.

I'd prefer exposing the yes/no nature of whether session.run() and session.install() would execute or not (as IMO the goal of exposing these attributes to match nox's behaviour but for custom logic). Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants