Skip to content

gh-148829: Add PySentinel_CheckExact() - #149725

Merged
vstinner merged 5 commits into
python:mainfrom
scoder:pysentinel_checkexact
May 13, 2026
Merged

vstinner merged 5 commits into
python:mainfrom
scoder:pysentinel_checkexact

Conversation

@scoder

@scoder scoder commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

As suggested in https://discuss.python.org/t/pep-661-sentinel-values/9126/343, this PR adds a PySentinel_CheckExact() C macro and makes PySentinel_Check() an alias, which can change if/when we allow subtypes in the future.

The discussion seems to suggest that subtyping will be valuable at some point. Adding both C macros now allows to write concise code on user side that (hopefully) requires less changes later.

I chose to make PySentinel_Check() an alias of PySentinel_CheckExact() and not the other way round because the current C macro actually does an exact check, so its correct name is PySentinel_CheckExact().

@read-the-docs-community

read-the-docs-community Bot commented May 12, 2026 •

Copy link
Copy Markdown
@scoder

scoder commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

I set the skipNews tag since we're still pre-release, but OTOH, we're post-beta1, so we might still want to add a news entry. Should I add one?

I was also mildly surprised to find the tests in object.c. By name, that seems quite generic for tests that belong to a specific builtin type.

@scoder

scoder commented May 12, 2026

Copy link
Copy Markdown
Contributor Author
@JelleZijlstra

Copy link
Copy Markdown
Member

Ah, sorry I forgot to follow up on this. I'm OK with the change and we should be able to do it in the beta phase, but we will need a NEWS entry since we already made a (beta) release with the old logic.

@scoder scoder removed the skip news label May 12, 2026
Comment thread Doc/c-api/sentinel.rst Resolved
Comment thread Doc/c-api/sentinel.rst Resolved
Comment thread Doc/c-api/sentinel.rst Resolved

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread Doc/c-api/sentinel.rst
Return true if *o* is a :class:`sentinel` object or a subtype.
The :class:`sentinel` type does not currently allow subclasses,
so this check is exact.
Future Python versions may choose to allow subtyping.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add "This function always succeeds." to both functions?

@vstinner
vstinner enabled auto-merge (squash) May 13, 2026 09:44
@vstinner
vstinner merged commit 94df625 into python:main May 13, 2026
51 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @scoder for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented May 13, 2026

Copy link
Copy Markdown

GH-149766 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label May 13, 2026
@vstinner

Copy link
Copy Markdown
Member

Merged. Thanks @scoder for your contribution!

vstinner pushed a commit that referenced this pull request May 13, 2026
gh-148829: Add PySentinel_CheckExact() (GH-149725)
(cherry picked from commit 94df625)

Co-authored-by: scoder <stefan_ml@behnel.de>
@scoder
scoder deleted the pysentinel_checkexact branch May 13, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants