Ir para o conteúdo

tempest_fastapi_sdk.checks

System checks estilo Django: validação de configuração no startup.

« Web, UI e utilitários


CheckLevelEnum

Bases: BaseIntEnum

Severity of a system-check message (mirrors logging levels).

A run "fails" when any message reaches the configured threshold (ERROR by default), so WARNING surfaces advice without blocking startup while ERROR / CRITICAL do block it.

CheckMessage dataclass

CheckMessage(
    level: CheckLevelEnum, message: str, hint: str | None = None, id: str | None = None
)

One finding produced by a system check.

Attributes:

Name Type Description
level CheckLevelEnum

The severity.

message str

What is wrong (or noteworthy).

hint str | None

How to fix it, shown after the message.

id str | None

A stable identifier (e.g. "security.W001") for allow-listing or documentation.

is_serious

is_serious(threshold: CheckLevelEnum = ERROR) -> bool

Return whether this message reaches threshold.

Parameters:

Name Type Description Default
threshold CheckLevelEnum

The level at or above which a message is considered serious. Defaults to ERROR.

ERROR

Returns:

Name Type Description
bool bool

True when self.level >= threshold.

CheckRegistry

CheckRegistry()

A registry of system-check functions, filterable by tag.

Register a check with :meth:register or the :meth:check decorator, optionally tagging it ("security", "database", …) so a run can target a subset. Use your own instance for isolated sets, or the process-wide :data:default_registry via the module-level helpers.

Initialize an empty registry.

register

register(func: CheckFn, *tags: str) -> CheckFn

Register func under zero or more tags.

Parameters:

Name Type Description Default
func CheckFn

The check callable.

required
*tags str

Optional tags for selective runs.

()

Returns:

Name Type Description
CheckFn CheckFn

func unchanged, so this doubles as a decorator.

check

check(*tags: str) -> Callable[[CheckFn], CheckFn]

Decorator form of :meth:register.

Parameters:

Name Type Description Default
*tags str

Optional tags for selective runs.

()

Returns:

Type Description
Callable[[CheckFn], CheckFn]

Callable[[CheckFn], CheckFn]: The registering decorator.

run

run(context: object = None, *, tags: Iterable[str] | None = None) -> list[CheckMessage]

Run the registered checks and collect their messages.

Parameters:

Name Type Description Default
context object

Passed to every check — typically the settings object under inspection, or None.

None
tags Iterable[str] | None

When given, only checks carrying at least one of these tags run.

None

Returns:

Type Description
list[CheckMessage]

list[CheckMessage]: Every message emitted, in registration

list[CheckMessage]

order.

clear

clear() -> None

Drop every registered check (test isolation).

check

check(*tags: str) -> Callable[[CheckFn], CheckFn]

Decorator registering a check on :data:default_registry.

Parameters:

Name Type Description Default
*tags str

Optional tags.

()

Returns:

Type Description
Callable[[CheckFn], CheckFn]

Callable[[CheckFn], CheckFn]: The registering decorator.

register_check

register_check(func: CheckFn, *tags: str) -> CheckFn

Register a check on :data:default_registry.

Parameters:

Name Type Description Default
func CheckFn

The check callable.

required
*tags str

Optional tags.

()

Returns:

Name Type Description
CheckFn CheckFn

func unchanged.

run_checks

run_checks(
    context: object = None,
    *,
    tags: Iterable[str] | None = None,
    registry: CheckRegistry | None = None,
) -> list[CheckMessage]

Run checks and return every message, without raising.

Parameters:

Name Type Description Default
context object

Passed to every check (usually the settings).

None
tags Iterable[str] | None

Optional tag filter.

None
registry CheckRegistry | None

Registry to run; None uses :data:default_registry.

None

Returns:

Type Description
list[CheckMessage]

list[CheckMessage]: Every emitted message.

run_system_checks

run_system_checks(
    context: object = None,
    *,
    tags: Iterable[str] | None = None,
    fail_level: CheckLevelEnum = ERROR,
    registry: CheckRegistry | None = None,
) -> list[CheckMessage]

Run checks and raise if any reaches fail_level.

Call this from a FastAPI lifespan to fail fast on a misconfigured deployment instead of serving traffic with, say, an empty JWT secret.

Parameters:

Name Type Description Default
context object

Passed to every check (usually the settings).

None
tags Iterable[str] | None

Optional tag filter.

None
fail_level CheckLevelEnum

The threshold that makes the run fail. Defaults to ERROR.

ERROR
registry CheckRegistry | None

Registry to run; None uses :data:default_registry.

None

Returns:

Type Description
list[CheckMessage]

list[CheckMessage]: Every emitted message (including the

list[CheckMessage]

non-serious ones), when the run passes.

Raises:

Type Description
SystemCheckError

When at least one message reaches fail_level.

SystemCheckError

SystemCheckError(messages: list[CheckMessage])

Bases: Exception

Raised when a run produces a message at or above the threshold.

Attributes:

Name Type Description
messages list[CheckMessage]

The serious messages that triggered the failure.

Initialize with the offending messages.

Parameters:

Name Type Description Default
messages list[CheckMessage]

The serious messages.

required