Skip to content

Implement the sentinel builtin (PEP 661) - #8945

Draft
youknowdot wants to merge 1 commit into
RustPython:mainfrom
youknowdot:cpython-315-sentinel
Draft

youknowdot wants to merge 1 commit into
RustPython:mainfrom
youknowdot:cpython-315-sentinel

Conversation

@youknowdot

@youknowdot youknowdot commented Oct 2, 2026 •

Copy link
Copy Markdown

Summary

Implement the native sentinel(name, /, *, repr=None) builtin described by PEP 661, as an independent prerequisite for the Python 3.15 standard-library migration.

  • Add the immutable, non-subclassable builtin with identity/hash/truthiness semantics, custom representation, read-only __name__, writable/deletable __module__, copy/deepcopy identity, and name-based pickle reduction.
  • Support sentinel operands in unions through the existing union implementation.
  • Traverse and clear owned references so string-subclass and mutable-module reference cycles can be collected.
  • Preserve the caller function's module snapshot, including module/eval/exec contexts.
  • Add six CPython-derived tests and six focused API, module, and GC regression tests. The snippet runs unconditionally on RustPython; the CPython comparison skips versions older than 3.15 before the unavailable builtin is imported.

This branch is based directly on RustPython ada43c4f82b9df4e260f1368038c31cd55c4a61e. It contains only the native sentinel implementation and its snippet. It does not change the advertised Python version, standard-library sources, import machinery, compile/module behavior, or generated workflows.

Reference and attribution

Behavior was compared with CPython v3.15.0rc2, commit 435c9e5a798c99653e3ab64ce29baed0e4f3dfee:

The six test_sentinel* methods are copied from that CPython test source; their assertions and AST semantics are preserved. Additional edge tests cover module capture, self/mutual/repr GC cycles, string subclasses, Unicode, signature/documentation, and identity/protocol behavior.

Validation

All commands below use the independent branch and its unchanged 3.14 Lib:

  • cargo build --locked --release --features sqlite: passed
  • Native sentinel snippet: all 12 tests passed on RustPython, despite its retained 3.14 version
  • CPython v3.15.0rc2 comparison: all 12 tests passed; CPython 3.14.7 cleanly takes the explicit version guard
  • Six upstream sentinel test methods: AST-equivalent to the exact rc2 source
  • rustpython -m test test_builtin test_types test_gc test_pickle: all four modules passed; 1,341 tests run, 104 skipped
  • Source isolation assertions: version 3.14, cpython-314, and site, importlib, and test.support all loaded from this branch's Lib
  • Configured pre-commit hooks and git diff --check: passed

The full snippet aggregate (pytest -v --timeout=60, CPython 3.14.7 plus the isolated RustPython binary) reports 469 passed, 13 failed. It is not a green aggregate result:

  • 10 failures stem from sandbox-denied AF_UNIX sockets: four manager tests and three multiprocessing snippets on both interpreters
  • 2 failures are the unchanged ownership-change test expecting PermissionError where this sandbox returns EINVAL
  • 1 is the unchanged CPython GC/import stress case reaching the 60-second limit; RustPython passes that case

Both sentinel harness cases and both SQLite cases pass. The CPython 3.14 sentinel harness case validates the version guard, while the separate 3.15rc2 run executes all 12 tests. All failing test source files are unchanged.

The remaining isolated-branch Rust checks also pass:

  • Workspace Rust tests: 1,345 passed, 18 ignored
  • Separate C-API tests: 115 passed, 4 ignored
  • Workspace and separate C-API all-targets Clippy: both passed without warnings

GitHub CI, CodSpeed, and pyperformance currently require maintainer approval for this fork; they have not run. This remains a draft for review.

Limitations

This change adds the runtime builtin and union support; it does not claim complete Python 3.15 compatibility or add third-party static type-checker support. Validation is on Linux x86_64; other platforms have not been exercised here.

AI assistance

OpenAI Codex assisted with implementation, CPython comparison, tests, review, and this PR description under maintainer direction. The runtime did not expose the exact model version. Reported checks are automated Linux x86_64 verification; no additional human-use or untested-platform verification is claimed.

Add unique sentinel values with copy/pickle identity, caller module capture, union support, and cycle collection. Compare constructor behavior and six upstream tests with CPython v3.15.0rc2; retain the existing Python target and bundled libraries.

Assisted-by: Codex:model-version-unavailable
@coderabbitai

coderabbitai Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codspeed

codspeed Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing youknowdot:cpython-315-sentinel (b994412) with main (c8d63e8)2

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. No successful run was found on main (ada43c4) during the generation of this report, so c8d63e8 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@fanninpm
fanninpm requested a review from youknowone October 2, 2026 14:17
Comment on lines +1 to +2
# Sentinel cases copied unchanged from CPython v3.15.0rc2 Lib/test/test_builtin.py.
# https://github.com/python/cpython/blob/v3.15.0rc2/Lib/test/test_builtin.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When RustPython decides to target CPython 3.15 (rather than CPython 3.14), the file in question will be updated by a semi-automated process.

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.

this can be simply replaced by test_builtin. so removing will be fine

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.

3 participants