Monty class is the main interface for creating and running a sandboxed Python interpreter. It parses Python code on initialization and can execute it multiple times with different inputs, separating parsing cost from execution.
Constructor
str
required
Python code to execute
str
default:"'main.py'"
Name used in tracebacks and error messages
list[str] | None
List of input variable names available in the code
bool
default:"False"
Whether to perform type checking on the code
str | None
Optional code to prepend before type checking, e.g. with input variable declarations or external function signatures
list[type] | None
Optional list of dataclass types to register for proper
isinstance() support on outputMontySyntaxError: If the code cannot be parsedMontyTypingError: Iftype_checkis True and type errors are found
Example
Methods
run()
dict[str, Any] | None
Dict of input variable values (must match names from constructor)
ResourceLimits | None
Optional resource limits configuration
dict[str, Callable[..., Any]] | None
Dict of external function callbacks
Callable[[Literal['stdout'], str], None] | None
Optional callback for print output
Callable[[OsFunction, tuple[Any, ...]], Any] | None
Optional callback for OS calls. Called with
(function_name, args) where function_name is like 'Path.exists' and args is a tuple of arguments. Must return the appropriate value for the OS function (e.g., bool for exists(), stat_result for stat()).The result of the last expression in the code
MontyRuntimeError: If the code raises an exception during execution
Example with External Functions
Example with Resource Limits
start()
dict[str, Any] | None
Dict of input variable values (must match names from constructor)
ResourceLimits | None
Optional resource limits configuration
Callable[[Literal['stdout'], str], None] | None
Optional callback for print output
FunctionSnapshotif an external function call is pendingNameLookupSnapshotif more futures need to be resolvedFutureSnapshotif futures need to be resolvedMontyCompleteif execution finished without external calls
MontyRuntimeError: If the code raises an exception during execution
Example with Iterative Execution
type_check()
str | None
Optional code to prepend before type checking, e.g. with input variable declarations or external function signatures
MontyTypingError: If type errors are found. Use.display(format, color)on the exception to render the diagnostics in different formats.RuntimeError: If the type checking infrastructure fails internally.
dump()
Monty.load(). This allows caching parsed code to avoid re-parsing on subsequent runs.
Bytes containing the serialized Monty instance
ValueError: If serialization fails
Example
load() (static method)
bytes
required
The serialized Monty data from
dump()list[type] | None
Optional list of dataclass types to register for proper
isinstance() support on outputA new Monty instance
ValueError: If deserialization fails
register_dataclass()
isinstance() support on output.
When a dataclass passes through Monty and is returned, it normally becomes an UnknownDataclass. By registering the original type, we can use it to instantiate a real instance of that dataclass.
type
required
The dataclass type to register
TypeError: If the argument is not a dataclass type
Async Helper Function
run_monty_async()
Monty
required
The Monty instance to execute
dict[str, Any] | None
Dictionary of input variable values
dict[str, Callable[..., Any]] | None
Dictionary of external functions (can be sync or async)
ResourceLimits | None
Optional resource limits configuration
Callable[[Literal['stdout'], str], None] | None
Optional callback for print output
AbstractOS | None
Optional OS access handler for filesystem operations (e.g., OSAccess instance)
The output of the Monty script
Example
MontyRepl
TheMontyRepl class provides an incremental, no-replay REPL (Read-Eval-Print Loop) session. Each feed() call compiles and executes only the provided snippet against preserved heap/global state, unlike traditional REPLs that replay all history.
create() (static method)
(repl, output) where output is the initial execution result.
str
required
Initial Python code to execute
str
default:"'main.py'"
Name used in tracebacks and error messages
list[str] | None
List of input variable names available in the code
dict[str, Any] | None
Initial input values for the session
ResourceLimits | None
Optional resource limits configuration
Callable[[Literal['stdout'], str], None] | None
Optional callback for print output
list[type] | None
Optional list of dataclass types to register
A tuple containing the REPL session and the initial execution result
MontySyntaxError: If the code cannot be parsedMontyRuntimeError: If the code raises an exception during execution
script_name (property)
feed()
str
required
Python code snippet to execute
Callable[[Literal['stdout'], str], None] | None
Optional callback for print output
The result of the code snippet
MontyRuntimeError: If the code raises an exception during execution
Example
dump()
MontyRepl.load().
Bytes containing the serialized REPL session
load() (static method)
bytes
required
The serialized REPL data from
dump()Callable[[Literal['stdout'], str], None] | None
Optional callback for print output
list[type] | None
Optional list of dataclass types to register
A new MontyRepl instance
ValueError: If deserialization fails
