Skip to content

no-shallow-container-type-guard

Do not claim typed container contents from a container-only type guard.

Why

TypeGuard and TypeIs let callers trust their declared narrowing. Checking only dict, list, or set does not validate keys, values, or elements, so callers can perform operations that fail at runtime.

Fix

Use object for unchecked container slots, or validate their contents when the narrower contract is required. Keep constrained input types that already prove the contents.

Examples

Before — flagged A container check does not prove string keys
app/values.py
from typing import TypeGuard
def is_mapping(value: object) -> TypeGuard[dict[str, object]]:
return isinstance(value, dict)
After — preferred Unchecked keys retain their object type
app/values.py
from typing import TypeGuard
def is_mapping(value: object) -> TypeGuard[dict[object, object]]:
return isinstance(value, dict)