Package: noxopt
NoxOptNoxsessions with options!InstallationIt's justpip install noxopt!Basic UsageDefine a session with typed parameters:fromnoxoptimportNoxOpt,Sessiongroup=NoxOpt()@group.sessiondefadd_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-smy-session----x10--y3And you'll see the following outputnox > 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 aNoxOptgroup must be consistent.
That is, if one session definedx: int, another session in the same group cannot
definex: boolinstead.Customizing OptionsThis time you're going to use someAnnotatedmetadata to customize your option:fromtypingimportAnnotatedfromnoxoptimportNoxOpt,Option,Sessiongroup=NoxOpt()@group.sessiondefsum_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-ssum-numbers----nums103264And you'll see the following outputnox > 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 fornumsshould be understood in the following way:# declare a type with metadataAnnotated[# your normal type annotationlist[int],# configure the option associated with the type annotation aboveOption(nargs="*",type=int)]You'll find thatOptionhas nearly the same parameters asargparse.add_argument.If you need to use a given option more than once you can do so by defining it as a
variable:fromfunctoolsimportreducefromtypingimportAnnotated,TypeAliasfromnoxoptimportNoxOpt,Option,Sessiongroup=NoxOpt()Integers=Annotated[list[int],Option(nargs="*",type=int)]@group.sessiondefsum_numbers(session:Session,nums:Integers)->None:session.log(sum(nums))@group.sessiondefmultiply_numbers(session:Session,nums:Integers)->None:session.log(reduce(lambdax,y:x*y,nums,0))Parametrizing SessionsIf want to use the@nox.parametrizedecorator with NoxOpt you'll need to explicitely declare which parameters should be
treated as command line options. This is done by annotating them withAnnotated[YourType, Option()]:fromtypingimportAnnotatedfromnoximportSession,parametrizefromnoxoptimportNoxOpt,Optiongroup=NoxOpt()@group.session@parametrize("num",[1,2,3])deflog_nums(session:Session,num:int,mult:Annotated[int,Option()])->None:session.log(num*mult)You could now run:nox-smultiply-nums----mult2And see the output:nox > Running session multiply-nums(num=1)
nox > Creating virtual environment (virtualenv) using python in .nox/multiply-nums-num-1
nox > 2
nox > Session multiply-nums(num=1) was successful.
nox > Running session multiply-nums(num=2)
nox > Creating virtual environment (virtualenv) using python in .nox/multiply-nums-num-2
nox > 4
nox > Session multiply-nums(num=2) was successful.
nox > Running session multiply-nums(num=3)
nox > Creating virtual environment (virtualenv) using python in .nox/multiply-nums-num-3
nox > 6
nox > Session multiply-nums(num=3) was successful.
nox > Ran multiple sessions:
nox > * multiply-nums(num=1): success
nox > * multiply-nums(num=2): success
nox > * multiply-nums(num=3): successCommon SetupNoxOpt allows you to add logic that runs before sessions in a group.fromnoxoptimportNoxOpt,Sessiongroup=NoxOpt()@nox.setupdefsetup(session:Session)->None:...# your setup logic here@group.sessiondefmy_session(session:Sesssion)->None:...# your session hereHere, thesetupfunction will run before all sessions in theNoxOptgroup. To
run setup only on specific sessions in a group you specify a prefix. Any sessions
whose names begin with that prefix will share the same setup procedure:fromnoxoptimportNoxOpt,Sessiongroup=NoxOpt()@nox.setup("python")defsetup_python(session:Session)->None:...# your setup logic here@group.sessiondefpython_tests(session:Session)->None:...@group.sessiondefjavascript_tests(session:Session)->None:...Here,setup_pythonwill only run when any session whose name begins withpythonis
executed. In this case that would only apply to thepython-testssession.You can also declare common settings for all sessions within a group by passingNoxOpt(where=dict(...)). This parameter accepts a dictionary that will be passed to
thenox.sessiondecorator as keyword arguments when constructing each session. So, if
you wanted to run all sessions in a group with Python 3.10 and 3.11 you would configure:fromnoxoptimportNoxOpt# run all sessions in this group using Python 3.10 and 3.11group=NoxOpt(where=dict(python=["3.10","3.11"]))Automatic TagsAn additional nicety of NoxOpt is that is can automatically create tags based on the
names of your sessions using theNoxOpt(auto_tag=True)parameter. The idea behind this
parameter is that if you have a set of sessions with a common naming scheme like:fromnoxoptimportNoxOpt,Sessiongroup=NoxOpt(auto_tag=True)@group.sessiondefcheck_python_tests(session:Session)->None:...@group.sessiondefcheck_python_format(session:Session)->None:...@group.sessiondefcheck_javascript_tests(session:Session)->None:...@group.sessiondefcheck_javascript_format(session:Session)->None:...NoxOpt will generate the following tags which, if run withnox -t <tag>will execute...check- all sessionscheck-python- onlycheck-python-testsandcheck-python-formatcheck-javascript- onlycheck-javascript-testsandcheck-javascript-formatIt does this by splitting every session name in theNoxOptgroup on-characters
a tag where there are at least two or more sessions with a common prefix.