Enforce typing (static + runtime)¶
Type hints help your editor and mypy, but they are erased at runtime
-- nothing stops a caller from passing a str where you annotated int
once the code ships. This recipe covers the two ways to close that gap:
- (A) Force annotations to exist -- author discipline, handled by linters (zero runtime cost).
- (B) Guarantee the runtime value matches the annotation -- real validation, with a per-call cost.
Golden rule
Any is a valid annotation -- what's wrong is not annotating.
Every strategy here requires things to be annotated; none forbids
Any.
(A) Force annotations with the linters¶
The SDK already enables ruff's ANN rule (require annotations) and
strict mypy. Every project scaffolded by tempest new ships it in
pyproject.toml:
[tool.ruff.lint]
# ANN forces annotating everything. ANN401 (forbid Any) is OFF on purpose.
select = ["E", "W", "F", "I", "B", "C4", "UP", "N", "SIM", "RUF", "ANN"]
ignore = ["B008", "B006", "ANN401", "ANN002", "ANN003"]
The tempest gates need the [cli] extra
lint, fix, format, fmt-check, type, test, check and
pr-prompt come from [cli]; with only [cli-ops] (the server one),
they exit with code 2 pointing at [cli]. Install it with
uv add --dev "tempest-fastapi-sdk[cli]" — details in
Installation → CLI.
Then just run the CLI gates:
tempest lint # ruff check (includes ANN)
tempest type # mypy
tempest check # everything: lint + fmt-check + type + test
A function without annotations now fails the gate:
def add(a, b): # missing types on a, b and the return
return a + b
# ruff: ANN001 Missing type annotation for function argument `a`
# ANN201 Missing return type annotation for public function `add`
The pydantic plugin only checks constructors with init_typed¶
plugins = ["pydantic.mypy"] on its own checks no argument of any model
constructor. The plugin does run — it is what synthesizes one keyword-only
parameter per field — but init_typed defaults to False, so every one of
those parameters comes out annotated Any:
from typing import reveal_type
from tempest_fastapi_sdk.schemas import BaseSchema
# docs-guard: skip — the rejected call below is what this section is about
class Probe(BaseSchema):
"""A schema with two typed fields."""
name: str
age: int
reveal_type(Probe.__init__)
Probe(name="x", age="doze")
With the plugin declared and nothing else (mypy 2.3.0, pydantic 2.13.4):
note: Revealed type is "def (__pydantic_self__: Probe, *, name: Any, age: Any, **kwargs: Any)"
Success: no issues found in 1 source file
Turning it on is one block in pyproject.toml:
[tool.mypy]
plugins = ["pydantic.mypy"]
[tool.pydantic-mypy]
init_typed = true
warn_required_dynamic_aliases = true
The same file, afterwards:
note: Revealed type is "def (__pydantic_self__: Probe, *, name: str, age: int, **kwargs: Any)"
error: Argument "age" to "Probe" has incompatible type "str"; expected "int" [arg-type]
Pylance and pyright load no plugin at all — they read the annotations directly and always flagged these call sites. Without the setting your editor and your CI disagree, and CI is the one that is wrong.
New projects ship it
tempest new writes the block as of v0.241.0, and the SDK itself now
uses it: turning it on cost zero fixes across the package's 409 source
files — it is a class of error that was never reported, not a backlog.
Service scaffolded before v0.241.0
Paste the block into your service's pyproject.toml by hand.
tempest check cannot turn it on for you: mypy reads plugin config
only from the config file, and exposes no command-line flag for it.
What init_typed starts refusing
Input pydantic would coerce at runtime. A Decimal field handed
"1.5" becomes error: Argument "amount" ... incompatible type "str";
expected "Decimal" under mypy, while at runtime the value still builds
as Decimal('1.5'). In a service that is the point — the annotation
becomes the contract, and callers who want the coercion write
Decimal("1.5") at the call site. In a library with public
constructors, decide case by case.
Enum field under use_enum_values: the tempest_fastapi_sdk.mypy plugin¶
BaseSchema turns on use_enum_values=True: a field annotated with an enum
holds the value ("paid"), not the member. The annotation still says the
enum, so mypy accepts code the runtime refuses:
from typing import reveal_type
from tempest_fastapi_sdk import BaseSchema, BaseStrEnum
# docs-guard: skip — the errors below are the subject of this section
class StatusEnum(BaseStrEnum):
"""Order status."""
PAID = "paid"
OPEN = "open"
class OrderSchema(BaseSchema):
"""An order as the API returns it."""
status: StatusEnum
def label(status: StatusEnum) -> str:
"""Return the member name of a status."""
return status.name
order = OrderSchema(status=StatusEnum.PAID)
reveal_type(order.status)
print(order.status == StatusEnum.PAID)
print(order.status.value)
print(label(order.status))
With only plugins = ["pydantic.mypy"] (mypy 2.3.1, pydantic 2.13.5):
And at runtime:
With the SDK's plugin next to it:
order.py:27: note: Revealed type is "str"
order.py:29: error: "str" has no attribute "value" [attr-defined]
order.py:30: error: Argument 1 to "label" has incompatible type "str"; expected "StatusEnum" [arg-type]
What the plugin does:
- Reading an enum field of a
use_enum_valuesmodel yields the value's type:strfor astrenum (BaseStrEnum,StrEnum),intfor anintenum,objectfor a plainEnum, whose value can be anything. It applies insideOptional, unions,list,tuple,dict. - The constructor accepts the member or the value: a field read from
one schema and passed to another one's constructor is a
str, which Pydantic validates back. - Comparison stays free:
order.status == StatusEnum.PAIDisTrueat runtime and passes mypy. - A plain
pydantic.BaseModeland a schema withuse_enum_values=Falseare left alone. The order of the two plugins in the list does not matter.
A new project starts with it
tempest new writes both plugins since v0.345.0. Turned on in the SDK
itself, the plugin found 7 schema-to-schema calls that only worked because
Pydantic converts the value back; the constructor now accepts the value,
and the one call through a subscripted generic (Model[T](...)) became an
explicit conversion.
Only mypy runs plugins
Pyright, Pylance and basedpyright keep reading the annotation as written.
And a constructor called through a subscripted generic (Model[T](...))
has no name for mypy to offer a hook on: convert at the call site
(StatusEnum(value)), which Pydantic accepts as well.
Configure typing strictness ([tool.tempest])¶
How strict the gates are is a knob in pyproject.toml. One field
controls both ruff's ANN rules and the mypy flags that
tempest lint/fix/type/check apply:
| Level | ruff (ANN) | mypy |
|---|---|---|
lenient |
nothing extra | nothing extra |
standard |
require annotations (ANN001/201/...) | --disallow-untyped-defs --disallow-incomplete-defs |
strict |
full ANN set | --strict |
The flags are layered on top of your [tool.ruff] / [tool.mypy]
-- they never relax the project config. ANN401 (which flags Any) is
never enabled, at any level.
Override per run without editing the file:
No [tool.tempest]?
When the field is absent (or there is no pyproject.toml), the level
is standard. Projects from tempest new ship it pre-set.
(B) Guarantee the runtime value¶
Where data comes from outside (queue message, external API response, CLI
input, dynamically built data), annotations are not enough -- you want
real validation. The SDK exposes three decorators over
pydantic.validate_call (already a dependency, so nothing new to
install):
strict_types -- no coercion¶
Rejects any value that is not already the annotated type. Arguments and the return are validated.
from tempest_fastapi_sdk import strict_types
# docs-guard: skip — the rejected call below is what this section is about
@strict_types
def add(a: int, b: int) -> int:
return a + b
add(1, 2) # 3
add("1", 2) # pydantic.ValidationError -- "1" is NOT coerced to 1
typed -- safe coercion¶
Same, but coerces when Pydantic can do so unambiguously ("1" -> 1).
Handy for stringly-typed input.
from tempest_fastapi_sdk import typed
# docs-guard: skip — the rejected call below is what this section is about
@typed
def add(a: int, b: int) -> int:
return a + b
add("1", 2) # 3 (coerced)
add("abc", 2) # pydantic.ValidationError -- cannot coerce
require_annotations -- fail at import when an annotation is missing¶
Does not validate values -- it guarantees the function is annotated,
failing at import (no linter run needed). self/cls and
*args/**kwargs are exempt; Any counts as a present annotation.
from typing import Any
from tempest_fastapi_sdk import require_annotations
@require_annotations
def ok(value: Any) -> None: # Any is valid
return None
@require_annotations
def bad(a) -> int: # TypeError at import:
return a # "bad: missing type annotation for parameter 'a'"
Where to use the runtime decorators
They carry a per-call cost. Use them at the boundaries (queue, external API, CLI), not on every internal method. In a FastAPI service the request body is already validated by its Pydantic schema at the router -- re-validating internally is redundant overhead.
Recap¶
Anyis a valid annotation; what's wrong is not annotating.- (A) linters force annotations to exist --
ANNin ruff + mypy, run viatempest lint/type/check. Zero runtime cost. - Strictness is a knob:
[tool.tempest] typing_strictness(lenient/standard/strict), with a per-run--strictnessoverride.ANN401never turns on. - (B) to guarantee the runtime value at the boundaries:
strict_types(no coercion),typed(coerces),require_annotations(require annotation at import). All overpydantic.validate_call.
Base enums¶
BaseStrEnum / BaseIntEnum extend the stdlib Enum with helpers tuned for Pydantic + SQLAlchemy round-tripping (lookup by value, JSON-serializable str / int inheritance, __contains__ that accepts raw values). Use them for every enum that crosses the API boundary.
from tempest_fastapi_sdk import BaseIntEnum, BaseStrEnum
class OrderStatusEnum(BaseStrEnum):
PENDING = "pending"
PAID = "paid"
SHIPPED = "shipped"
CANCELLED = "cancelled"
class Priority(BaseIntEnum):
LOW = 0
NORMAL = 1
HIGH = 2
assert OrderStatusEnum.PENDING == "pending" # str inheritance
assert "paid" in OrderStatusEnum # raw value membership
assert OrderStatusEnum("paid") is OrderStatusEnum.PAID # canonical lookup
assert Priority.NORMAL + 1 == Priority.HIGH # int math
assert str(OrderStatusEnum.PAID) == "paid" # text conversion = value
assert f"{Priority.HIGH:03d}" == "002" # numeric specs keep working
Because they inherit from str / int, Pydantic serializes them transparently as their underlying value and SQLAlchemy can persist them via the standard Enum column without an extra converter.
str(member) gives you the value, not \"Class.MEMBER\"
On a bare str/Enum mixin, str(OrderStatusEnum.PAID) and
f"{OrderStatusEnum.PAID}" return "OrderStatusEnum.PAID" — the classic footgun
that leaks the member name into a log line, a query string, or a value
written to a raw column. The SDK bases override __str__ and
__format__ to render the value, the way enum.StrEnum does. So
str(status) is a safe, explicit way to reach the stored representation —
equivalent to status.value, and without breaking numeric specs on
BaseIntEnum.
Changed in 0.171.0. Before that, str() returned "Class.MEMBER"; if
some code of yours relied on that format (log messages, say), switch it to
repr(member) or member.name.