Why renaming a Python sentinel breaks old pickles
Python 3.15 added sentinel(), a built-in for creating placeholder values such as MISSING. Renaming one looks like the safest change in a codebase: change the definition, update every use, run the tests. But if the program has saved the sentinel to disk with pickle, every file written before the rename stops loading, and none of the tests notice.
Contact updates saved for later
contact_update is a small module that edits contact records. A record is a dictionary with an email address and a phone number. A change to a record is described by a second dictionary, an update, with one entry per field:
update = {"email": MISSING, "phone": None}
Each value means something different. A string replaces the field. None clears it. MISSING leaves it as it is. The update above keeps the email address and deletes the phone number.
None cannot mean “leave it alone” here, because it already means “clear”. A sentinel fills that gap: a unique object reserved for one meaning. Code checks for it with is, which compares identity, so value is MISSING is true only for that one object and never for a string or None.
Updates are not always applied right away. The program saves them to a file with pickle, Python’s built-in module for turning objects into bytes, and a separate process loads and applies them later, the way a job queue or an offline sync would. That trip through a file is where the sentinel’s name starts to matter.
Renaming MISSING to UNSET
A cleanup renames the sentinel, the kind of change a refactoring tool or a code review approves without a second look:
-MISSING = sentinel("MISSING")
+UNSET = sentinel("UNSET")
Every use of the name is updated and the program keeps running. The updates saved before the rename no longer load. pickle.loads() raises:
AttributeError: module 'contact_update' has no attribute 'MISSING'
The saved file holds no copy of the sentinel. It holds the text contact_update and MISSING, an instruction to import that name when the file is loaded. That design is what lets a sentinel come back from a pickle as the same object, and it makes the sentinel’s name part of the file format.
Why Python 3.15 added sentinel()
Before Python 3.15 the usual sentinel was MISSING = object(), a bare object equal to nothing but itself. In June 2021 Tal Einat proposed a standard replacement in PEP 661, pointing out that object() sentinels print as <object object at 0x...>, have no type to put in an annotation, and turn into a different object when copied or pickled. The PEP was accepted in April 2026 and Python 3.15 ships it as the built-in sentinel.
The pickling complaint is the one contact_update runs into first.
An object() sentinel comes back as a stranger
To reproduce the failure, build contact_update in an empty directory, starting with the pre-3.15 object() sentinel. contact_update.py defines the sentinel and apply_update(), which overwrites every field whose value is not MISSING. A None value overwrites the field with None, which clears it.
MISSING = object()
def apply_update(contact, update):
for field, value in update.items():
if value is not MISSING:
contact[field] = value
return contact
save_update.py pickles one update and writes the bytes to pending_update.pkl.
import pickle
from pathlib import Path
from contact_update import MISSING
update = {"email": MISSING, "phone": None}
Path("pending_update.pkl").write_bytes(pickle.dumps(update))
apply_saved_update.py loads the file in a separate run and applies it to a contact.
import pickle
from pathlib import Path
from contact_update import apply_update
update = pickle.loads(Path("pending_update.pkl").read_bytes())
contact = {"email": "[email protected]", "phone": "555-0100"}
print(apply_update(contact, update))
Run the writer, then the reader. Each command is a fresh Python process, the way a queued job or a cache works.
python3.15 save_update.py
python3.15 apply_saved_update.py
The email address should survive. It does not:
{'email': <object object at 0xffffb2f8c320>, 'phone': None}
Nothing raised an error. pickle saved the object() as “create a new object”, the reader got a fresh object, value is not MISSING was true, and apply_update() wrote that object over the email address. (The address in the output changes from run to run.)
sentinel() comes back as the same object
Change the first line of contact_update.py and leave the rest as it is:
MISSING = sentinel("MISSING")
Run both scripts again. The email address is kept and the phone number is cleared:
{'email': '[email protected]', 'phone': None}
sentinel("MISSING") creates an object that prints as MISSING and that pickles by name. The loaded value is the module’s own MISSING, so the is check works across processes.
The renamed module cannot read the saved file
Now rename MISSING to UNSET. Replace contact_update.py with this version and keep pending_update.pkl from the previous run:
UNSET = sentinel("UNSET")
def apply_update(contact, update):
for field, value in update.items():
if value is not UNSET:
contact[field] = value
return contact
Run only the reader:
python3.15 apply_saved_update.py
pickle.loads() raises before apply_update() is ever called. The traceback ends with:
AttributeError: module 'contact_update' has no attribute 'MISSING'
The file stores an import path, not the object
pickletools is a standard library module that prints the instructions inside a pickle without running them. pending_update.pkl still holds the update saved before the rename, so inspect it directly:
python3.15 -m pickletools pending_update.pkl
These are the lines for the "email" value:
22: \x8c SHORT_BINUNICODE 'contact_update'
38: \x94 MEMOIZE (as 2)
39: \x8c SHORT_BINUNICODE 'MISSING'
48: \x94 MEMOIZE (as 3)
49: \x93 STACK_GLOBAL
STACK_GLOBAL pops a module name and an attribute name and returns getattr(import_module("contact_update"), "MISSING"). Nothing else describes the sentinel. When the module no longer has that attribute, the lookup is the AttributeError from the previous section. (pickletools documentation, how pickle looks up names)
For comparison, the same command on pickle.dumps(object()) shows 'builtins', 'object', STACK_GLOBAL, then EMPTY_TUPLE and NEWOBJ. It looks up the class object and calls it, which is why the first experiment got a new object.
The name comes from the sentinel’s __reduce__() method, the hook pickle calls to ask an object how to save itself:
python3.15 -c 'from contact_update import MISSING; print(repr(MISSING.__reduce__()), MISSING.__module__)'
'MISSING' contact_update
When __reduce__() returns a string, pickle treats the object as a global variable with that name in the module given by __module__. In CPython 3.15.2, sentinel_reduce() returns the name passed to sentinel() (source, lines 174 to 185), and __module__ is the module where sentinel() was called.
The same rule applies when saving. pickle checks that the name leads back to the same object, so a sentinel whose string does not match its variable cannot be pickled at all. With UNSET = sentinel("MISSING") in contact_update.py, pickle.dumps(UNSET) raises:
_pickle.PicklingError: Can't pickle MISSING: it's not found as contact_update.MISSING
A sentinel created inside a function, or anywhere else it cannot be imported by module and name, cannot be pickled either (PEP 661 specification).
Keep the old name importable
Old files need contact_update.MISSING to exist. Add it back as an alias, a second name for the same object:
UNSET = sentinel("UNSET")
MISSING = UNSET # name stored in pickles written before the rename
def apply_update(contact, update):
for field, value in update.items():
if value is not UNSET:
contact[field] = value
return contact
The old file loads and applies correctly:
{'email': '[email protected]', 'phone': None}
The alias fixes reading. Writing is a separate decision, because new pickles store the name passed to sentinel(). With the version above, every file written from now on says UNSET, and a copy of the program from before the rename cannot read those files. That matters during a rolling deploy, when old and new workers share a queue, or after a rollback.
When older code may read newly written files, swap which name is the real one:
MISSING = sentinel("MISSING")
UNSET = MISSING # new name for code; files keep storing MISSING
Code uses UNSET, every pickle still says contact_update.MISSING, and both versions read each other’s files. The cost is that the value still prints as MISSING.
Remove the alias only when no stored file can refer to it. For a cache that means after it has been cleared. For a queue, after it has drained.
Pin the old format with a test
A rename like this passes every ordinary test, because the tests never load a file written by the previous version. Save one such file in the repository and load it in a test. Copy the pending_update.pkl written before the rename to tests/fixtures/pending_update_before_rename.pkl, then add tests/test_pickle_compat.py:
import pickle
from pathlib import Path
from contact_update import UNSET
def test_update_pickled_before_rename_still_loads():
data = Path("tests/fixtures/pending_update_before_rename.pkl").read_bytes()
assert pickle.loads(data)["email"] is UNSET
Run it from the project directory with python3.15 -m pytest. Without the alias it fails with the same AttributeError. With the alias it passes.
Keep sentinels out of stored data
The alias is the right fix for a pickle cache that can be deleted and rebuilt. For data that has to outlive refactors, avoid storing the sentinel at all. In contact_update, a missing key already says “leave this field alone”:
update = {"phone": None}
apply_update() handles that dictionary without any change, since it only touches the keys that are present. Nothing in the file depends on a Python name, so renames are free, and the same data can be written as JSON ({"phone": null}) for code in other languages to read. Keep sentinel() for what PEP 661 designed it for: default arguments and in-memory markers, where the only reader is the code that defined it.
Tested on CPython 3.15.2 in the python:3.15.2-bookworm Docker image. For the basics of what a pickle saves and what it looks up, see What Python pickle saves and what it looks up.