no-dict-return-annotation (BTDM004)¶
Added in 0.8.0 · Stable · Enabled by default (error)
Family: data-models · Standard: CES-79 · Languages: python
Directive¶
A boundary function returns a typed structure, never a raw dict: a @dataclass for internal
boundaries, or a pydantic BaseModel where validation or serialization is needed (a FastAPI
endpoint, for one). An error by default.
A boundary function is a function of a module or a method of a class, whose name and whose class
name do not start with _, and that is not defined inside another function. The rule skips private
and nested functions.
This rule catches a function (def or async def) annotated to return dict or dict[...].
A lookup table, whose keys are data and not field names, is declared with an alias whose name ends
in Table (StatusTable = dict[str, str]); the rule accepts it.
Why¶
- The type checker cannot check the keys of a
dict[str, Any], so a renamed key breaks its callers at run time. - A
@dataclassorBaseModeldeclares the fields, and the type checker checks them.
Do¶
Don't¶
Suppression¶
A comment on the def line silences it.