Skip to content

Plan: fix class bodies that raise leaving pyscript in the wrong scope - #3

Closed
honeybeartech-homelab-automation[bot] wants to merge 3 commits into
team/classdef-finally-basefrom
team/classdef-finally
Closed

honeybeartech-homelab-automation[bot] wants to merge 3 commits into
team/classdef-finally-basefrom
team/classdef-finally

Conversation

@honeybeartech-homelab-automation

@honeybeartech-homelab-automation honeybeartech-homelab-automation Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

This PR holds the plan for a small upstream fix. It stays on this fork and is never merged. Full plan: docs/plans/classdef-finally.md. Its base is a copy of upstream's latest master, so this PR shows only our changes.

The bug. When a class body raises an error, pyscript never leaves the class's scope. Example: with try: class Foo: raise ValueError ... except ValueError: pass at the top of a script, a later x = 5 is stored inside the failed class, not in the script. Real Python stores it in the script. I checked this on upstream's latest code.

The fix. Wrap the class body in try/finally, so pyscript always steps back out, plus tests checked against real Python. That's a few lines in eval.py.

How it's sent. One commit on a clean branch from upstream master, with a prefilled link for you to open the upstream PR. custom-components#902 touches the same lines, so whichever merges second needs a small update. We'd do that.

Team: Engineer, Security Officer and Auditor. Research and QA are skipped as too small for this job. The plan check is skipped because its reviewer's monthly quota is used up.

Contribution rules followed: docs/contributing.rst and tests/README.md, pre-commit (ruff, codespell, pylint, mypy), CI on Python 3.14, one small fix per PR with tests, and commits signed off.

Status: built and checked, with nothing open. See the latest report comment.

Approve this PR (or comment "approve") to finish. Close it to throw the work away.

Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com>
@honeybeartech-homelab-automation

Copy link
Copy Markdown
Author

@HoneyBearTech The plan for the class-body try/finally fix is ready. Please approve this PR (or comment "approve") to start the work.

ast_classdef switches into the class namespace before running the body
and only switched back after the body finished normally. When the body
raised, pyscript stayed in the class namespace, so after the exception
was caught, later statements bound their names in the failed class
instead of the script. Inside a function the scope stack was left one
entry too long.

Run the body in a try/finally so the outer scope is always restored,
including for the SyntaxError raised for return or break in a class
body. Add tests compared against CPython.

Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com>
Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com>
@honeybeartech-homelab-automation

Copy link
Copy Markdown
Author

@HoneyBearTech The class-body fix is built and checked, with nothing open. It's ready for your approval.

Done

When a class body raises an error, pyscript now always steps back out of the class, as real Python does. Before, after a caught error, later lines of the script landed inside the failed class. It's a 9-line change in eval.py, plus two tests that run each case in real Python and in pyscript and compare the results.

Checks

  • Engineer's run: all 204 tests, ruff, pylint, mypy and codespell pass on Python 3.14. The fix applies cleanly onto upstream's latest code, and both new tests fail without it.
  • Security: clear. No leaks, nothing names a tool, and the commit reads as your own work.
  • Audit: approved, with no findings. It also confirmed that the fix covers return or break in a class body and a cancelled task.
  • Skipped: QA, research and the plan check (the job is small; the plan check's quota is still used up). The plain-English check is also skipped for the same quota reason.

Rejected findings

None.

What happens when you approve

  • I put the fix on a clean branch, fix-classdef-scope, cut from upstream's latest code. It holds only the fix and its tests.
  • I give you a link that opens the upstream PR with its title and description already filled in. You click Create pull request.
  • This PR isn't merged; you can close it afterwards.

Upstream PR title: Restore the scope when a class body raises. The description explains the bug with a short example and mentions the overlap with custom-components#902.

To approve

Approve this PR (or comment "approve") to finish. Close it to throw the work away.

Upstream PR description

What was broken
When a class body raises, pyscript stayed in the class's namespace. After the exception was caught, later statements bound their names in the failed class instead of the script:

try:
    class Foo:
        raise ValueError("boom")
except ValueError:
    pass

x = 5   # ended up in the failed class's namespace, not in globals

Inside a function, the scope stack was left one entry too long.

Why
ast_classdef pushes the current symbol table and switches into the class namespace before running the body, but it only switched back after the body finished normally.

What I changed
The class body now runs in a try/finally, so the outer scope is always restored, including for the SyntaxError raised for return or break in a class body.

Testing

  • Two new tests in tests/test_unit_eval.py, compared against CPython:
    • a class body that raises, caught at module level, in a function, and in a nested class;
    • an uncaught error reaching the caller, with the next evaluation unaffected.
  • The full test suite, ruff, pylint, mypy and codespell pass on Python 3.14.

Note
custom-components#902's second commit also changes ast_classdef. Whichever merges second needs a small rebase; I'm happy to do it.

@honeybeartech-homelab-automation

Copy link
Copy Markdown
Author

@HoneyBearTech Thanks for approving. The fix is on its own clean branch, fix-classdef-scope, cut from upstream's latest code. All 204 tests pass there.

Click this link, check that the title and description are filled in, then click Create pull request. It opens from your account against custom-components/pyscript:

After that, you can close this PR without merging.

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