Skip to content

Adapt Sentinel implementation to accepted version of PEP 661 #742

@JelleZijlstra

Description

@JelleZijlstra

PEP-661 was accepted, but it is implemented a little differently than our typing_extensions.Sentinel. Let's see how we can make the typing-extensions version better match the version that will be in Python 3.15, while keeping backward compatibility for users who may already be using typing_extensions.Sentinel.

Here are the relevant differences:

  • We have typing_extensions.Sentinel; 3.15 will have builtins.sentinel (lowercase). Let's not worry about the name. On Python 3.15 we'll re-export builtins.sentinel as typing_extensions.Sentinel.
  • Our Sentinel is subclassable; builtins.sentinel will not be. Let's add an __init_subclass__ that emits a DeprecationWarning.
  • We added an extra repr argument to control the repr. Let's emit a DeprecationWarning if it is passed.
  • The default repr is different: <name> for us, just name for the builtin. Let's just change this.
  • We made our Sentinel not picklable; the builtin is picklable by name. So let's add a __module__ attribute inferred from the caller (can reuse _set_module) and a __reduce__ that just returns self.__name__.
  • The builtin doesn't support weakrefs; our version does because it's in Python. I don't know of a way to deprecate our support, so let's just document this difference.
  • The builtin exposes a __name__ attribute; we have _name. Again let's just change this.
  • There are some observable changes from the builtin being in C (e.g., the type is immutable), but let's not worry about that.

There are three open PRs about changing Sentinel. #715 and #732 make changes that introduce further incompatibilities, so I'm going to close those. #617 goes in the right direction but also introduces some further incompatible changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions