Skip to content
SakibpediaPublic

About

Catch null-after-.get bugs before they reach production

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

nullguard

CI Python License

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 null

This 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.

Install

pip install git+https://github.com/Sakibpedia/nullguard.git

Or from a checkout:

pip install -e ".[dev]"

Usage

python -m nullguard path/to/project
nullguard src tests --json

Example 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.

What it detects

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 in or {} or a conditional — the author already normalized it;
  • an explicit None default, or a non-literal default such as a variable;
  • values whose None-ness is already checked with isinstance(value, dict), if value:, or if value is not None:;
  • .get(k) with no default — that is a different contract.

Limits

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.

Development

pip install -e ".[dev]"
python -m pytest
python -m ruff check src tests

MIT licensed. Built by S M Nazmuz Sakib.

About

Catch null-after-.get bugs before they reach production

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages