From 97c0e4c57f577a4a524fc8504744e1e54adba0ea Mon Sep 17 00:00:00 2001 From: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:35:15 +0000 Subject: [PATCH 1/5] docs: plan for global-path-875 Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> --- docs/plans/global-path-875.md | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 docs/plans/global-path-875.md diff --git a/docs/plans/global-path-875.md b/docs/plans/global-path-875.md new file mode 100644 index 0000000..94fc9a8 --- /dev/null +++ b/docs/plans/global-path-875.md @@ -0,0 +1,47 @@ +# Plan: global-path-875 + +## What Sean asked for +Act on the maintainer's review of upstream PR custom-components/pyscript#902 (the #875 fix): they approved it, and asked whether we want to also fix the `global` path in the same PR or leave it for a follow-up. + +## What we found +- The maintainer's three suggested changes fix every `global` case in their comment, but they bring back a problem we removed from #875 earlier: after `from mod import Foo`, a function doing `global Foo; Foo = Other` (or `del Foo`) changes `mod`'s own `Foo`, for every script that imports `mod`. This happens because `from mod import Foo` copies the module's internal wrapper object, so both scripts share one box. +- The same sharing already causes a bug on master today, even without `global`: after `from mod import Foo`, a plain top-level `Foo = Other` or `del Foo` in the importing script changes `mod`'s `Foo`. +- The maintainer's changes also make one case they mention worse: after `global Foo; del Foo`, defining `Foo` again raises `NameError` in functions defined earlier. +- Two small extra changes fix both: `from mod import ...` takes the value out of the wrapper (as real Python does), and a class definition reuses the existing wrapper instead of making a new one. With them, all 202 existing tests pass and about 40 test cases behave like real Python. That covers the maintainer's cases, cross-module imports, closures, and `del` followed by a redefinition. + +## Recommendation: leave #902 as it is and send this as a follow-up PR +The change is bigger than the maintainer's sketch and changes import behaviour slightly (see Risks), so it deserves its own review. #902 is already approved and can merge now. One fix per PR also matches the project's habits. +(If Sean prefers adding it to #902, comment so on this PR; the work is the same, only the delivery changes.) + +## What we'll change +1. **Reply on #902 (Sean posts it).** A short thank-you comment for Sean to post from his account: "please merge as is; the global path follows in a separate PR". It also explains the cross-module catch in their suggestion. *Check:* text reviewed for tone and accuracy before Sean posts it. The PR's description also ends with an empty "Known limits" heading; we'll give Sean one line to fill it in. +2. **`custom_components/pyscript/eval.py`, five small edits:** + - assigning through `global` updates the existing wrapper instead of replacing it; + - deleting through `global` marks the wrapper undefined instead of removing it; + - reading through `global` returns the class, not the wrapper; + - `from mod import X` and `from mod import *` copy the value, not the wrapper; + - a class definition reuses an existing wrapper for its name, and keeps the old class visible until the new one is finished (as Python does). + *Check:* full test suite. +3. **Tests in `tests/test_unit_eval.py`** (or a nearby test file if module imports need it). They cover each case in the maintainer's comment (`|`, `is`, setting an attribute, rebinding, `del`, `del` then redefine), closures, and the cross-module cases (`from mod import Foo` / `import *`, then a rebind or `del` in the importer; a second script still sees the original). Each test is checked against what real Python gives. *Check:* the new tests fail on the current branch and pass with step 2. +4. **All project checks** on Python 3.14, as CI runs them: `pytest`, `ruff check`, `ruff format --check`, `pylint` and `mypy` on `custom_components/pyscript/*.py tests/*.py`, plus codespell from pre-commit. +5. **Upstream PR.** A clean branch on the fork, cut from the #902 branch, holding only the code and test commits (no plan or notes). We also write a prefilled link for Sean to open the upstream PR from his account. Its title and description explain the change, credit the maintainer's suggestion, name the import behaviour change, list the known limits, say it builds on #902, and say "Refs #875". After #902 merges, the branch is rebased onto upstream `master`. + +## Contribution rules that apply +- `docs/contributing.rst` points to `tests/README.md`: a dev setup from `tests/requirements_test.txt`, pre-commit hooks, and `pytest`. PRs come from a fork. +- Pre-commit runs ruff (check and format, line length 109), codespell, check-json, yamllint, pylint and mypy. CI (`validate.yml`, Python 3.14) runs hassfest, ruff, pytest with coverage, pylint and mypy. +- No PR template exists. We follow the maintainer's observed habits: small focused PRs, a clear description, and tests for every fix. +- Commits signed off (`git commit -s`), authored as Sean, written as solo work. + +## Who's on it +Engineer (code and tests), QA (tests and plan check), Security Officer (leaks and risky code), Auditor (independent review of the logic), and a plain-English check of the report. No second security review: the change doesn't touch credentials, shell commands, the network, setup scripts, CI or dependencies. + +## Risks +- **Import behaviour change.** Today `from mod import Foo` keeps following later changes to `Foo` inside `mod`, because both share the wrapper. After this change it takes a snapshot, as Python does. An app that relied on the old behaviour would stop seeing those updates. Code using `mod.Foo` is not affected. This will be stated plainly in the upstream PR. +- **Redefining a class.** The wrapper is reused, so while the new class body runs, the name still means the old class, and if the body fails the old class stays. Real Python does the same. A first-time definition works as it does today. Tests will cover both. +- **The maintainer may prefer their smaller version.** The PR text will show the failing cross-module case, so they can judge. + +## Out of scope (stated as known limits in the upstream PR) +- `import mod` then `mod.Foo | None` or `mod.Foo = Other` (attribute access on a module still hands out the wrapper). +- A lambda or `@pyscript_compile` function reading a module-level class that is later rebound. +- `global Zed; del Zed` on a name that was never defined doesn't raise `NameError`. +- No change to #902 itself, beyond the description line for Sean. From 9a3c678eee6619a1778b4c4081b451989c64de81 Mon Sep 17 00:00:00 2001 From: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:38:58 +0000 Subject: [PATCH 2/5] docs: plan update - add the global path to #902 Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> --- docs/plans/global-path-875.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/plans/global-path-875.md b/docs/plans/global-path-875.md index 94fc9a8..bf807c8 100644 --- a/docs/plans/global-path-875.md +++ b/docs/plans/global-path-875.md @@ -9,12 +9,11 @@ Act on the maintainer's review of upstream PR custom-components/pyscript#902 (th - The maintainer's changes also make one case they mention worse: after `global Foo; del Foo`, defining `Foo` again raises `NameError` in functions defined earlier. - Two small extra changes fix both: `from mod import ...` takes the value out of the wrapper (as real Python does), and a class definition reuses the existing wrapper instead of making a new one. With them, all 202 existing tests pass and about 40 test cases behave like real Python. That covers the maintainer's cases, cross-module imports, closures, and `del` followed by a redefinition. -## Recommendation: leave #902 as it is and send this as a follow-up PR -The change is bigger than the maintainer's sketch and changes import behaviour slightly (see Risks), so it deserves its own review. #902 is already approved and can merge now. One fix per PR also matches the project's habits. -(If Sean prefers adding it to #902, comment so on this PR; the work is the same, only the delivery changes.) +## Decision: add it to #902 +Sean chose to add the `global` fix to #902 itself, as the maintainer offered, rather than send a separate PR. The new commits go on top of the existing #902 commit, so the maintainer reviews only what's new. ## What we'll change -1. **Reply on #902 (Sean posts it).** A short thank-you comment for Sean to post from his account: "please merge as is; the global path follows in a separate PR". It also explains the cross-module catch in their suggestion. *Check:* text reviewed for tone and accuracy before Sean posts it. The PR's description also ends with an empty "Known limits" heading; we'll give Sean one line to fill it in. +1. **Reply on #902 (Sean posts it, after the new commits are pushed).** A short comment for Sean to post from his account. It thanks the maintainer, says the `global` path is now included, and explains why it has two changes beyond their sketch: the cross-module catch, and redefinition after `del`. It also points out the change to import behaviour. We'll also give Sean an updated description for #902 (the current one ends with an empty "Known limits" heading). *Check:* text reviewed for tone and accuracy before Sean posts it. 2. **`custom_components/pyscript/eval.py`, five small edits:** - assigning through `global` updates the existing wrapper instead of replacing it; - deleting through `global` marks the wrapper undefined instead of removing it; @@ -24,7 +23,7 @@ The change is bigger than the maintainer's sketch and changes import behaviour s *Check:* full test suite. 3. **Tests in `tests/test_unit_eval.py`** (or a nearby test file if module imports need it). They cover each case in the maintainer's comment (`|`, `is`, setting an attribute, rebinding, `del`, `del` then redefine), closures, and the cross-module cases (`from mod import Foo` / `import *`, then a rebind or `del` in the importer; a second script still sees the original). Each test is checked against what real Python gives. *Check:* the new tests fail on the current branch and pass with step 2. 4. **All project checks** on Python 3.14, as CI runs them: `pytest`, `ruff check`, `ruff format --check`, `pylint` and `mypy` on `custom_components/pyscript/*.py tests/*.py`, plus codespell from pre-commit. -5. **Upstream PR.** A clean branch on the fork, cut from the #902 branch, holding only the code and test commits (no plan or notes). We also write a prefilled link for Sean to open the upstream PR from his account. Its title and description explain the change, credit the maintainer's suggestion, name the import behaviour change, list the known limits, say it builds on #902, and say "Refs #875". After #902 merges, the branch is rebased onto upstream `master`. +5. **Update #902.** After the final approval here, push only the code and test commits (no plan or notes) onto the #902 branch (`fix-class-annotation-union-875` on the fork). This updates upstream #902 directly. The existing #902 commit is kept as it is, with no history rewrite or force-push. The commit message credits the maintainer's suggestion, names the import behaviour change, and lists the known limits. ## Contribution rules that apply - `docs/contributing.rst` points to `tests/README.md`: a dev setup from `tests/requirements_test.txt`, pre-commit hooks, and `pytest`. PRs come from a fork. @@ -38,10 +37,11 @@ Engineer (code and tests), QA (tests and plan check), Security Officer (leaks an ## Risks - **Import behaviour change.** Today `from mod import Foo` keeps following later changes to `Foo` inside `mod`, because both share the wrapper. After this change it takes a snapshot, as Python does. An app that relied on the old behaviour would stop seeing those updates. Code using `mod.Foo` is not affected. This will be stated plainly in the upstream PR. - **Redefining a class.** The wrapper is reused, so while the new class body runs, the name still means the old class, and if the body fails the old class stays. Real Python does the same. A first-time definition works as it does today. Tests will cover both. -- **The maintainer may prefer their smaller version.** The PR text will show the failing cross-module case, so they can judge. +- **The maintainer may prefer their smaller version.** The reply will show the failing cross-module case, so they can judge. +- **#902's approval goes stale.** New commits mean the maintainer reviews again before merging. #902 is wider than it was, which they invited. ## Out of scope (stated as known limits in the upstream PR) - `import mod` then `mod.Foo | None` or `mod.Foo = Other` (attribute access on a module still hands out the wrapper). - A lambda or `@pyscript_compile` function reading a module-level class that is later rebound. - `global Zed; del Zed` on a name that was never defined doesn't raise `NameError`. -- No change to #902 itself, beyond the description line for Sean. +- No change to the existing #902 commit. From a3609ce51009cefdbd7e646a40130cb05e20ae1a Mon Sep 17 00:00:00 2001 From: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:59:20 +0000 Subject: [PATCH 3/5] update module-level names in place when assigned or deleted through global (#875) A module-level class is stored in the global symbol table as an EvalLocalVar, and functions defined at module level hold on to that wrapper. Used through a global declaration, the class still came back wrapped, so inside a function with "global Foo", "Foo | None" raised TypeError, "Foo is type(Foo())" was False and "Foo.x = 3" set x on the wrapper. Assigning or deleting through global replaced or removed the table entry, so functions defined earlier kept seeing the old class after "global Foo; Foo = Other" or "global Foo; del Foo". As suggested in review, assignment through global now calls .set() on an existing EvalLocalVar instead of replacing it, del through global calls set_undefined() on it (and raises NameError if it is already undefined, like the module-level branch), and a load through global unwraps it when it is defined. An undefined one is left wrapped, as the lookup that falls through to the global symbol table already does, so a class defined through global can still refer to itself in its annotations. Updating a wrapper in place is only safe if no other binding shares it, and functions defined earlier should also see a class defined again or imported under the same name. So: - Assignment, import and from-import now bind names through one helper, assign_name(). It stores the value held by a defined EvalLocalVar rather than the wrapper, so "from mod import Foo", "from mod import *" and "Foo = globals()['Original']" no longer share another binding's wrapper. Otherwise "global Foo; Foo = Other" or "del Foo" in the importer would change mod's own Foo, for every script importing mod. A plain module-level "Foo = Other" or "del Foo" after "from mod import Foo" already did this before. - Reading an attribute of a module, as in "mod.Foo", now also gives the value held by mod's EvalLocalVar, so "Foo = mod.Foo" and "globals()['Foo'] = mod.Foo" store the class itself, and "mod.Foo | None", "mod.Foo is mod.modread()" and isinstance(obj, mod.Foo) work. For a name mod has deleted it raises AttributeError, as in Python. "mod.Foo = Other" updates mod's EvalLocalVar in place, so mod's own functions see the new value, as in Python. - Like assignment, import and from-import now update an existing EvalLocalVar for the name, so functions defined earlier see the imported value, and a name declared global in a function is bound in the global table instead of locally. - Names bound by import or from-import inside a function are now local to it, as in Python, so a nested function can use them; for example "import mod as m" inside a function used to be invisible to a nested function. In a function with a nested function, reading any local name before it is bound, or after it is deleted, now raises UnboundLocalError instead of NameError, as in Python. Without a nested function, reading an imported name before the import raises UnboundLocalError when a module-level name of that name exists, instead of finding it. - In a class body, assignment, import, del, and class and def statements bind in the class namespace, even inside a function that declares the name global, and reading a name finds the class's own binding before the global one, as in Python. Assignment and import there replace an EvalLocalVar left by a nested class instead of updating it, so a method doesn't pick up the new value as an enclosing variable. - "from mod import Foo" of a name mod has deleted raises ImportError with Python's message, and "from mod import *" skips it, since del now leaves an undefined EvalLocalVar in mod. - A class statement always made a new EvalLocalVar, so functions defined earlier never saw a class defined again under the same name, and inside the new class body the name was undefined. It now reuses an existing wrapper and sets the new class once the class statement completes. Until then the name still holds the old class, also if the class body raises, as in Python. A first definition works as before. This changes how imports and aliases behave: "from mod import Foo" and "Foo = mod.Foo" now take a snapshot of mod's Foo, as in Python, and no longer follow later changes to Foo inside mod; "mod.Foo" itself does. Known limits: - getattr(mod, "Foo"), mod.__dict__ and vars(mod) still hand out mod's EvalLocalVar, and so does "mod.Foo" inside a lambda or @pyscript_compile function - a lambda or @pyscript_compile function gets a module-level class as its EvalLocalVar: reading an attribute works and follows a later rebind, but calling it or "|" raises TypeError, and "is", "==", isinstance() and issubclass() against the class give False - in a function without a nested function, reading an imported name before the import gives the builtin of that name if there is one, and raises NameError if there is neither a module-level name nor a builtin, instead of UnboundLocalError - a dotted import such as "import os.path" inside a function is not local to it, so a nested function can't use it - a method reads a class nested in its class body (class C: class Foo: ...) as if it were an enclosing variable, instead of the module-level name - "global Zed; del Zed" on a name that was never defined doesn't raise NameError - after "global Foo; del Foo", a bare read of Foo through global gives back the undefined wrapper instead of raising NameError. Reading an attribute, calling it or using it in isinstance() still raises NameError, but "|" raises TypeError, and "is", "==", bool(), str() and setting an attribute don't raise at all Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> --- custom_components/pyscript/eval.py | 120 ++++- tests/test_unit_eval.py | 740 +++++++++++++++++++++++++++++ 2 files changed, 838 insertions(+), 22 deletions(-) diff --git a/custom_components/pyscript/eval.py b/custom_components/pyscript/eval.py index 75d4b69..64d4f56 100644 --- a/custom_components/pyscript/eval.py +++ b/custom_components/pyscript/eval.py @@ -13,7 +13,7 @@ import sys import time import traceback -from types import TracebackType +from types import ModuleType, TracebackType from typing import TYPE_CHECKING, Any import weakref @@ -279,6 +279,11 @@ def __repr__(self): return f"EvalLocalVar @{hex(id(self))} = {self.value if self.defined else 'undefined'}" +def unwrap_defined(value): + """Return the value held by a defined EvalLocalVar, otherwise value unchanged.""" + return value.get() if isinstance(value, EvalLocalVar) and value.is_defined() else value + + class EvalName: """Identifier that hasn't yet been resolved.""" @@ -301,11 +306,15 @@ def __init__(self, obj, attr): def setattr(self, value): """Set the attribute value.""" - setattr(self.obj, self.attr, value) + # update a pyscript module's EvalLocalVar in place, so its own functions see the change + if isinstance(self.obj, ModuleType) and isinstance(vars(self.obj).get(self.attr), EvalLocalVar): + vars(self.obj)[self.attr].set(unwrap_defined(value)) + else: + setattr(self.obj, self.attr, value) def getattr(self): """Get the attribute value.""" - return getattr(self.obj, self.attr) + return unwrap_defined(getattr(self.obj, self.attr)) class EvalFunc: @@ -926,6 +935,7 @@ def __init__(self, name: str, global_ctx: "GlobalContext", logger_name: str | No self.set_logger_name(logger_name if logger_name is not None else self.name) self.config_entry = Function.hass.data.get(DOMAIN, {}).get(CONFIG_ENTRY, {}) self.dec_eval_depth = 0 + self.class_sym_table: SymTable | None = None async def ast_not_implemented(self, arg, *args): """Raise NotImplementedError exception for unimplemented AST types.""" @@ -966,7 +976,7 @@ async def ast_import(self, arg): mod = await Function.hass.async_add_executor_job(importlib.import_module, imp.name) else: mod = sys.modules[imp.name] - self.sym_table[imp.name if imp.asname is None else imp.asname] = mod + self.assign_name(imp.name if imp.asname is None else imp.asname, mod) async def ast_importfrom(self, arg): """Execute from X import Y.""" @@ -976,7 +986,7 @@ async def ast_importfrom(self, arg): mod = await self.global_ctx.module_import(imp.name, arg.level) if not mod: raise ModuleNotFoundError(f"module '{imp.name}' not found") - self.sym_table[imp.name if imp.asname is None else imp.asname] = mod + self.assign_name(imp.name if imp.asname is None else imp.asname, mod) return if arg.module == "stubs" or arg.module.startswith("stubs."): for imp in arg.names: @@ -1000,10 +1010,17 @@ async def ast_importfrom(self, arg): for imp in arg.names: if imp.name == "*": for name, value in mod.__dict__.items(): - if name[0] != "_": - self.sym_table[name] = value + # skip a module-level EvalLocalVar whose name was deleted + if name[0] != "_" and not (isinstance(value, EvalLocalVar) and not value.is_defined()): + self.assign_name(name, value) else: - self.sym_table[imp.name if imp.asname is None else imp.asname] = getattr(mod, imp.name) + value = getattr(mod, imp.name) + if isinstance(value, EvalLocalVar) and not value.is_defined(): + raise ImportError( + f"cannot import name '{imp.name}' from '{mod.__name__}' (unknown location)" + ) + # assign_name takes the value out of the module's EvalLocalVar + self.assign_name(imp.name if imp.asname is None else imp.asname, value) async def ast_if(self, arg): """Execute if statement.""" @@ -1067,23 +1084,30 @@ async def ast_classdef(self, arg): keywords = {kw.arg: await self.aeval(kw.value) for kw in arg.keywords} metaclass = keywords.pop("metaclass", type(bases[0]) if bases else type) - if self.curr_func and arg.name in self.curr_func.global_names: + if self.binds_global(arg.name): sym_table_assign = self.global_sym_table else: sym_table_assign = self.sym_table - sym_table_assign[arg.name] = EvalLocalVar(arg.name) + # + # reuse an existing EvalLocalVar, so functions defined earlier see the new class; + # it keeps the old class until the new one is set below, as in Python + # + if not isinstance(sym_table_assign.get(arg.name), EvalLocalVar): + sym_table_assign[arg.name] = EvalLocalVar(arg.name) if hasattr(metaclass, "__prepare__"): sym_table = metaclass.__prepare__(arg.name, tuple(bases), **keywords) else: sym_table = {} self.sym_table_stack.append(self.sym_table) self.sym_table = sym_table + prev_class_sym_table, self.class_sym_table = self.class_sym_table, sym_table for arg1 in arg.body: val = await self.aeval(arg1) if isinstance(val, EvalReturn): raise SyntaxError(f"{val.name()} statement outside function") if isinstance(val, EvalStopFlow): raise SyntaxError(f"{val.name()} statement outside loop") + self.class_sym_table = prev_class_sym_table self.sym_table = self.sym_table_stack.pop() decorators = [await self.aeval(dec) for dec in arg.decorator_list] @@ -1191,7 +1215,7 @@ async def executor_wrap(*args, **kwargs): else: func_var = func - if self.curr_func and name in self.curr_func.global_names: + if self.binds_global(name): sym_table = self.global_sym_table else: sym_table = self.sym_table @@ -1412,13 +1436,37 @@ async def recurse_assign(self, lhs, val): raise NameError( f"invalid name '{var_name}' (should be 'domain.entity' or 'domain.entity.attr')" ) - if self.curr_func and var_name in self.curr_func.global_names: - self.global_sym_table[var_name] = val - return - if var_name in self.sym_table and isinstance(self.sym_table[var_name], EvalLocalVar): - self.sym_table[var_name].set(val) + self.assign_name(var_name, val) + + def binds_global(self, var_name): + """Return whether var_name is bound in the global symbol table by a global declaration.""" + # a class body binds in its own namespace, whatever an enclosing function declares + return bool(self.curr_func and var_name in self.curr_func.global_names) and ( + self.sym_table is not self.class_sym_table + ) + + def assign_name(self, var_name, val): + """Bind a plain variable name to val, updating an existing EvalLocalVar in place.""" + # + # store the value rather than another binding's EvalLocalVar (eg, from mod.Foo), + # so a later update of this name doesn't change that other binding + # + val = unwrap_defined(val) + if self.sym_table is self.class_sym_table: + # a class body binds plainly in the class namespace (see binds_global) + self.sym_table[var_name] = val + return + if self.binds_global(var_name): + # update a module-level EvalLocalVar in place, so functions defined earlier see it + if isinstance(self.global_sym_table.get(var_name), EvalLocalVar): + self.global_sym_table[var_name].set(val) else: - self.sym_table[var_name] = val + self.global_sym_table[var_name] = val + return + if var_name in self.sym_table and isinstance(self.sym_table[var_name], EvalLocalVar): + self.sym_table[var_name].set(val) + else: + self.sym_table[var_name] = val async def ast_assign(self, arg): """Execute assignment statement.""" @@ -1456,8 +1504,14 @@ async def ast_delete(self, arg): var = await self.aeval(arg1.value) del var[await self.aeval(arg1.slice)] elif isinstance(arg1, ast.Name): - if self.curr_func and arg1.id in self.curr_func.global_names: - if arg1.id in self.global_sym_table: + if self.binds_global(arg1.id): + # as with assignment, mark a module-level EvalLocalVar undefined in place + if isinstance(self.global_sym_table.get(arg1.id), EvalLocalVar): + if self.global_sym_table[arg1.id].is_defined(): + self.global_sym_table[arg1.id].set_undefined() + else: + raise NameError(f"name '{arg1.id}' is not defined") + elif arg1.id in self.global_sym_table: del self.global_sym_table[arg1.id] elif arg1.id in self.sym_table: if isinstance(self.sym_table[arg1.id], EvalLocalVar): @@ -1515,6 +1569,12 @@ async def ast_attribute(self, arg): val = await self.aeval(arg.value) if isinstance(arg.ctx, ast.Store): return EvalAttrSet(val, arg.attr) + if isinstance(val, ModuleType): + # a pyscript module keeps module-level classes in EvalLocalVars, so unwrap them + value = getattr(val, arg.attr) + if isinstance(value, EvalLocalVar) and not value.is_defined(): + raise AttributeError(f"module '{val.__name__}' has no attribute '{arg.attr}'") + return unwrap_defined(value) return getattr(val, arg.attr) async def ast_name(self, arg): @@ -1524,14 +1584,24 @@ async def ast_name(self, arg): # check other scopes if required by global declarations # if self.curr_func and arg.id in self.curr_func.global_names: + if self.sym_table is self.class_sym_table and arg.id in self.sym_table: + # a class body finds its own names first + val = self.sym_table[arg.id] + return val.get() if isinstance(val, EvalLocalVar) else val if arg.id in self.global_sym_table: - return self.global_sym_table[arg.id] + return unwrap_defined(self.global_sym_table[arg.id]) raise NameError(f"global name '{arg.id}' is not defined") # # now check in our current symbol table, and then some other places # if arg.id in self.sym_table: if isinstance(self.sym_table[arg.id], EvalLocalVar): + if ( + self.curr_func + and arg.id in self.curr_func.local_names + and not self.sym_table[arg.id].is_defined() + ): + raise UnboundLocalError(f"local variable '{arg.id}' referenced before assignment") return self.sym_table[arg.id].get() return self.sym_table[arg.id] if arg.id in self.local_sym_table: @@ -1544,8 +1614,7 @@ async def ast_name(self, arg): # it here too (eg, when it's used inside another class's body); while the # class body is still running it isn't defined yet, so leave it wrapped # - val = self.global_sym_table[arg.id] - return val.get() if isinstance(val, EvalLocalVar) and val.is_defined() else val + return unwrap_defined(self.global_sym_table[arg.id]) if arg.id in BUILTIN_AST_FUNCS_FACTORY: return BUILTIN_AST_FUNCS_FACTORY[arg.id](self) if hasattr(builtins, arg.id) and arg.id not in BUILTIN_EXCLUDE and arg.id[0] != "_": @@ -2025,6 +2094,13 @@ async def get_names_set(self, arg, names, nonlocal_names, global_names, local_na target_vars, _ = await self.loopvar_scope_save(arg.generators) for name in target_vars: local_names.add(name) + elif cls_name in {"Import", "ImportFrom"}: + # imported names are local too, so nested functions can share their binding + for alias in arg.names: + name = alias.name if alias.asname is None else alias.asname + if name != "*" and "." not in name: + local_names.add(name) + names.add(name) elif cls_name == "Try": for handler in arg.handlers: if handler.name is not None: diff --git a/tests/test_unit_eval.py b/tests/test_unit_eval.py index ab2f11b..b4f1f44 100644 --- a/tests/test_unit_eval.py +++ b/tests/test_unit_eval.py @@ -4,6 +4,7 @@ import asyncio import contextlib from dataclasses import dataclass +import sys from types import FunctionType, ModuleType import pytest @@ -2505,3 +2506,742 @@ async def test_with_exit_does_not_swallow_cancel(hass): await Function.waiter_sync() await Function.waiter_stop() await Function.reaper_stop() + + +GLOBAL_MOD_NAME = "mod_global" + +GLOBAL_MOD_SOURCE = """ +class Foo: + x = 1 + +def modread(): + return Foo + +def drop(): + global Foo + del Foo +""" + +GLOBAL_BASE_SOURCE = """ +class Foo: + x = 1 + +class Other: + x = 9 + +def read(): + return Foo + +def try_call(func): + try: + func() + return "no error" + except NameError: + return "NameError" +""" + +# +# each entry is a list of scripts and the value of "result" in the last one, which +# is what CPython gives; the scripts share one fresh copy of the GLOBAL_MOD_SOURCE module +# +globalNameTests = [ + [ + [ + GLOBAL_BASE_SOURCE + + """ +def f(): + global Foo + return [Foo | None, Foo is type(Foo())] +union, same = f() +result = [union == (Foo | None), same] +""" + ], + [True, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def f(): + global Foo + Foo.x = 3 + return [Foo.x, read().x] +result = [f(), Foo.x] +""" + ], + [[3, 3], 3], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def rebind(): + global Foo + Foo = Other +rebind() +result = [read() is Other, Foo is Other] +""" + ], + [True, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def drop(): + global Foo + del Foo +def read_global_attr(): + global Foo + return Foo.x +drop() +result = [try_call(read), try_call(read_global_attr), try_call(drop)] +""" + ], + ["NameError", "NameError", "NameError"], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def drop(): + global Foo + del Foo +drop() +class Foo: + x = 5 +result = [read().x, read() is Foo] +""" + ], + [5, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def drop(): + global Foo + del Foo +def make(): + global Foo + class Foo: + x = 7 +drop() +make() +result = [read().x, read() is Foo, Foo.x] +""" + ], + [7, True, 7], + ], + [ + [ + """ +def make(): + global Node + class Node: + nxt: Node = None +make() +result = [Node.nxt, list(Node.__annotations__)] +""" + ], + [None, ["nxt"]], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +class Foo: + x = Foo.x + 4 +result = [read().x, read() is Foo] +""" + ], + [5, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +del Foo +class Foo: + x = 5 +result = [read().x, read() is Foo] +""" + ], + [5, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def outer(): + def inner(): + global Foo + Foo = Other + inner() + return Foo +def outer_union(): + def inner(): + global Foo + return Foo | None + return inner() +result = [outer_union() == (Foo | None), outer() is Other, read() is Other] +""" + ], + [True, True, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +def outer(): + def inner(): + return Foo + return inner +inner = outer() +def rebind(): + global Foo + Foo = Other +rebind() +result = [inner() is Other, read() is Other] +""" + ], + [True, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo, modread +def rebind(): + global Foo + Foo = Other +rebind() +result = [Foo is Other, modread().x, modread() is Other] +""" + ], + [True, 1, False], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo, modread +same = [Foo is modread(), read() is modread()] +def drop(): + global Foo + del Foo +drop() +result = [same, try_call(read), modread().x] +""" + ], + [[True, True], "NameError", 1], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo, modread +Foo = Other +result = [Foo is Other, modread().x, modread() is Other] +""" + ], + [True, 1, False], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo, modread +del Foo +result = [try_call(read), modread().x] +""" + ], + ["NameError", 1], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import * +def rebind(): + global Foo + Foo = Other +rebind() +result = [Foo is Other, modread().x, modread() is Other] +""" + ], + [True, 1, False], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import * +del Foo +result = [try_call(read), modread().x] +""" + ], + ["NameError", 1], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo +def rebind(): + global Foo + Foo = Other +rebind() +""", + GLOBAL_BASE_SOURCE + + """ +from mod_global import * +def drop(): + global Foo + del Foo +drop() +""", + GLOBAL_BASE_SOURCE + + """ +from mod_global import Foo +Foo = Other +""", + """ +from mod_global import Foo, modread +result = [Foo.x, modread().x, Foo is modread()] +""", + ], + [1, 1, True], + ], + [ + [ + """ +import mod_global +Foo = mod_global.Foo +class Other: + x = 9 +def rebind(): + global Foo + Foo = Other +rebind() +result = [Foo.x, Foo is Other, mod_global.modread().x] +""" + ], + [9, True, 1], + ], + [ + [ + """ +import mod_global +Foo = mod_global.Foo +same = Foo is mod_global.modread() +def read_foo(): + return Foo.x +def drop_alias(): + global Foo + del Foo +drop_alias() +try: + read_foo() + gone = False +except NameError: + gone = True +result = [same, gone, mod_global.modread().x] +""" + ], + [True, True, 1], + ], + [ + [ + """ +import mod_global +Foo = mod_global.Foo +class Foo: + x = 9 +result = [Foo.x, mod_global.modread().x] +""" + ], + [9, 1], + ], + [ + [ + """ +import mod_global +Foo = mod_global.Foo +class Other: + x = 9 +def rebind(): + global Foo + Foo = Other +rebind() +""", + """ +import mod_global +Foo = mod_global.Foo +same = Foo is mod_global.modread() +def read_foo(): + return Foo.x +def drop_alias(): + global Foo + del Foo +drop_alias() +try: + read_foo() + gone = False +except NameError: + gone = True +""", + """ +import mod_global +Foo = mod_global.Foo +class Foo: + x = 9 +""", + """ +from mod_global import Foo, modread +result = [Foo.x, modread().x, Foo is modread()] +""", + ], + [1, 1, True], + ], + [ + [ + """ +class Original: + x = 1 +Foo = globals()["Original"] +class Other: + x = 9 +def rebind(): + global Foo + Foo = Other +rebind() +result = [Original.x, Foo.x, Foo is Other] +""" + ], + [1, 9, True], + ], + [ + [ + """ +def outer(): + from mod_global import Foo + def inner(): + return Foo.x + return inner() +def outer_as(): + from mod_global import Foo as Local + def inner(): + return Local.x + return inner +def outer_import(): + import mod_global as mod + def inner(): + return mod.modread().x + return inner() +result = [outer(), outer_as()(), outer_import()] +""" + ], + [1, 1, 1], + ], + [ + [ + """ +def outer(): + from mod_global import Foo, modread + def read(): + return Foo + def change(): + nonlocal Foo + Foo = 20 + change() + return [Foo, read(), modread().x] +result = outer() +""" + ], + [20, 20, 1], + ], + [ + [ + """ +from mod_global import modread +def load(): + global Foo, mod_global + from mod_global import Foo + import mod_global +load() +result = [Foo.x, Foo is modread(), mod_global.modread() is Foo] +""" + ], + [1, True, True], + ], + [ + [ + """ +import mod_global +mod_global.drop() +try: + from mod_global import Foo + msg = None +except ImportError as exc: + msg = str(exc) +from mod_global import * +def read_foo(): + return Foo.x +try: + read_foo() + gone = False +except NameError: + gone = True +result = [msg, gone] +""" + ], + ["cannot import name 'Foo' from 'mod_global' (unknown location)", True], + ], + [ + [ + """ +class Foo: + x = 1 +def make(): + global Foo + class C: + from math import pi as Foo + class D: + import math as Foo + class E: + Foo = 3 + return [C, D, E] +C, D, E = make() +result = [isinstance(C.Foo, float), D.Foo.__name__, E.Foo, Foo.x] +""" + ], + [True, "math", 3, 1], + ], + [ + [ + """ +class Foo: + x = 1 +def make(): + global Foo + class C: + Foo = 3 + y = Foo + del Foo + class D: + def Foo(self): + return 2 + class E: + class Foo: + x = 4 + return [C, D, E] +C, D, E = make() +result = [C.y, hasattr(C, "Foo"), D().Foo(), E.Foo.x, Foo.x] +""" + ], + [3, False, 2, 4, 1], + ], + [ + [ + """ +class Foo: + x = 1 +class C: + class Foo: + x = 2 + from math import pi as Foo + def read(self): + return Foo.x +class D: + class Foo: + x = 2 + Foo = 5 + def read(self): + return Foo.x +result = [C().read(), isinstance(C.Foo, float), D().read(), D.Foo] +""" + ], + [1, True, 1, 5], + ], + [ + [ + """ +import mod_global +globals()["Foo"] = mod_global.Foo +class Other: + x = 9 +def rebind(): + global Foo + Foo = Other +rebind() +result = [Foo.x, mod_global.modread().x] +""" + ], + [9, 1], + ], + [ + [ + """ +import mod_global +globals().update({"Foo": mod_global.Foo}) +class Foo: + x = 9 +result = [Foo.x, mod_global.modread().x] +""" + ], + [9, 1], + ], + [ + [ + """ +import mod_global +Foo = mod_global.modread() +result = [ + (mod_global.Foo | None) == (Foo | None), + mod_global.Foo is Foo, + isinstance(Foo(), mod_global.Foo), + type(mod_global.Foo()) is mod_global.Foo, +] +""" + ], + [True, True, True, True], + ], + [ + [ + """ +import mod_global +from mod_global import Foo as Snap +class Other: + x = 9 +mod_global.Foo = Other +result = [mod_global.Foo is Other, mod_global.modread() is Other, Snap.x] +""" + ], + [True, True, 1], + ], + [ + [ + """ +import math +def f(): + def inner(): + return math.sqrt(4) + try: + before = math.sqrt(4) + except UnboundLocalError: + before = "unbound" + import math + return [before, inner()] +def g(): + def inner(): + return x + x = 1 + del x + try: + x + return "no error" + except UnboundLocalError: + return "unbound" +result = [f(), g()] +""" + ], + [["unbound", 2.0], "unbound"], + ], +] + + +def run_global_name_test_python(sources): + """Run the scripts in CPython and return the last one's result.""" + mod = ModuleType(GLOBAL_MOD_NAME) + FunctionType(compile(GLOBAL_MOD_SOURCE, GLOBAL_MOD_NAME, "exec"), mod.__dict__)() + sys.modules[GLOBAL_MOD_NAME] = mod + try: + for source in sources: + namespace = {} + FunctionType(compile(source, "test", "exec"), namespace)() + return namespace["result"] + finally: + del sys.modules[GLOBAL_MOD_NAME] + + +async def run_global_name_test(sources): + """Run the scripts in pyscript and return the last one's result.""" + mod = ModuleType(GLOBAL_MOD_NAME) + module_ctx = GlobalContext(GLOBAL_MOD_NAME, global_sym_table=mod.__dict__, manager=GlobalContextMgr) + module_ctx.module = mod + module_ctx.file_path = f"modules/{GLOBAL_MOD_NAME}.py" + module_ast = AstEval(GLOBAL_MOD_NAME, global_ctx=module_ctx) + Function.install_ast_funcs(module_ast) + module_ast.parse(GLOBAL_MOD_SOURCE) + await module_ast.eval() + GlobalContextMgr.set(f"modules.{GLOBAL_MOD_NAME}", module_ctx) + try: + for source in sources: + global_ctx = GlobalContext("test", global_sym_table={}, manager=GlobalContextMgr) + ast = AstEval("test", global_ctx=global_ctx) + Function.install_ast_funcs(ast) + ast.parse(source) + await ast.eval() + return global_ctx.global_sym_table["result"] + finally: + GlobalContextMgr.delete(f"modules.{GLOBAL_MOD_NAME}") + + +async def test_global_names_match_python(hass): + """Test module-level names used through global and from-imports, 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) + + for sources, expect in globalNameTests: + assert run_global_name_test_python(sources) == expect, sources[-1] + assert await run_global_name_test(sources) == expect, sources[-1] + await Function.waiter_sync() + await Function.waiter_stop() + await Function.reaper_stop() + + +async def test_class_body_exception_keeps_old_class(hass): + """Test that a class redefinition whose body raises keeps the old class, as in 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) + + source = ( + GLOBAL_BASE_SOURCE + + """ +class Foo: + x = 5 + raise ValueError("bad class body") +""" + ) + check = "result = [Foo.x, read().x, read() is Foo]" + expect = [1, 1, True] + + namespace = {} + with pytest.raises(ValueError, match="bad class body"): + FunctionType(compile(source, "test", "exec"), namespace)() + FunctionType(compile(check, "test", "exec"), namespace)() + assert namespace["result"] == expect + + global_ctx = GlobalContext("test", global_sym_table={}, manager=GlobalContextMgr) + ast = AstEval("test", global_ctx=global_ctx) + Function.install_ast_funcs(ast) + ast.parse(source) + with pytest.raises(ValueError, match="bad class body"): + await ast.eval() + ast = AstEval("test", global_ctx=global_ctx) + Function.install_ast_funcs(ast) + ast.parse(check) + await ast.eval() + assert global_ctx.global_sym_table["result"] == expect + + await Function.waiter_sync() + await Function.waiter_stop() + await Function.reaper_stop() From bd5c68e38c9fda5ba4809ba0bf7f63d84303b9f3 Mon Sep 17 00:00:00 2001 From: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> Date: Fri, 20 Nov 2026 04:57:13 +0000 Subject: [PATCH 4/5] docs: record global-path-875 Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> --- docs/plans/global-path-875.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/plans/global-path-875.md b/docs/plans/global-path-875.md index bf807c8..44167c3 100644 --- a/docs/plans/global-path-875.md +++ b/docs/plans/global-path-875.md @@ -45,3 +45,33 @@ Engineer (code and tests), QA (tests and plan check), Security Officer (leaks an - A lambda or `@pyscript_compile` function reading a module-level class that is later rebound. - `global Zed; del Zed` on a name that was never defined doesn't raise `NameError`. - No change to the existing #902 commit. + +--- + +## Record (2026-11-20) + +**Status:** built and checked; two open problems from the final review (below). Not yet pushed to #902. + +**What shipped on this branch:** one commit, `update module-level names in place when assigned or deleted through global (#875)`. It changes `eval.py` (+98/−22) and adds tests (+740). It applies cleanly on its own onto #902's commit, and all 204 tests, ruff, pylint, mypy and codespell pass on Python 3.14. + +**Decisions:** +- Add the `global` fix to #902 instead of a separate PR (Sean, on this PR). +- A read through `global` leaves an undefined wrapper wrapped, as in the maintainer's sketch, so a class defined through `global` can still name itself in its type hints. Rejected: raising NameError there, which broke code that works today. +- Unwrap on every binding (assignment, imports) and on module attribute reads, so no binding shares another module's wrapper. Rejected: an "owner" field on the wrapper (bigger change, and it left existing leaks). +- Class bodies bind in the class namespace, even inside a function that declares the name `global`. +- Imports inside a function are local to it, as in Python. + +**Review rounds:** +- Round 1: the audit found two must-fix regressions: aliases through `mod.Foo` still shared the wrapper, and imports inside a function broke closures. Both fixed. +- Round 2: the audit found class-body import and alias problems and a wrong exception type. All fixed. + +**Open (final review):** +1. `global x` written inside a class body is ignored. A regression for assignment; for imports, upstream has the same bug already. +2. A class nested in a class whose metaclass `__prepare__` returns a mapping without `.get()` fails. A one-line fix. + +**Rejected or deferred findings:** +- `global Zed; del Zed` on a never-defined name doesn't raise. Unchanged from upstream, listed as a known limit. +- `hasattr(mod, "Foo")` after mod deletes `Foo` returns True. Same family as the listed `getattr(mod, ...)` limit. +- Star import inside a function is accepted. Unchanged from upstream. +- A class body that raises doesn't restore pyscript's scope stack. Unchanged from upstream; worth its own small PR. +- Plan check and plain-English check were skipped (the reviewer's monthly quota was used up). From e275418ff831319560560fbe4a14322eddcedfb2 Mon Sep 17 00:00:00 2001 From: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:37:00 +0000 Subject: [PATCH 5/5] apply final review fixes to the global path change (#875) Adds the class-body global and prepared namespace fixes, their tests and the updated job record on top of the previous version. The commit to send upstream is 4a99336. Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com> --- custom_components/pyscript/eval.py | 34 +++++++----- docs/plans/global-path-875.md | 11 ++-- tests/test_unit_eval.py | 85 ++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 19 deletions(-) diff --git a/custom_components/pyscript/eval.py b/custom_components/pyscript/eval.py index 64d4f56..fa40541 100644 --- a/custom_components/pyscript/eval.py +++ b/custom_components/pyscript/eval.py @@ -936,6 +936,7 @@ def __init__(self, name: str, global_ctx: "GlobalContext", logger_name: str | No self.config_entry = Function.hass.data.get(DOMAIN, {}).get(CONFIG_ENTRY, {}) self.dec_eval_depth = 0 self.class_sym_table: SymTable | None = None + self.class_global_names: set[str] = set() async def ast_not_implemented(self, arg, *args): """Raise NotImplementedError exception for unimplemented AST types.""" @@ -1092,7 +1093,7 @@ async def ast_classdef(self, arg): # reuse an existing EvalLocalVar, so functions defined earlier see the new class; # it keeps the old class until the new one is set below, as in Python # - if not isinstance(sym_table_assign.get(arg.name), EvalLocalVar): + if not (arg.name in sym_table_assign and isinstance(sym_table_assign[arg.name], EvalLocalVar)): sym_table_assign[arg.name] = EvalLocalVar(arg.name) if hasattr(metaclass, "__prepare__"): sym_table = metaclass.__prepare__(arg.name, tuple(bases), **keywords) @@ -1101,6 +1102,7 @@ async def ast_classdef(self, arg): self.sym_table_stack.append(self.sym_table) self.sym_table = sym_table prev_class_sym_table, self.class_sym_table = self.class_sym_table, sym_table + prev_class_global_names, self.class_global_names = self.class_global_names, set() for arg1 in arg.body: val = await self.aeval(arg1) if isinstance(val, EvalReturn): @@ -1108,6 +1110,7 @@ async def ast_classdef(self, arg): if isinstance(val, EvalStopFlow): raise SyntaxError(f"{val.name()} statement outside loop") self.class_sym_table = prev_class_sym_table + self.class_global_names = prev_class_global_names self.sym_table = self.sym_table_stack.pop() decorators = [await self.aeval(dec) for dec in arg.decorator_list] @@ -1370,6 +1373,10 @@ async def ast_return(self, arg): async def ast_global(self, arg): """Execute global statement.""" + if self.sym_table is self.class_sym_table: + # a class body's global declarations apply to the class body only + self.class_global_names.update(arg.names) + return if not self.curr_func: raise SyntaxError("global statement outside function") for var_name in arg.names: @@ -1440,10 +1447,10 @@ async def recurse_assign(self, lhs, val): def binds_global(self, var_name): """Return whether var_name is bound in the global symbol table by a global declaration.""" - # a class body binds in its own namespace, whatever an enclosing function declares - return bool(self.curr_func and var_name in self.curr_func.global_names) and ( - self.sym_table is not self.class_sym_table - ) + if self.sym_table is self.class_sym_table: + # a class body binds in its own namespace, whatever an enclosing function declares + return var_name in self.class_global_names + return bool(self.curr_func and var_name in self.curr_func.global_names) def assign_name(self, var_name, val): """Bind a plain variable name to val, updating an existing EvalLocalVar in place.""" @@ -1452,10 +1459,6 @@ def assign_name(self, var_name, val): # so a later update of this name doesn't change that other binding # val = unwrap_defined(val) - if self.sym_table is self.class_sym_table: - # a class body binds plainly in the class namespace (see binds_global) - self.sym_table[var_name] = val - return if self.binds_global(var_name): # update a module-level EvalLocalVar in place, so functions defined earlier see it if isinstance(self.global_sym_table.get(var_name), EvalLocalVar): @@ -1463,6 +1466,10 @@ def assign_name(self, var_name, val): else: self.global_sym_table[var_name] = val return + if self.sym_table is self.class_sym_table: + # a class body binds plainly in the class namespace (see binds_global) + self.sym_table[var_name] = val + return if var_name in self.sym_table and isinstance(self.sym_table[var_name], EvalLocalVar): self.sym_table[var_name].set(val) else: @@ -1583,9 +1590,12 @@ async def ast_name(self, arg): # # check other scopes if required by global declarations # - if self.curr_func and arg.id in self.curr_func.global_names: - if self.sym_table is self.class_sym_table and arg.id in self.sym_table: - # a class body finds its own names first + in_class = self.sym_table is self.class_sym_table + if (in_class and arg.id in self.class_global_names) or ( + self.curr_func and arg.id in self.curr_func.global_names + ): + if in_class and arg.id not in self.class_global_names and arg.id in self.sym_table: + # a class body finds its own names first, unless it declares them global val = self.sym_table[arg.id] return val.get() if isinstance(val, EvalLocalVar) else val if arg.id in self.global_sym_table: diff --git a/docs/plans/global-path-875.md b/docs/plans/global-path-875.md index 44167c3..46d171f 100644 --- a/docs/plans/global-path-875.md +++ b/docs/plans/global-path-875.md @@ -50,9 +50,9 @@ Engineer (code and tests), QA (tests and plan check), Security Officer (leaks an ## Record (2026-11-20) -**Status:** built and checked; two open problems from the final review (below). Not yet pushed to #902. +**Status:** built and checked, with no open problems. Not yet pushed to #902. -**What shipped on this branch:** one commit, `update module-level names in place when assigned or deleted through global (#875)`. It changes `eval.py` (+98/−22) and adds tests (+740). It applies cleanly on its own onto #902's commit, and all 204 tests, ruff, pylint, mypy and codespell pass on Python 3.14. +**What shipped on this branch:** one commit, `update module-level names in place when assigned or deleted through global (#875)`. It changes `eval.py` (+109/−23) and adds tests (+825). It applies cleanly on its own onto #902's commit, and all 204 tests, ruff, pylint, mypy and codespell pass on Python 3.14. **Decisions:** - Add the `global` fix to #902 instead of a separate PR (Sean, on this PR). @@ -64,14 +64,11 @@ Engineer (code and tests), QA (tests and plan check), Security Officer (leaks an **Review rounds:** - Round 1: the audit found two must-fix regressions: aliases through `mod.Foo` still shared the wrapper, and imports inside a function broke closures. Both fixed. - Round 2: the audit found class-body import and alias problems and a wrong exception type. All fixed. - -**Open (final review):** -1. `global x` written inside a class body is ignored. A regression for assignment; for imports, upstream has the same bug already. -2. A class nested in a class whose metaclass `__prepare__` returns a mapping without `.get()` fails. A one-line fix. +- Round 3 (extra round, Sean commented "fix"): the audit found that `global x` written inside a class body was ignored, and that a nested class failed with a custom `__prepare__` mapping. Both are fixed. The final audit found no new problems; it asked for one more known limit in the commit message (a `@pyscript_compile` function defined in a class body under `global`, which upstream already has), and that was added. **Rejected or deferred findings:** - `global Zed; del Zed` on a never-defined name doesn't raise. Unchanged from upstream, listed as a known limit. -- `hasattr(mod, "Foo")` after mod deletes `Foo` returns True. Same family as the listed `getattr(mod, ...)` limit. +- `hasattr(mod, "Foo")` after mod deletes `Foo` returns True. Added to the `getattr(mod, ...)` known limit. - Star import inside a function is accepted. Unchanged from upstream. - A class body that raises doesn't restore pyscript's scope stack. Unchanged from upstream; worth its own small PR. - Plan check and plain-English check were skipped (the reviewer's monthly quota was used up). diff --git a/tests/test_unit_eval.py b/tests/test_unit_eval.py index b4f1f44..267504d 100644 --- a/tests/test_unit_eval.py +++ b/tests/test_unit_eval.py @@ -3148,6 +3148,91 @@ def inner(): ], [["unbound", 2.0], "unbound"], ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +x = 1 +def make(): + class C: + global x, Foo + x = 2 + from mod_global import Foo + return C +C = make() +import mod_global +result = [x, hasattr(C, "x"), hasattr(C, "Foo"), read() is mod_global.Foo] +""" + ], + [2, False, False, True], + ], + [ + [ + GLOBAL_BASE_SOURCE + + """ +x = 1 +class C: + global x, Foo + x = 2 + from math import pi as Foo + y = x + 1 +result = [x, hasattr(C, "x"), C.y, hasattr(C, "Foo"), isinstance(read(), float)] +""" + ], + [2, False, 3, False, True], + ], + [ + [ + """ +x = 1 +def f(): + class C: + global x + x = 2 + x = 5 + return x +def g(): + global x + class C: + x = 3 + return C.x +result = [f(), x, g(), x] +""" + ], + [5, 2, 3, 2], + ], + [ + [ + """ +def pyscript_compile(func): + return func +@pyscript_compile +def make_meta(): + class Namespace: + def __init__(self): + self.values = {} + def __getitem__(self, name): + return self.values[name] + def __setitem__(self, name, value): + self.values[name] = value + def __contains__(self, name): + return name in self.values + class Meta(type): + @classmethod + def __prepare__(mcs, name, bases): + return Namespace() + def __new__(mcs, name, bases, namespace): + return type.__new__(mcs, name, bases, dict(namespace.values)) + return Meta +class C(metaclass=make_meta()): + class D: + x = 3 + y = D.x +result = [C.D.x, C.y] +""" + ], + [3, 3], + ], ]