Skip to content

PEP 671: implement experimental late-bound argument defaults - #159455

Draft
yotatameguchi-boop wants to merge 1 commit into
python:mainfrom
yotatameguchi-boop:pep-671-late-bound-defaults
Draft

yotatameguchi-boop wants to merge 1 commit into
python:mainfrom
yotatameguchi-boop:pep-671-late-bound-defaults

Conversation

@yotatameguchi-boop

Copy link
Copy Markdown

This implements name=>expression defaults from draft PEP 671 on current CPython main. An omitted argument evaluates its expression in the function's runtime scope; an explicitly supplied argument suppresses evaluation, including None and Ellipsis.

This is an experimental reference implementation for evaluating the proposal. PEP 671 remains Draft; this change does not represent acceptance of the language proposal. The original reference implementation informed the design, but the implementation targets the current compiler, stack-reference interpreter, and function attribute machinery.

The change supports positional-only, positional-or-keyword, and keyword-only defaults; functions, lambdas, methods, generic functions, coroutines, generators, and captured parameters. It preserves left-to-right evaluation, original normalized expression descriptions before constant folding, and argument validation before evaluating defaults. It adds QUERY_FAST/QUERY_DEREF, function metadata in __defaults_extra__/__kwdefaults_extra__, AST round-tripping, and signature rendering through inspect.Parameter.late_default. FullArgSpec retains its seven-field shape.

Validation on Apple Silicon macOS with Apple Clang 21, debug builds:

  • GIL build: 1,989 tests across 19 relevant test modules; test_sys (105 tests); test_capi (1,611 tests). All test runs succeeded, with platform/resource skips.
  • Free-threaded build with -X gil=0: 1,397 tests across 12 relevant modules; all test runs succeeded, with skips. Verified sys._is_gil_enabled() is False.
  • 42 focused feature tests cover binding order, explicit values, mutable defaults, closures/cells, source preservation, introspection/pickling, mutated metadata, invalid ASTs/syntax, C vectorcall, concurrency, and GC cycles.
  • test_late_defaults -R 3:3 succeeded with no leaks during the measured repetitions.
  • AST/parser/opcode/global-object generated files regenerated; git diff --check succeeded.

Implementation limits: functions with late-bound defaults currently bypass existing call specializations and conservatively treat all parameters as potentially unbound for local-variable analysis. Two metadata pointers are added to every function object. The internal function layout and bytecode magic change. No public C API entry points are added. Windows/Linux, JIT-enabled execution, performance benchmarks, and a Sphinx documentation build have not been validated.

AI assistance was used for source investigation, implementation, documentation, and tests. The human contributor has confirmed review of the implementation and this description in accordance with CPython's AI tooling policy.

A matching upstream tracking issue has not been established. This draft is submitted as an experimental implementation of PEP 671 for discussion, not as an accepted language change.

@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@bedevere-app

bedevere-app Bot commented Oct 15, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

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.

1 participant