Python API¶
Most people only need the CLI. The Python API is for embedding a shell in your own code, building tools on top of the namespace, and writing adapters.
Starting a shell¶
embed ¶
embed(
namespace: dict[str, Any] | None = None,
*,
interface: str | None = None,
config_file: str | Path | None = None,
include_caller: bool = True,
banner: bool = True,
**overrides: Any,
) -> None
Open a fastapi-repl shell right here, like IPython.embed().
The project configuration is discovered as usual, then the caller's local
and global variables (and namespace) are added on top, so you can poke
at the state of a script or a debugging session with your models and
session at hand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namespace
|
dict[str, Any] | None
|
Extra names to add last. |
None
|
interface
|
str | None
|
|
None
|
config_file
|
str | Path | None
|
Use this config file instead of discovering one. |
None
|
include_caller
|
bool
|
Add the caller's globals and locals. |
True
|
banner
|
bool
|
Print the startup banner. |
True
|
**overrides
|
Any
|
Any :class: |
{}
|
Raises:
| Type | Description |
|---|---|
ReplError
|
If called while an event loop is running (for example
inside an |
build_namespace ¶
build_namespace(
config_file: str | Path | None = None, **overrides: Any
) -> tuple[ReplSession, dict[str, Any]]
Build a session without starting a shell.
Useful in notebooks, tests and scripts. Remember to call
session.close() (or use the session as a context manager) when done.
start_shell ¶
start_shell(
loaded: LoadedConfig,
*,
extra: dict[str, Any] | None = None,
banner: bool = True,
console: Console | None = None,
) -> None
Build a session from loaded and run the configured interface until exit.
Configuration¶
load_config ¶
load_config(
*,
cli: Mapping[str, Any] | None = None,
config_file: str | Path | None = None,
cwd: Path | None = None,
environ: Mapping[str, str] | None = None,
load_env_file: bool = True,
) -> LoadedConfig
Discover, merge and validate the configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cli
|
Mapping[str, Any] | None
|
Values from command-line flags. |
None
|
config_file
|
str | Path | None
|
An explicit config file. It may be a |
None
|
cwd
|
Path | None
|
Where to start looking for the project root. Defaults to the cwd. |
None
|
environ
|
Mapping[str, str] | None
|
Environment variables. Defaults to |
None
|
load_env_file
|
bool
|
Load |
True
|
Raises:
| Type | Description |
|---|---|
ConfigError
|
If a file cannot be parsed or a value is invalid. |
LoadedConfig
dataclass
¶
The result of :func:load_config.
root
instance-attribute
¶
The project root. Relative paths in the config are resolved from here.
files
class-attribute
instance-attribute
¶
Config files that were read, lowest precedence first.
sources
class-attribute
instance-attribute
¶
Maps dotted setting names (print_sql, sqlalchemy.engine) to where they came from.
Sessions and the event loop¶
ReplSession ¶
A fully prepared shell session.
Use it as a context manager::
with ReplSession(load_config()) as session:
session.namespace.to_dict()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loaded
|
LoadedConfig
|
The resolved configuration. |
required |
console
|
Console | None
|
Console for the banner and warnings. Defaults to stdout. |
None
|
extra
|
dict[str, Any] | None
|
Extra names added last ( |
None
|
Runtime ¶
Owns the event loop used by the shell session.
error_hooks
instance-attribute
¶
Called with each exception raised by code run on the loop (see :meth:report_error).
run ¶
Run an awaitable to completion on the session loop and return its result.
Context variables set by the awaitable are propagated back to the caller.
Ctrl+C cancels the awaitable and re-raises KeyboardInterrupt.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called while the loop is already running (for
example from inside ptpython, where you can |
report_error ¶
Tell the error hooks that user code raised error.
Called automatically for awaitables run through :meth:run.
Interfaces also call it for errors in synchronous code. Each exception
is reported once, and a failing hook never hides the original error.
await_ ¶
Run an awaitable and return its result. Exposed in the shell as await_.
Useful in interfaces without top-level await support (bpython) and
in synchronous helper code.
adopt_context
staticmethod
¶
Copy variables set in context into the current context.
LifespanManager ¶
Drives an ASGI application's lifespan protocol.
This speaks raw ASGI rather than calling framework internals, so it works with FastAPI, Starlette, Litestar, Quart and any other ASGI 3 app.
The app runs in a background task on the session loop. After
:meth:startup, the task sits waiting for the shutdown message, which
:meth:shutdown sends when the shell exits.
startup
async
¶
Send lifespan.startup and wait for the app to finish starting.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The lifespan |
Raises:
| Type | Description |
|---|---|
LifespanError
|
If the app reports a startup failure or times out. |
shutdown
async
¶
Send lifespan.shutdown and wait for the app to finish.
Raises:
| Type | Description |
|---|---|
LifespanError
|
If the app reports a shutdown failure. |
Adapters¶
ORMAdapter ¶
Base class for ORM adapters.
Lifecycle, in order:
detect()(class method) decides whether the adapter applies.setup()runs on the session loop, e.g. to initialise connections.enable_read_only()ifread_onlyis on.default_imports(),discover_models()andobjects()feed the namespace.enable_sql_echo()if--print-sqlis on, after startup hooks ran.on_error()after each failed statement, while the shell is open.teardown()runs on the session loop when the shell exits.
name
class-attribute
¶
Unique identifier, used in the adapters setting and plugin options.
package
class-attribute
¶
Import name of the ORM package, used by :meth:is_installed.
replaces
class-attribute
¶
Adapters made redundant by this one (SQLModel replaces SQLAlchemy).
detect
classmethod
¶
Return True if this project uses the ORM.
Called after the app, models and base have been imported, so
checking sys.modules is usually enough. Only used when the
adapters setting is empty (auto-detection).
database ¶
Where the adapter is connected, for the banner. Never include passwords.
Use :func:mask_url to render a URL safely.
enable_read_only
async
¶
Make every connection read-only (the read_only setting).
Raise an exception if that cannot be guaranteed: the shell then refuses to start rather than silently giving write access.
on_error ¶
Called after a statement typed in the shell raised error.
Use it to recover, e.g. roll back a session that can no longer be used.
Only called when the rollback_on_error setting is on.
default_imports ¶
Import specs for helpers such as select or Q.
Skipped when the default_imports setting is false.
tip ¶
An example line of code for the banner, using names in namespace.
AdapterContext
dataclass
¶
Everything an adapter can use. Passed to the adapter's constructor.
root
class-attribute
instance-attribute
¶
The project root.
modules
class-attribute
instance-attribute
¶
Modules imported from the models setting, including submodules.
bases
class-attribute
instance-attribute
¶
Objects imported from the base setting.
options
class-attribute
instance-attribute
¶
This adapter's [tool.fastapi-repl.plugins.<name>] table.
warn
class-attribute
instance-attribute
¶
Report a non-fatal problem to the user.
ModelInfo
dataclass
¶
A model class discovered by an adapter.
name
instance-attribute
¶
The name the model gets in the shell (before aliases and collisions).
prefix
class-attribute
instance-attribute
¶
Prefix used when two models share a name (collision = "prefix").
label
class-attribute
instance-attribute
¶
Optional group label, such as a Tortoise app label. Usable in dont_load.
SQLPrinter ¶
Prints SQL statements with syntax highlighting.
Adapters call the printer with each statement they see. Custom adapters can use it too; see "Writing adapters" in the docs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
console
|
Console | None
|
Where to print. Defaults to stderr, so piped output stays clean. |
None
|
truncate
|
int | None
|
Maximum number of characters of SQL to show. |
None
|
location
|
bool
|
Also show the line of user code that triggered the query. |
False
|
__call__ ¶
__call__(
statement: str,
params: Any = None,
*,
duration: float | None = None,
many: bool = False,
caller: str | None = None,
) -> None
Print one statement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
str
|
The SQL text. |
required |
params
|
Any
|
Bound parameters, shown dimmed. |
None
|
duration
|
float | None
|
Execution time in seconds. |
None
|
many
|
bool
|
The statement ran with |
False
|
caller
|
str | None
|
|
None
|
Errors¶
ReplError ¶
Bases: Exception
Base class for all fastapi-repl errors.
The CLI catches these and prints the message without a traceback, so the message should tell the user what went wrong and how to fix it.