Skip to content

gh-148829: Add PySentinel_CheckExact()#149725

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

gh-148829: Add PySentinel_CheckExact()#149725
vstinner merged 5 commits into
python:mainfrom
scoder:pysentinel_checkexact

Conversation

@scoder
Copy link
Copy Markdown
Contributor

@scoder scoder commented May 12, 2026

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().

@scoder scoder requested a review from ZeroIntensity as a code owner May 12, 2026 14:52
@scoder scoder added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label May 12, 2026
@read-the-docs-community
Copy link
Copy Markdown

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

@scoder
Copy link
Copy Markdown
Contributor Author

scoder commented May 12, 2026

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
Copy link
Copy Markdown
Contributor Author

scoder commented May 12, 2026

@vstinner @JelleZijlstra

@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 Misc/NEWS.d/next/C_API/2026-05-12-16-47-21.gh-issue-149725.HZLBTZ.rst Outdated
Comment thread Doc/c-api/sentinel.rst Outdated
Comment thread Doc/c-api/sentinel.rst Outdated
Comment thread Doc/c-api/sentinel.rst Outdated
Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

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
55 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
Copy link
Copy Markdown

bedevere-app Bot commented May 13, 2026

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 pre-release feature fixes, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants