PEP 661: Sentinel Values

Hi all, first post here ^^

I came across this PEP through a YouTube video covering upcoming 3.15 features, and since it was just accepted I figured it wasn’t too late to share a few thoughts. I haven’t read the full thread (337 posts is a lot!), so apologies if any of this has already been covered.

I’m not a CPython internals person, I mostly encounter sentinels from the typing side, as a way to express “this argument was not provided” in a signature.

One small concrete thing first

The 3.15 docs don’t mention the subclassing restriction at all. A user who tries class MySentinel(sentinel): ... gets a TypeError with no explanation and no pointer toward Enum as the grouped-sentinel alternative. Even if the ban stays for good reasons, that seems worth a documentation note regardless.

On the subclassing restriction itself

I didn’t find a rationale for the __init_subclass__ block anywhere, not in the PEP body, not in Rejected Ideas. The concern I can imagine is that class MISSING(sentinel): ... looks like a sentinel instance rather than a factory, which is fair. But the useful pattern doesn’t look like that:

class FalsySentinel(sentinel):
    def __bool__(self) -> bool:
        return False

MISSING = FalsySentinel("MISSING")

Lowercase name, explicit instantiation, the ambiguity mostly goes away. And __bool__ customisation was listed as a dropped feature in the PEP due to “added complexity,” but with subclassing it would just be… normal Python. No new API surface needed.

That said, I think I can see why the ban might exist technically. The __init__ uses sys._getframemodulename(1) with a hardcoded frame depth to capture the calling module for pickling, and a super().__init__() call from a subclass shifts that frame. That’s a real problem in pure Python. But since sentinel ships as a C builtin, that seems like something addressable at that level, which makes me genuinely unsure whether the ban reflects a considered design decision, or just that nobody needed subclassing badly enough to do the extra work.

I’d love to hear if that was actually discussed somewhere in the thread!

1 Like