Nox sessions with options
Project description
NoxOpt
Nox sessions with options!
Installation
It's just pip install noxopt
!
Basic Usage
Define a session with typed parameters:
from noxopt import NoxOpt, Session
nox = NoxOpt()
@nox.session
def add_numbers(session: Session, x: int, y: int) -> None:
session.log(x + y)
Now you can pass this session the declared option via the command line:
nox -s my-session -- --x 10 -- y 3
And you'll see the following output
nox > Running session my-session
nox > Creating virtual environment (virtualenv) using python in .nox/my-session
nox > 13
nox > Session my-session was successful.
Note that all options declared with the sessions of a NoxOpt
group must be consistent.
That is, if one session defined x: int
, another session in the same group cannot
define x: bool
instead.
Customizing Options
This time you're going to use some Annotated
metadata to customize your option:
from typing import Annotated, TypeAlias
from noxopt import NoxOpt, Option, Session
nox = NoxOpt()
@nox.session
def sum_numbers(
session: Session,
nums: Annotated[list[int], Option(nargs="*", type=int)],
) -> None:
session.log(sum(nums))
This time when you run it you can pass several of numbers:
nox -s sum-numbers -- --nums 10 3 26 4
And you'll see the following output
nox > Running session my-session
nox > Creating virtual environment (virtualenv) using python in .nox/my-session
nox > 43
nox > Session my-session was successful.
Note that the annotation for nums
should be understood in the following way:
# declare a type with metadata
Annotated[
# your normal type annotation
list[int],
# configure the option associated with the type annotation above
Option(nargs="*", type=int)
]
You'll find that Option
has nearly the same parameters as
argparse.add_argument
.
If you need to use a given option more than once you can do so by defining it as a variable:
from functools import reduce
from typing import Annotated, TypeAlias
from noxopt import NoxOpt, Option, Session
nox = NoxOpt()
Integers = Annotated[list[int], Option(nargs="*", type=int)]
@nox.session
def sum_numbers(session: Session, nums: Integers) -> None:
session.log(sum(nums))
@nox.session
def multiply_numbers(session: Session, nums: Integers) -> None:
session.log(reduce(lambda x, y: x * y, nums, 0))
Automatic Tags
An additional nicety of NoxOpt is that is can automatically create tags based on the
names of your sessions using the NoxOpt(auto_tag=True)
parameter. The idea behind this
parameter is that if you have a set of sessions with a common naming scheme like:
from noxopt import NoxOpt, Session
nox = NoxOpt(auto_tag=True)
@nox.session
def check_python_tests(session: Session) -> None:
...
@nox.session
def check_python_format(session: Session) -> None:
...
@nox.session
def check_javascript_tests(session: Session) -> None:
...
@nox.session
def check_javascript_format(session: Session) -> None:
...
NoxOpt will generate the following tags:
check
- run sessions begining withcheck
check-python
- run sessions begining withcheck-python
check-javascript
- run sessions begining withcheck-javascript
It does this by splitting every session name in the group on -
characters and creating
tags based on their common prefixes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file noxopt-0.0.6.tar.gz
.
File metadata
- Download URL: noxopt-0.0.6.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f96de01b755af7bf5cb23ecd04f9527900a9baea6eb946f319c997373ded43d |
|
MD5 | 310af045d1c6367672591126a681a20b |
|
BLAKE2b-256 | bdae38b784fb9e2148dce46766db9c7d53688e458aa79928d79608f57089df6f |
File details
Details for the file noxopt-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: noxopt-0.0.6-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 911e7878bdf2ee069439946278143af7161ea2b1f28a606b76118df25f9d16b9 |
|
MD5 | 289ca48188da4ec17d15127f63cef95e |
|
BLAKE2b-256 | 372e2ef995002d705809a49c95c5d5a6b100b3d18407c5e500e95f078b41d689 |