Smarter Typing Library

Shared type aliases for the Smarter platform.

A type construction that the codebase spells out many times (more than about ten) is declared here once, under a name with the Smarter prefix and the Type suffix, e.g. SmarterDictType. Use these aliases instead of repeating the construction, and wrap them at the point of use, e.g. Optional[SmarterDictType] or list[SmarterDictType].

An alias doesn’t make a loose type acceptable. A mapping whose values really can be anything is a SmarterDictType, but a sizable, structured piece of JSON still gets a Pydantic model (see ADR-031, Pydantic over JSON).

The module also has required(), which narrows an Optional value that is never None in practice, e.g. a saved row’s created_at.

smarter.lib.typing.SmarterDictType

A mapping of string keys to values of any type.

E.g. keyword arguments, a status report, or a parsed YAML or JSON object whose shape isn’t modeled.

alias of dict[str, Any]

smarter.lib.typing.required(value, name)[source]

Return a value that is Optional in its type, but must not be None here.

E.g. a saved row’s created_at and updated_at, which are nullable columns that Django sets on the first save, or an account’s account_number, which is generated when the account is created:

status = SAMGuardrailStatus(
    accountNumber=required(self.account.account_number, "account.account_number"),
    created=required(guardrail.created_at, "guardrail.created_at"),
)
Parameters:
  • value (TypeVar(_T) | None) – The value.

  • name (str) – What the value is, for the error message.

Return type:

TypeVar(_T)

Returns:

The value.

Raises:

ValueError – If the value is None.