Skip to content

Plan: fix nested classes stored as a placeholder on the outer class - #4

Closed
honeybeartech-homelab-automation[bot] wants to merge 3 commits into
team/nested-class-attr-basefrom
team/nested-class-attr
Closed

honeybeartech-homelab-automation[bot] wants to merge 3 commits into
team/nested-class-attr-basefrom
team/nested-class-attr

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, the last remaining case from custom-components#875. It stays on this fork and is never merged. Full plan: docs/plans/nested-class-attr.md. Its base is a copy of upstream's latest master, so this PR shows only our changes.

The bug. A class defined inside another class keeps a pyscript-internal placeholder as the outer class's attribute, not the class itself. So Outer.Inner | None fails with the same error as custom-components#875, for example in another class's type hints. type(Outer.Inner()) is Outer.Inner also gives False instead of True. I checked this on upstream's latest code and on custom-components#902's branch.

The fix. When the outer class is finished, swap each placeholder for the real class. It's a few lines in eval.py, plus tests checked against real Python.

Kept as it is: today a method can use the nested class by its bare name (Inner), which real Python doesn't allow. Scripts may rely on that, so the fix leaves it working, and a test pins it.

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 and custom-components#906 touch the same function, so whichever merges later needs a small update. We'd do that.

Team: Engineer, Security Officer and Auditor. QA is skipped as too small for this job. The plan check is skipped because its reviewer's monthly quota is still 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 nested class fix is ready. Please approve this PR (or comment "approve") to start the work.

…#875)

A class defined inside a class body was left on the outer class as the
internal EvalLocalVar placeholder instead of the class itself. So after
the outer class was created, Outer.Inner | None raised the same
TypeError as in custom-components#875, and type(Outer.Inner()) is Outer.Inner was False.

Before the outer class is created, replace each defined EvalLocalVar in
a plain dict class namespace with the value it holds. The placeholders
themselves are left alone, so methods that read the nested class by its
bare name keep working as before. A class namespace that isn't a plain
dict, such as a mapping returned by a custom metaclass __prepare__, is
left as it is.

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 nested class fix is built and checked, with nothing open. It's ready for your approval.

Done

A class defined inside another class is now stored on the outer class as the real class. So Outer.Inner | None works (it used to fail with the same error as custom-components#875), and type(Outer.Inner()) is Outer.Inner is True, as in real Python. It's a 5-line change in eval.py, plus tests that compare pyscript with real Python. Methods that use the nested class by its bare name keep working as they do today, and a test pins that.

Checks

  • Engineer's run: all 210 tests, ruff, pylint, mypy and codespell pass on Python 3.14. The fix applies cleanly onto upstream's latest code, and the new tests fail without it.
  • Security: clear.
  • Audit: two rounds.
    • Round 1 found one real problem: with an unusual custom class setup, the first version could leave the class half-updated. It's fixed by leaving those setups exactly as upstream does today.
    • Round 2 approved, with one wording fix to the commit message, which I made.
  • Skipped: QA, research and the plan check (small job; the plan check's quota is still used up). The plain-English check is also skipped for the quota.

Rejected findings

  • A method that replaces the nested class using nonlocal no longer updates Outer.Inner. Real Python refuses to run that code at all, so I didn't change anything for it. The commit message only promises that methods reading the nested class keep working.

What happens when you approve

  • I put the fix on a clean branch, fix-nested-class-attr, cut from upstream's latest code.
  • I give you a link that opens the upstream PR with its title and description already filled in. You click Create pull request, then close this PR.

Upstream PR title: Store nested classes themselves on the outer class (custom-components#875)

To approve

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

Upstream PR description

Refs custom-components#875

What was broken
A class defined inside another class's body stayed on the outer class as the internal EvalLocalVar placeholder, not the class itself. So once the outer class was built, Outer.Inner behaved differently from Python:

class Outer:
    class Inner:
        pass

class Bar:
    y: Outer.Inner | None = None   # TypeError: unsupported operand type(s) for |: 'EvalLocalVar' and 'NoneType'

type(Outer.Inner()) is Outer.Inner  # False

This is the same error as custom-components#875, for a class nested in another class.

What I changed
Just before the outer class is created, each defined EvalLocalVar in its namespace is replaced by the value it holds. The placeholder objects themselves aren't changed, so methods that use the nested class by its bare name keep working as before. A class namespace that isn't a plain dict, such as one returned by a custom metaclass __prepare__, is left as it is.

Testing

  • New tests in tests/test_unit_eval.py, compared against CPython:
    • Outer.Inner | None, at module level and in another class's annotations;
    • type(Outer.Inner()) is Outer.Inner and isinstance;
    • a dataclass with a nested dataclass;
    • two levels of nesting.
  • Further tests:
    • one pins the bare-name method behaviour;
    • two check custom __prepare__ namespaces.
  • The full test suite, ruff, pylint, mypy and codespell pass on Python 3.14.

Note
custom-components#902 and custom-components#906 also change ast_classdef, in different lines. Whichever merges later may need 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-nested-class-attr, cut from upstream's latest code. All 210 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