Skip to content

Parameters

The markers of the parameters of a task, used in Annotated[...], and the network rules of a task.

fastsandbox.Depends dataclass

Depends(dependency: Callable[..., Any], use_cache: bool = True)

A dependency resolved for each run.

The dependency is a function, sync or async, a generator or an async generator (its code after yield runs once the task ends); it can itself take a Context and other dependencies.

Example

async def token(ctx: Context) -> str: ... async def task(ctx: Context, token: Annotated[str, Depends(token)]) -> None: ...

Attributes:

Name Type Description
dependency Callable[..., Any]

The function that provides the value.

use_cache bool

Whether one value is shared by every parameter of a run that depends on the same function.

fastsandbox.WorkspaceDir dataclass

WorkspaceDir(clone: bool = False)

Marks a path parameter as the workspace of the sandbox.

The host directory is mounted at its own path, or cloned with clone=True.

Example

async def fix(ctx: Context, repo: Annotated[Path, WorkspaceDir()]) -> None: ...

Attributes:

Name Type Description
clone bool

The sandbox works on a private clone of the Git repository of the directory instead of the directory itself.

fastsandbox.Mounted dataclass

Mounted(target: str | None = None, read_only: bool = False)

Marks a path parameter as a further host directory or file mounted into the sandbox.

Mounted("/data") mounts it at /data, Mounted() at its own host path. The daemon mounts further paths only beside a workspace: a task with a Mounted input takes a WorkspaceDir one too, or its run fails with InvalidSpecError (further mounts need a workspace) before anything is created.

Example

async def report( ... data: Annotated[Path, Mounted("/data", read_only=True)], ... out: Annotated[Path, WorkspaceDir()], ... ) -> None: ...

Attributes:

Name Type Description
target str | None

Absolute path inside the sandbox; the host path when unset.

read_only bool

Whether the sandbox can only read it.

fastsandbox.Uploaded dataclass

Uploaded(target: str)

Marks a path parameter as a host directory copied into the sandbox.

The contents of the directory are copied into target before the task runs; the copy is the sandbox's own.

Attributes:

Name Type Description
target str

Directory inside the sandbox.

fastsandbox.Network dataclass

Network(allowed: tuple[str, ...] = (), denied: tuple[str, ...] = (), http_allowed: tuple[HttpRule, ...] = (), http_denied: tuple[HttpRule, ...] = ())

The network rules of the sandbox of a task. Combine them with |.

Every host is a host pattern: api.example.com, *.example.com, example.com:8443 or a CIDR prefix. A URL or a path fails the run with InvalidSpecError before anything is created; an HTTP rule the daemon refuses, applied before the start of the sandbox, fails it with LaunchError (step network). The rules add to the global policy of the daemon, and a refusal wins over any allowance. Under the Open preset of sbx, the global policy allows every host, so that an allowance restricts nothing and only a refusal keeps the sandbox from a host.

Example

Network.allow("pypi.org", "files.pythonhosted.org") | Network.deny("example.org")

Attributes:

Name Type Description
allowed tuple[str, ...]

The hosts allowed.

denied tuple[str, ...]

The hosts refused.

http_allowed tuple[HttpRule, ...]

The HTTP requests allowed.

http_denied tuple[HttpRule, ...]

The HTTP requests refused.

allow classmethod

allow(*hosts: str) -> Network

Allows connections to hosts, from the creation of the sandbox.

deny classmethod

deny(*hosts: str) -> Network

Refuses connections to hosts, whatever else allows them, from the creation.

allow_http classmethod

allow_http(*hosts: str, methods: Iterable[str], path: str = EVERY_PATH) -> Network

Allows the HTTP requests of methods to path on hosts, from the start.

deny_http classmethod

deny_http(*hosts: str, methods: Iterable[str], path: str = EVERY_PATH) -> Network

Refuses the HTTP requests of methods to path on hosts, from the start.

apply_to

apply_to(spec: SandboxSpec) -> SandboxSpec

spec, given the rules.

fastsandbox.framework.network.HttpRule dataclass

HttpRule(hosts: tuple[str, ...], methods: tuple[str, ...], path: str = EVERY_PATH)

An HTTP rule: the requests of methods to path on hosts.

Attributes:

Name Type Description
hosts tuple[str, ...]

The hosts, host patterns.

methods tuple[str, ...]

The methods, e.g. GET; a rule names at least one.

path str

The path pattern, e.g. /v1/**.