Catch null-after-.get bugs before they reach production.
dict.get(key, default) only uses default when the key is absent. When the key is present with a JSON null, the result is None — so the "safe" default is not safe:
attrs = event.get("attributes", {}) # None if "attributes": null
custom = attrs.get("attributes", {}) # AttributeError
dict(record.get("credentials", {})) # TypeError when credentials is nullThis exact class of bug caused a whole Datadog log page to fail, made an integration store unreadable, and added a trailing separator to a pytest environment value. nullguard flags it statically.
pip install git+https://github.com/Sakibpedia/nullguard.gitOr from a checkout:
pip install -e ".[dev]"python -m nullguard path/to/project
nullguard src tests --jsonExample output:
src/store.py:95:18: NG001: 'credentials' may be None when the key exists with a null value; dict() would fail
src/client.py:98:29: NG002: chained .get() on the result of .get(key, literal) - an explicit null value makes it None
Exit code is 1 when findings exist, 0 when clean, so it can be used as a CI gate. Syntax errors are reported on stderr and skipped.
| Code | Pattern | Why it matters |
|---|---|---|
NG001 |
value = obj.get(k, {}) followed by value.get(...), dict(value), len(value), value[k], iteration, .strip(), … |
A null value makes every one of those operations fail. |
NG002 |
obj.get(k, {}).get(...), dict(obj.get(k, {})), for x in obj.get(k, []) |
Same problem without an intermediate variable. |
The linter deliberately ignores:
obj.get(k, ...)wrapped inor {}or a conditional — the author already normalized it;- an explicit
Nonedefault, or a non-literal default such as a variable; - values whose None-ness is already checked with
isinstance(value, dict),if value:, orif value is not None:; .get(k)with no default — that is a different contract.
This is a heuristic AST check, not a type checker. It does not prove that a given key can actually hold null, and it does not track values across functions or modules. Treat findings as review prompts.
pip install -e ".[dev]"
python -m pytest
python -m ruff check src testsMIT licensed. Built by S M Nazmuz Sakib.