Skip to content

Valid Code & Restrictions

Xian contracts are ordinary Python syntax inside a restricted execution model. The linter enforces that restricted subset before code is accepted.

Allowed Syntax

FeatureExampleNotes
assignmentx = 1
arithmetic+, -, *, /, //, %, **
comparisons==, !=, <, >, <=, >=, in, not in, is, is not
boolean logicand, or, not
if / elif / elseif x > 0:
for / whileloopsmetered like everything else
functionsdef f():top-level only
returnreturn value
assertassert ok, "message"main validation pattern
collectionslist, dict, set, tuple
list comprehensions[x for x in items]generator expressions are not allowed
subscripts / slicesx[0], x[1:3]
importsimport currency, import hashlibmodule-level only

Forbidden Syntax

FeatureError CodeWhy
try/except, with, lambda, yield, yield from, nonlocal, @E001blocked syntax in the sandbox
names starting or ending with _E002blocks Python internals / escape paths
import inside a functionE003imports must be explicit and module-level
from x import yE004use import x then x.y
stdlib module importE005only deployed contracts and runtime modules are allowed
class definitionsE006contracts are module/function based
async functionsE007no async execution in contracts
invalid decoratorsE008only @export and @construct
multiple constructorsE009only one constructor allowed
multiple decorators on one functionE010single-decorator model
forbidden ORM kwargsE011runtime owns those names
tuple unpacking of ORM declarationsE012storage declarations must be explicit
no @export function presentE013contract needs a public API
forbidden builtin or reserved nameE014unsafe builtins and reserved identifiers are blocked
exported arg shadows ORM nameE015avoids confusing state collisions
invalid argument annotationE016exported args must use allowed types
missing export annotationE017exported args must be typed
invalid export return annotationE018export returns may only use allowed types
nested function definitionE019avoids closures and hidden state
parse errorE020ordinary syntax error

Allowed Builtins

The current builtin allowlist is intentionally small:

text
Exception False None True abs all any ascii bin bool bytearray bytes chr
dict divmod filter float format frozenset hex int isinstance issubclass len
list map max min oct ord pow range reversed round set sorted str sum tuple zip

Everything else is treated as forbidden.

Allowed Export Signature Annotations

The same allowlist applies to exported arguments and exported return annotations:

python
str, int, float, bool, dict, list, Any
datetime.datetime, datetime.timedelta

In Xian, float in an export signature means a deterministic decimal-backed value at runtime. Use float for user-facing decimal amounts, not Python Decimal.

Examples:

python
@export
def balance_of(address: str) -> float:
    return balances[address]

@export
def stream_window(stream_id: str) -> dict:
    return {
        "begins": streams[stream_id, "begins"],
        "closes": streams[stream_id, "closes"],
    }

Invalid annotations still fail:

python
@export
def bad(value: Decimal):
    return value

Import Rules

Allowed imports fall into two buckets:

  • deployed contracts, for example import currency
  • runtime-provided modules such as hashlib, datetime, random, importlib, crypto, and decimal

The Python standard library is not generally available to contracts.

Practical Guidance

When in doubt:

  • keep contract code flat and explicit
  • use assert instead of exception handling
  • keep all imports at module scope
  • prefer simple data structures
  • test the contract locally with ContractingClient