diff --git a/custom_components/pyscript/eval.py b/custom_components/pyscript/eval.py index 88d24d4..890d356 100644 --- a/custom_components/pyscript/eval.py +++ b/custom_components/pyscript/eval.py @@ -1120,6 +1120,11 @@ async def ast_classdef(self, arg): if "__init__" in sym_table: sym_table["__init__evalfunc_wrap__"] = sym_table["__init__"] del sym_table["__init__"] + # store nested classes themselves on the class; the EvalLocalVar objects stay valid for methods + if type(sym_table) is dict: # pylint: disable=unidiomatic-typecheck + for name, val in sym_table.items(): + if isinstance(val, EvalLocalVar) and val.is_defined(): + sym_table[name] = val.get() cls = metaclass(arg.name, tuple(bases), sym_table, **keywords) if inspect.iscoroutine(cls): cls = await cls diff --git a/docs/plans/nested-class-rebase.md b/docs/plans/nested-class-rebase.md new file mode 100644 index 0000000..e29b7d8 --- /dev/null +++ b/docs/plans/nested-class-rebase.md @@ -0,0 +1,50 @@ +# Plan: nested-class-rebase + +## What Sean asked for +Follow up on upstream [custom-components/pyscript#907](https://github.com/custom-components/pyscript/pull/907), the nested class fix. + +## What we found +- The maintainer approved #907 and asked for two things: + 1. **Rebase onto current `master`.** GitHub shows #907 as conflicting. Only `tests/test_unit_eval.py` conflicts: the #906 tests were also added at the end of that file. The `eval.py` change still applies cleanly. + 2. **One docstring:** `test_nested_class_bare_name_in_method` pins behaviour that differs from real Python (#902 lists it as a known limit). Its docstring should say it documents current behaviour, not the intended one. +- Since that review, two more PRs merged upstream (#908 and #909). We tried the rebase on top of them too: the same file conflicts, in the same way (both sides only add tests at the end of the file). +- The maintainer also checked more cases against real Python and found the fix covers them. No code change was asked for. + +## What we'll change +1. **Rebase the fix commit onto upstream's latest `master`.** Keep both the new upstream tests and ours in `tests/test_unit_eval.py`; nothing else changes. *Check:* the `eval.py` change is the same as in #907, and every upstream test is still there. +2. **Update the docstring** of `test_nested_class_bare_name_in_method` to say it pins current pyscript behaviour, which differs from Python (a known limit listed in #902), so nobody mistakes it for the intended behaviour. *Check:* the reviewer's wording request is met; the test is unchanged otherwise. +3. **All project checks** on Python 3.14, as CI runs them: `pytest`, `ruff check`, `ruff format --check`, `pylint`, `mypy`, `codespell`. *Check:* all pass; the new tests still fail without the `eval.py` change. +4. **Update #907.** After your final approval, the rebased commit replaces the old one on the fork branch `fix-nested-class-attr`, which #907 uses. Rebasing means replacing the branch's history (a "force push"); that's what the maintainer asked for, and only #907 uses that branch. You'll also get a short reply to paste on #907, because the app can't write on the upstream repo. + +## Contribution rules that apply +- `docs/contributing.rst` and `tests/README.md`: dev setup from `tests/requirements_test.txt`, pre-commit hooks, `pytest`, PRs from a fork. +- Pre-commit runs ruff (check and format, line length 109), codespell, pylint and mypy. CI (`validate.yml`, Python 3.14) runs hassfest, ruff, pytest with coverage, pylint and mypy. +- No PR template; we follow the maintainer's habits: one small fix per PR, with tests. Commits are signed off. + +## Who's on it +Engineer, Security Officer and Auditor. Research and the plan check are skipped: this is a small, mechanical follow-up and we already did the trial rebase. QA is skipped too (the Auditor runs the tests). No second security review: nothing security-related is touched. + +## Risks +- **Upstream moves again** before we push. We'll check right before pushing and redo the rebase if needed. +- **The maintainer re-reviews.** A new commit on #907 may need their approval again; they asked for it, so that's expected. + +## Out of scope +- Any change to how the fix works. +- Making methods stop seeing nested classes by bare name (a Python difference, but changing it could break scripts). + +--- + +## Record (2026-11-21) + +**Status:** built and checked, with nothing open. Not yet sent to #907. + +**What shipped on this branch:** one commit, `store nested classes themselves on the outer class (#875)` (`0e28002`): #907's fix rebased onto upstream `master` (`c20990b`, after #906, #908 and #909). The `eval.py` change is the same as in #907. In `tests/test_unit_eval.py` all upstream tests are kept unchanged and ours follow them. The docstring of `test_nested_class_bare_name_in_method` now says it pins current pyscript behaviour that differs from Python (a known limit listed in #902). All 217 tests, ruff (check and format), pylint, mypy and codespell pass on Python 3.14. Without the `eval.py` change, the five comparison cases fail. + +**Review rounds:** +- Security: clear. It noted that this plan file must not go upstream; only the fix commit does. +- Audit: approved, no findings. It also checked four more cases against Python (bound methods of temporary and saved instances, a failed nested-class redefinition, a class inside a function that declares globals). + +**Decisions:** +- Resolved the conflict by keeping both sets of appended tests; no other change. +- For #907, the fix commit is copied onto upstream `master` on its own (without this plan commit) and replaces the old commit on `fix-nested-class-attr`. +- Research, plan check and QA were skipped as too small for this job. diff --git a/tests/test_unit_eval.py b/tests/test_unit_eval.py index 0ef8d9b..2414a64 100644 --- a/tests/test_unit_eval.py +++ b/tests/test_unit_eval.py @@ -3501,3 +3501,221 @@ async def test_class_body_error_reaches_caller(hass): await Function.waiter_sync() await Function.waiter_stop() await Function.reaper_stop() + + +NESTED_CLASS_CASES = { + "union_module_level": ( + "class Outer:\n class Inner:\n pass\nres = (Outer.Inner | None) == (Outer.Inner | None)\n" + ), + "union_other_class_annotation": ( + "class Outer:\n class Inner:\n pass\n" + "class Bar:\n y: Outer.Inner | None = None\n" + "res = [Bar.y, Bar.__annotations__['y'] == (Outer.Inner | None)]\n" + ), + "identity": ( + "class Outer:\n class Inner:\n pass\n" + "res = [type(Outer.Inner()) is Outer.Inner, isinstance(Outer.Inner(), Outer.Inner)]\n" + ), + "dataclass": ( + "from dataclasses import dataclass\n" + "@dataclass\nclass Outer:\n @dataclass\n class Inner:\n a: int = 1\n" + " x: Inner | None = None\n" + "res = [Outer().x, Outer(Outer.Inner()).x.a, type(Outer.Inner()) is Outer.Inner]\n" + ), + "two_levels": ( + "class Outer:\n class Mid:\n class Inner:\n v = 7\n" + "res = [Outer.Mid.Inner.v, type(Outer.Mid.Inner()) is Outer.Mid.Inner," + " (Outer.Mid.Inner | None) == (Outer.Mid.Inner | None)]\n" + ), +} + + +@pytest.mark.parametrize("name", list(NESTED_CLASS_CASES)) +async def test_nested_class_attr_matches_python(hass, name): + """Test that a class defined in a class body is the class itself, compared with CPython.""" + hass.data[DOMAIN] = {CONFIG_ENTRY: MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: True})} + Function.init(hass) + State.init(hass) + State.register_functions() + TrigTime.init(hass) + DecoratorRegistry.init(hass) + + source = NESTED_CLASS_CASES[name] + expected_ns: dict = {} + exec(source, expected_ns) # pylint: disable=exec-used + expected = expected_ns["res"] + + global_ctx = GlobalContext("test", global_sym_table={}, manager=GlobalContextMgr) + ast = AstEval("test", global_ctx=global_ctx) + Function.install_ast_funcs(ast) + ast.parse(source) + try: + await ast.eval() + assert global_ctx.global_sym_table["res"] == expected + finally: + await Function.waiter_sync() + await Function.waiter_stop() + await Function.reaper_stop() + + +async def test_nested_class_bare_name_in_method(hass): + """ + Test that a method can use a nested class by its bare name. + + This pins current pyscript behavior that differs from Python, where a method + can't see a nested class by its bare name and raises NameError (a known + limit listed in #902), so it is not the intended behavior. + """ + hass.data[DOMAIN] = {CONFIG_ENTRY: MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: True})} + Function.init(hass) + State.init(hass) + State.register_functions() + TrigTime.init(hass) + DecoratorRegistry.init(hass) + + # pyscript only: CPython raises NameError here, so this can't be compared with CPython + global_ctx = GlobalContext("test", global_sym_table={}, manager=GlobalContextMgr) + ast = AstEval("test", global_ctx=global_ctx) + Function.install_ast_funcs(ast) + ast.parse( + "class Outer:\n class Inner:\n v = 4\n def make(self):\n return Inner.v\n" + "res = [Outer().make(), Outer.Inner.v]\n" + ) + await ast.eval() + assert global_ctx.global_sym_table["res"] == [4, 4] + + await Function.waiter_sync() + await Function.waiter_stop() + await Function.reaper_stop() + + +class NoKeysNamespace: + """Class namespace that supports item access but has no keys() or items().""" + + def __init__(self): + """Initialize the namespace.""" + self.d = {} + + def __setitem__(self, key, value): + """Set an item.""" + self.d[key] = value + + def __getitem__(self, key): + """Get an item.""" + return self.d[key] + + def __contains__(self, key): + """Check for an item.""" + return key in self.d + + def __delitem__(self, key): + """Delete an item.""" + del self.d[key] + + +class NoKeysMeta(type): + """Metaclass whose __prepare__ returns a namespace without keys().""" + + @classmethod + def __prepare__(mcs, name, bases, **kwargs): + """Return the custom namespace.""" + return NoKeysNamespace() + + def __new__(mcs, name, bases, ns, **kwargs): + """Build the class from the custom namespace.""" + return super().__new__(mcs, name, bases, dict(ns.d)) + + +NESTED_PREPARE_SOURCE = """ +class Outer(metaclass=NoKeysMeta): + class Inner: + pass + v = 3 + +res = [Outer.v, Outer.Inner is not None] +""" + + +async def test_nested_class_prepare_without_keys(hass): + """Test that a class namespace from __prepare__ that can't be iterated still builds the class.""" + hass.data[DOMAIN] = {CONFIG_ENTRY: MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: True})} + Function.init(hass) + State.init(hass) + State.register_functions() + TrigTime.init(hass) + DecoratorRegistry.init(hass) + + expected_ns: dict = {"NoKeysMeta": NoKeysMeta} + exec(NESTED_PREPARE_SOURCE, expected_ns) # pylint: disable=exec-used + global_ctx = GlobalContext("test", global_sym_table={"NoKeysMeta": NoKeysMeta}, manager=GlobalContextMgr) + ast = AstEval("test", global_ctx=global_ctx) + Function.install_ast_funcs(ast) + ast.parse(NESTED_PREPARE_SOURCE) + await ast.eval() + assert global_ctx.global_sym_table["res"] == expected_ns["res"] == [3, True] + + await Function.waiter_sync() + await Function.waiter_stop() + await Function.reaper_stop() + + +class RejectSecondRewrite(dict): + """Class namespace that refuses to rebind "Second".""" + + def __setitem__(self, key, value): + """Set an item, but only allow "Second" to be bound once.""" + if key == "Second" and key in self: + raise TypeError("Second may only be bound once") + super().__setitem__(key, value) + + +class CheckNamespaceMeta(type): + """Metaclass that requires its two nested classes to arrive in the same form.""" + + @classmethod + def __prepare__(mcs, name, bases, **kwargs): + """Return the custom namespace.""" + return RejectSecondRewrite() + + def __new__(mcs, name, bases, ns, **kwargs): + """Build the class, checking the namespace is consistent.""" + if type(ns["First"]) is not type(ns["Second"]): + raise ValueError("mixed namespace representations") + return super().__new__(mcs, name, bases, dict(ns)) + + +NESTED_REJECT_SOURCE = """ +class Outer(metaclass=CheckNamespaceMeta): + class First: + pass + class Second: + pass + +res = Outer.__name__ +""" + + +async def test_nested_class_prepare_dict_subclass(hass): + """Test that a custom __prepare__ namespace is left as it is and the class still builds.""" + hass.data[DOMAIN] = {CONFIG_ENTRY: MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: True})} + Function.init(hass) + State.init(hass) + State.register_functions() + TrigTime.init(hass) + DecoratorRegistry.init(hass) + + expected_ns: dict = {"CheckNamespaceMeta": CheckNamespaceMeta} + exec(NESTED_REJECT_SOURCE, expected_ns) # pylint: disable=exec-used + global_ctx = GlobalContext( + "test", global_sym_table={"CheckNamespaceMeta": CheckNamespaceMeta}, manager=GlobalContextMgr + ) + ast = AstEval("test", global_ctx=global_ctx) + Function.install_ast_funcs(ast) + ast.parse(NESTED_REJECT_SOURCE) + try: + await ast.eval() + assert global_ctx.global_sym_table["res"] == expected_ns["res"] == "Outer" + finally: + await Function.waiter_sync() + await Function.waiter_stop() + await Function.reaper_stop()