Reviewer · Null Handling
Guard nullable values
Treat every value that can legitimately be `None` (or missing) as nullable end-to-end: annotate it correctly, guard it before use, and give it an explicit fallback before handing it to code that requires a real value.
Treat every value that can legitimately be None (or missing) as nullable end-to-end: annotate it correctly, guard it before use, and give it an explicit fallback before handing it to code that requires a real value.
Apply these rules:
-
Annotate what the default actually is. If a parameter or attribute defaults to
None, its type hint must say so (Optional[Dict[str, Any]] = None, notDict[str, Any] = None). A class-level annotation with no assignment is not initialization — assign the attribute in__init__(or give it a real default) so static checkers don’t flag it as uninitialized. -
Don’t assume an attribute or key exists just because the “normal” case has it. When a value might come from a dict instead of an object (e.g. parsed tool calls), or might come from an object whose nested attribute is optional (
self.director.llm,self.knowledge_generator.agent), usegetattr(obj, "attr", default)or an explicitisinstance/.get()check instead of direct access. Prefer degrading gracefully (return defaults/zeros) over raising from a property or helper that’s called in afinallyor hot path. - Bind an
Optionalattribute to a local before narrowing. Re-readingself.attracross multiple statements does not preserve a prioris not Nonecheck (and is unsafe if another thread can swap it). Bind once:path = self.memory_md_path if path and os.path.exists(path): size = os.path.getsize(path) -
Supply an explicit fallback when passing an
Optionalvalue to an API that requires a concrete one, e.g.agent.agent_name or "Worker",current_agent.agent_name or "". Don’t let aNonesilently propagate into a role, key, or label. -
Distinguish “missing” from “falsy-but-present”.
dict.setdefault("summary", reason)only fires when the key is absent; if the field is commonly initialized to""orNone, check truthiness instead (if not subtask.get("summary"): ...) so a real fallback value isn’t skipped. -
Validate types before arithmetic/logic on externally-sourced values. Don’t assume a field from an external provider or mock is numeric/non-null; coerce or skip (
if isinstance(value, (int, float))) rather than letting aTypeErrorsurface from deep inside a shared helper. - Avoid relying on an implicit
Noneas a hidden state flag (e.g.transcript is Nonemeaning “first iteration”). If that’s the intent, make it explicit (loop_count == 1) or document the invariant clearly, so future edits don’t silently break the branch.</description>
</invoke>