Treat every potentially-null/nullable config subsection and external payload field as untrusted: normalize it before accessing deeper keys or calling methods.
Rules
null subsections (common in YAML):
section = cfg.get("x") or {} before section.get(...).isinstance(value, dict) (or equivalent) before nested .get().startswith):
isinstance(field, str).NameError from Optional/Any).Example
def pick_language(stt_config: dict) -> str | None:
stt_config = stt_config or {}
groq_cfg = stt_config.get("groq") or {} # handles groq: null
local_cfg = stt_config.get("local") or {}
return groq_cfg.get("language") or local_cfg.get("language")
def handle_matrix_location(payload: dict) -> None:
geo_uri = payload.get("geo_uri")
if not isinstance(geo_uri, str):
return # or safely log/drop
if geo_uri.startswith("geo:"):
...
This prevents crashes like AttributeError: 'NoneType' object has no attribute 'get' and AttributeError/TypeError from calling string methods on non-strings.
Enter the URL of a public GitHub repository