optionaldict

raw JSON →
0.1.2 verified Mon Apr 27 auth: no python maintenance

A dict subclass that silently ignores NoneType values when setting items, useful for cleaning up API payloads or configurations. Current version 0.1.2, no recent releases (last in 2015), effectively in maintenance mode.

pip install optionaldict
error AttributeError: module 'optionaldict' has no attribute 'OptionalDict'
cause Importing with wrong casing or using old style import.
fix
Use: from optionaldict import OptionalDict
gotcha OptionalDict does not ignore None on initialization from another dict with None values; only assignments via __setitem__ are filtered.
fix After creating OptionalDict from a regular dict, manually reassign keys to trigger filtering, or use dict comprehension.
gotcha OptionalDict only checks for None, not other falsy values like empty string, 0, or False.
fix If you need to ignore other falsy values, subclass or wrap OptionalDict.

Create an OptionalDict, set keys with None values are ignored.

from optionaldict import OptionalDict

d = OptionalDict()
d['key1'] = 'value1'
d['key2'] = None  # This assignment is silently ignored
d['key3'] = 'value3'
print(dict(d))  # Output: {'key1': 'value1', 'key3': 'value3'}