
MISSING is not a user-facing class—it’s an internal CPython implementation detail: a singleton type (Token.MISSING) used by the contextvars module to represent an unset or invalidated token, exposed as contextvars.Token.MISSING.
`missing` is not a user-facing class—it’s an internal cpython implementation detail: a singleton type (`token.missing`) used by the `contextvars` module to represent an unset or invalidated token, exposed as `contextvars.token.missing`.
When traversing Python’s class hierarchy with object.__subclasses__(), you may unexpectedly encounter a class named MISSING. This is not a bug in your code—nor is it a standard built-in like NoneType or NotImplemented. Instead, it’s a private, low-level type defined in CPython’s C source: _PyContextTokenMissing_Type, registered under the qualified name "Token.MISSING".
This class serves a precise purpose in Python’s context variable system. When you call contextvars.ContextVar.set(), it returns a contextvars.Token instance. Later, calling token.reset() invalidates that token—and internally, the runtime uses Token.MISSING (a unique sentinel object of its own type) to mark the token as no longer valid. You can verify this directly:
import contextvars
# Get the MISSING singleton
missing = contextvars.Token.MISSING
print(type(missing)) # <class>
print(missing.__module__) # 'Token' — note: not a regular Python module
# Confirm it's the same object found via __subclasses__()
found = None
for cls in object.__subclasses__():
if cls.__name__ == "MISSING" and getattr(cls, '__module__', None) == "Token":
found = cls
break
print(found is type(missing)) # True</class>
⚠️ Important caveats:
-
Token.MISSINGis not part of the public API. Its existence, name, and behavior are implementation details of CPython (as seen inPython/context.c) and may change without notice—even across patch versions. - It does not appear in
sys.modules, because"Token"is not a loadable Python module—it’s a synthetic module name assigned to a built-in type for introspection only. - You should never rely on
MISSINGin application code. To check token validity, use the documented pattern:try: token.reset() except ValueError: # Token is already reset (or invalid) pass
In summary: MISSING is a runtime sentinel—elegant, invisible to most users, and intentionally hidden from documentation. Your traversal script correctly surfaced it because object.__subclasses__() includes all direct subclasses of object, including interpreter-internal types. Treat it as a curiosity—not a feature.
Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!











