Skip to content

Use CPython 3.14's wording for interpreter error messages - #950

Merged
craigbarratt merged 1 commit into
masterfrom
cpython-error-messages
Dec 5, 2026
Merged

craigbarratt merged 1 commit into
masterfrom
cpython-error-messages

Conversation

@craigbarratt

Copy link
Copy Markdown
Member

pyscript raised its own wording for many errors that Python raises itself. Running tests/test_unit_eval.py's expected exceptions under CPython 3.14 showed about 20 with the same exception type but different text. This switches them to CPython 3.14's messages.

Error Before Now (as CPython 3.14)
unpacking too few values to unpack (expected 3) not enough values to unpack (expected 3, got 2)
too many values to unpack (expected 2) too many values to unpack (expected 2, got 3); the count is given only for a list, tuple or dict, as in Python
cannot unpack non-iterable object cannot unpack non-iterable int object
missing arguments f() missing 1 required positional arguments f() missing 2 required positional arguments: 'a' and 'b'
f() missing required keyword-only arguments f() missing 1 required keyword-only argument: 'b'
too many arguments f() called with too many positional arguments f() takes from 1 to 2 positional arguments but 3 were given, including the (and 1 keyword-only argument) form
unexpected keyword f() called with unexpected keyword arguments: a, c f() got an unexpected keyword argument 'a', the first one in call order
statements return statement outside function, break/continue statement outside loop 'return' outside function, 'break' outside loop, 'continue' not properly in loop
nonlocal statement outside function nonlocal declaration not allowed at module level
unassigned variables local variable 'x' referenced before assignment cannot access local variable 'x' where it is not associated with a value
name 'x' is not defined (an enclosing function's variable, read or del) cannot access free variable 'x' where it is not associated with a value in enclosing scope
global name 'x' is not defined name 'x' is not defined

Two small fixes came along:

  • The missing-positional count was wrong when a later argument was passed by keyword. For example, def f(a, b) called as f(b=2) said missing 2. It now names just 'a'.
  • An exception other than TypeError raised while iterating the value being unpacked is no longer replaced by "cannot unpack non-iterable object".

Not changed

  • CPython 3.10+ names a method with its class, as in C.m() missing .... Pyscript functions don't track a qualified name, so pyscript still says m() missing ....
  • global x at module level still raises a SyntaxError. Python allows it, so it has no message to match.
  • Messages for pyscript-only features (state variables, task.*, import restrictions, @pyscript_compile arguments) are unchanged.

Testing

  • A comparison of about 40 cases with CPython 3.14 (unpacking of every iterable type, each argument-error variant, the statements, unassigned local, free and global variables, and del) matches everywhere except the method-name prefix.
  • The tests' expected messages are updated, and tests/test_unit_eval.py's expected exceptions no longer differ from CPython, apart from pyscript-only features.
  • pytest (307 passed), ruff, pylint, mypy and pre-commit pass.

#947 currently raises the old UnboundLocalError wording; it should use the new message once this is merged.

🤖 Generated with Claude Code

pyscript raised its own wording for many errors that Python raises
itself. Use CPython 3.14's messages instead:

- unpacking: "not enough values to unpack (expected 3, got 2)", "too
  many values to unpack (expected 2, got 3)" (the count only for a list,
  tuple or dict, as in Python), and "cannot unpack non-iterable int
  object"; an exception other than TypeError from iter() is no longer
  replaced
- calls: "missing 2 required positional arguments: 'a' and 'b'", "takes
  from 1 to 2 positional arguments but 3 were given" (including the
  keyword-only variant), "missing 1 required keyword-only argument: 'b'"
  and "got an unexpected keyword argument 'a'"; the missing count was
  also wrong when a later argument was passed by keyword
- "'return' outside function", "'break' outside loop", "'continue' not
  properly in loop", "nonlocal declaration not allowed at module level"
- "cannot access local variable 'x' where it is not associated with a
  value" (UnboundLocalError), "cannot access free variable 'x' where it
  is not associated with a value in enclosing scope", also for del, and
  "name 'x' is not defined" for a global

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@craigbarratt
craigbarratt merged commit 74c1543 into master Dec 5, 2026
11 checks passed
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