Why None Is Not Enough: Sentinel Values for PATCH Updates in Python
A recent Hacker News discussion on sentinel values in Python prompted the author to recall a production bug from an update function that used None to mean both "not provided" and "clear the value." The problem surfaces clearly in PATCH APIs, where a field can be absent, explicitly null, or set to a value, and a plain None default cannot distinguish the first two cases. The article reviews the common object()-based sentinel and points out its drawbacks: an unhelpful repr, unreliable copying and pickling, and poor type-checker support. It then proposes an Enum-based sentinel that keeps a readable repr, survives deepcopy and pickle as a singleton, and lets type checkers narrow the type correctly.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in