Language reference
Differences from Python
Nox looks like Python and most small Python programs become Nox programs by adding type annotations. This page is the checklist of what differs, grouped by what you will hit first. Everything here is deliberate unless marked (not yet).
Types are mandatory#
Every variable's first assignment, every parameter and every return value carries a type. There is no Any, no dynamic attribute
creation, no setattr/getattr/exec/eval. x = [] is an error — write x: list[int] = []. A name keeps its type for its scope.
Inside functions x := expr infers the type from the value (not None/empty containers; see Types); this is not Python's walrus operator.
Numbers#
| Python | Nox |
|---|---|
unbounded int |
64-bit int that wraps on overflow; fixed-width types (u8…i64) trap on overflow |
2 ** -1 is 0.5 |
int ** int is an int (negative exponent → 0); use a float base |
10 ** 30 is exact |
wraps modulo 2⁶⁴ |
True + 1 is 2 |
bool never takes part in arithmetic — int(True) + 1 |
if x: truthiness |
conditions must be bool — write if x != 0: / if len(xs) > 0: |
round(2.5) is 2 |
same (half to even), but round(x) always returns int |
int(u8_value) n/a |
int(x) works on fixed-width integers and bool |
Integer // and % by zero raise ZeroDivisionError; float division never raises (inf/nan). Floats print like Python's repr.
Syntax#
- No
lambdawith statements. Semicolons, one-line compound statements (if x: y()),try … else,for/while … else,with a, b:, raw strings (r"…"), adjacent literal concatenation,...andglobal xare supported;noxc fmtwrites the expanded canonical form. - No Python expression walrus (
(n := f()));x := valueis a declaration statement that infers the type (see Types). *args,**kwargs, keyword-only (*), positional-only (/) and call-site unpacking (f(*xs),f(**d)) are supported (see Functions);f(**d)never binds a named parameter by key. Star-assignment (a, *b = xs, lists only), nested targets and[*a]/{**d}displays work. Default values must be constant literals.yieldgenerators are lazy state machines typedIterator[T](see Functions);yieldinsidetry … finallyis allowed, there is nosend(). Generator expressions are accepted only as a call argument and are evaluated eagerly into a list.nonlocalis supported (the variable needs an explicit type at its first declaration);global xis accepted and changes nothing, because module-level names are assigned directly from functions.- No
%formatting and no!ain f-strings (!rand!swork).\xHHis limited to ASCII; use\uHHHH/\UHHHHHHHHfor other characters. Identifiers are compared by code point (no NFKC normalization; seenox.unicodefor normalizing text). - Bare
raise(insideexcept) andraise X from Y(the cause is stored asX.cause, the message text ofY) work. async withandasync forexist;awaittakes aTask, a channel operation or an async-protocol call, not an arbitrary expression.- No
del name(onlydel xs[i]anddel d[k]). __name__ == "__main__"does not exist: the top-level statements of the entry file are the program, andmainis a reserved name.type Name = Taliases are supported (module level, no generic aliases); decorators that wrap are not part of Nox.match(onenums),@property,@staticmethodand@classmethodare.- Chained comparisons (
a < b < c) work.
Collections and iteration#
map,filter,zip,enumerateandreversedreturn new lists, not lazy iterators;rangeis only usable infor, comprehensions andlist(...).dictkeys areint,float,boolorstr; values are scalars, classes, lists or dicts.setelements areint,float,boolorstr. Both keep insertion order (as Python dicts do; Python sets do not). Tuples cannot be dictionary keys.- Lists are homogeneous:
[1, "a"]is a type error. A list of subclasses needs an annotation (xs: list[Base] = [Dog(), Cat()]). - Slices are values — you cannot assign to a slice or delete one.
xs.sort()needsint,floatorstrelements unless you passkey=. - A loop variable keeps its value after the loop, but after a
rangeloop it is one past the last element, not the last element. sorted,min,maxtakekey=, but the elements (or the keys) must beint,floatorstr.
Strings#
str is UTF-8 and indexed by code point; case conversion and character classes follow Unicode like Python (no normalization, no locale-specific mappings). The format
mini-language is fully supported in f-strings, format() and str.format.
Classes#
- Multiple inheritance (
class C(A, B):Cis a subtype of every base, see Classes); class attributes are shared storage assigned only through the declaring class (Cls.X = v, neverself.X = v); no metaclasses,__slots__,__getattr__,__call__orisinstance. - Fields are fixed: created in
__init__(or declared in the class body) and never added later.selfmay be left unannotated. ==on two instances compares fields structurally unless you define__eq__;isis only forNone.- Operator overloading uses the usual dunder names;
x += ycalls__add__. - A class used before it is defined is fine; a protocol is structural and only usable as a parameter type (no
list[Protocol]).
Errors and resources#
Exceptions are classes, except Base: catches subclasses, and e.message/e.line are available. There is no traceback object — an uncaught
exception prints the class and source line. Resources are released by with, defer and finally; __del__ runs once when the last reference goes away (Classes); there is no weakref.
Built-in error text is English; match the exception class, not the wording.
Modules#
import a.b / from a.b import x [as y] only — no import *, no relative imports, no dynamic import; a module may restrict what others can use with an opt-in __all__. All modules compile together;
module-level variables are read from other modules with from m import name, not as m.name. Third-party code arrives through nox.json and
noxc fetch, not pip.
Concurrency#
There is no GIL and no threading/asyncio. Tasks (spawn/await), channels and nox.thread replace them, and on the default backend tasks run
in parallel on a worker pool (Concurrency). There is no list[Task[T]]; gather results through a channel.
Performance model#
Nox is compiled ahead of time to native code. There is no interpreter overhead, no boxing of int/float, and memory is released
deterministically. A straight port of a numeric Python loop typically runs one to two orders of magnitude faster.