Reviewer · Null Handling
Guard nullable values before use
Many bugs in this codebase come from dereferencing a value that is `None`, missing, or of an unexpected shape without checking first. Optional attributes, dict/object hybrids, not-yet-initialized members, and type annotations that claim non-optional but default to `None` all need explicit guarding before access:
Many bugs in this codebase come from dereferencing a value that is None, missing, or of an unexpected shape without checking first. Optional attributes, dict/object hybrids, not-yet-initialized members, and type annotations that claim non-optional but default to None all need explicit guarding before access:
- Bind optional attributes to a local variable before testing them, so type-narrowing survives across subsequent calls instead of re-reading a possibly-reassigned field:
path = self.memory_md_path if path and os.path.exists(path): size = os.path.getsize(path) - When a value may be a dict or an object (or absent), use a single accessor that handles both rather than assuming one shape:
fn = tool_call.get("function") if isinstance(tool_call, dict) else getattr(tool_call, "function", None) - When delegating to a nested/optional attribute that may not exist yet (e.g. not assigned until later in
__init__), guard withgetattr(self, "x", None)instead of assuming it’s always set:return sum_agent_usage([ getattr(self.knowledge_generator, "agent", None), getattr(self.reasoner, "agent", None), ]) - Before iterating, indexing, or calling methods on a value pulled from external/loosely-typed data (JSON schemas, provider responses), verify its type (
isinstance(value, dict),isinstance(value, (int, float))) rather than assuming it matches the happy path. - When a value is
Optional[str]but is about to be used where a plainstr(or other non-null type) is required, resolve it with an explicit fallback at the call site (agent.agent_name or "Worker"), not by leaving the gap for the callee to discover. - Keep annotations honest: if a parameter or attribute can genuinely be
None(including as a default), type itOptional[...]; don’t let the signature promise a guarantee the implementation doesn’t provide. - Prefer a definite state check (e.g. an explicit flag or loop counter) over an incidental
is Nonecheck as a proxy for “not yet initialized,” since future refactors can silently break that implicit invariant.
Before merging code that touches optional or loosely-typed values, ask: what happens when this is None, missing, an unexpected type, or read before it’s assigned — and make that path explicit rather than letting it raise or silently produce wrong data.</description>
</invoke>