Pyne Code Rules
Look up the P codes PyneCore reports when a module breaks a rule of Pyne code, before it runs: the message, the place it points at and the fix.
P0001: Identifier with PyneCore’s reserved character
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`{name}` contains `·`, which is reserved for PyneCore's internal names in Pyne code; rename the identifier.PyneCore names the state it adds to a script with a · in the name, so no
identifier of Pyne code may hold one. Python folds a few other characters
into · (NFKC normalization), so an identifier spelled with them is rejected
too.
P0002: Identifier reserved by the transform
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`{name}` is a name PyneCore's transform generates, reserved in Pyne code; rename the identifier.The transform adds functions and variables of its own to every script. An identifier of the script spelled like one of them would be overwritten by it, so the name is reserved.
P0003: Script decorator not on main
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
The script decorator belongs on `main`, not on `{function}`.A script has one entry point, the function the runner calls: main. The
@script.indicator, @script.strategy or @script.library decorator marks
it, so it stands on main and on no other function.
P0004: Second script decorator
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
A module has one script decorator, and `main` is defined twice.A module has one entry point. Keep one decorated main and remove the other.
P0005: na_bool is not a literal
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`na_bool` must be a literal `True` or `False`: the script's bool semantics are fixed before its module body runs.na_bool decides how the script’s bools are compiled, so PyneCore reads it
from the source before the module runs. Write it as True or False, not as
a name or an expression.
P0006: Series variable outside a function
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
A `Series` variable must be declared inside a function.A series keeps its history per call of the function that declares it. At
module level there is no such call, so declare the series inside main or
another function.
P0007: Persistent variable outside a function
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
A `Persistent` variable must be declared inside a function.A persistent variable keeps its value from bar to bar per call of the
function that declares it, as Pine’s var does. Declare it inside main or
another function.
P0008: Stateful call at module level
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
A function that keeps state from bar to bar cannot be called at module level.A ta.* call, or any function that keeps a series or a persistent, stores
its state per call site and per bar. The module body runs once, not on every
bar, so call it from main or a function main calls.
P0009: Input outside main’s parameters
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`{function}()` is not a `main()` parameter default; inputs must be declared as `main()` parameter defaults: `def main(length=input.int(14, ...))`.PyneCore reads a script’s inputs from the defaults of main’s parameters, so
that a run can set them before the script starts. An input called anywhere
else cannot be set.
P0010: lib imported under an alias
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`lib` must be imported as itself, not as an alias.The transform recognizes the Pine builtins by the lib. prefix. Write
from pynecore import lib.
P0011: Wildcard import of a module without all
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
Cannot use a wildcard import: the module `{module}` has no `__all__` defined.A wildcard import brings in exactly the names of the module’s __all__.
Without one, import the names you use one by one.
P0012: Unknown lib attribute
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`{module}` has no attribute `{name}`.The name is not part of this module of pynecore.lib. Check its spelling.
If it is a new pynecore.lib name, the module property registry is stale:
regenerate module_properties.json with scripts/module_property_collector.py.
P0013: Module-level object modified inside a function
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`{name}` {how} inside a function: an object created outside a function cannot be modified inside one.Module-level state is not restored when a bar is re-executed, so a change
made to it on one execution of a bar would stay when the bar runs again.
Declare the value as Persistent or IBPersistent inside the function
instead.
P0014: Plot called conditionally
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`{function}()` must be called unconditionally in the direct body of module-level `main`, before any possible early return.A plot is one series of the script’s output, and its place in the output is
fixed by its call. A call inside a branch, a loop, another function or after
an early return would leave the series without a value on some bars. Plot
na instead where nothing should be drawn.
P0015: Plot used as a value
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`{function}()` cannot be passed or stored as a function value; use a direct call or a single static alias so its scope can be checked.PyneCore checks where each plot is called. A plot function passed around as a value could be called from anywhere, so call it directly.
P0016: Stateful method of the wrong kind
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
The method `{method}` keeps state (a `ta` call, a series or a persistent), which only a plain or a `@staticmethod` method can do.A method’s state lives with the call site of the method, which PyneCore can tie only to a plain method or a static method. Remove the other decorator, or move the stateful code into a function.
P0017: Method receiver from an unpacked argument
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`{function}()` uses variables of `main()`, so its receiver cannot come from an unpacked argument; pass the receiver explicitly: `{function}(receiver, *args)`.A method that reads variables of main() gets them passed in after its
receiver, so PyneCore has to see which argument the receiver is.
P0018: Export captures a library global
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
The exported function `{function}` cannot capture the non-constant library global `{name}`.An exported function runs in the importing script, where the library’s module-level variables do not change with the library’s bars. Pass the value in as a parameter, or make the global a constant.
P0019: Strategy state in a security expression
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`strategy.{name}` cannot be used as the expression argument of `{function}()`: strategy state is only available in the chart context, not in a security context.A request.security expression runs on another symbol or timeframe, where
the chart’s strategy has no position, orders or trades.
P0020: Drawing in a security expression
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
`{name}()` cannot be used in the expression of `{function}()`.A drawing belongs to the chart. A request.security expression runs in
another context, so it cannot create or change one.
P0021: Too many requests in one function
| Severity | PyneCore raises |
|---|---|
| error | SyntaxError |
Message:
The function `{function}` reaches {count} `request.security` calls through the functions it calls; a function can use at most {limit} request values.Each request.security call a function reaches, directly or through the
functions it calls, takes one request value of that function. Split the
work across functions, or request a tuple of values in one call.