From 00cee934b4ae68c6a11c551e49c64e615799e312 Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Wed, 11 Nov 2026 10:28:11 -0500 Subject: [PATCH 1/2] NZ walk: pin four rules the review of 04c8379fd found unpinned (#1578) The delta review of #1567's head, 04c8379fd, approved it with four optional findings: three rules of the static walk that no root pinned, and one form that "What it can't see" didn't list. This commit takes all four. Each new root sits beside the getter roots of #1567: a module with a __getattr__ that answers for name, so Python reads the root's target whenever the module's own bindings miss name. - names_all's ast.Name clause (finding 1). r_getattr_maybe_star_all_walrus_default and _walrus_class star-import a source that binds __getattr__ through a walrus in a top-level def's default, or __all__ through a walrus in a class's bases. Either way the star import brings in nothing. _Module files the def or class under its functions or classes, so only the clause sees the walrus, and a knockout of the clause fails both roots. - names_all's declared test (finding 2). r_getattr_maybe_star_all_global_getter star-imports a source whose function declares global __getattr__ and imports a getter under that name. A knockout that reads only "__all__" in declared fails it. - sure_stars' top-level restriction (finding 3). r_getattr_maybe_star_skipped holds its star import under `if False:`, so the import brings in nothing. A knockout that counts every star import as binding for sure fails it. - "What it can't see" (finding 4) now lists an __all__ that sys.modules[__name__] sets, and the BLIND root r_getattr_self_set_all pins it: Python reads the getter's target, and the walk counts name as bound for sure. The cases hold 212 roots, 249 targets, 14 in OVER, 27 in BLIND and eight decoys. No earlier root, source, OVER or BLIND entry changes, and the walk's code changes only in that docstring entry. On CPython 3.14.7 and 3.13.15, outside pytest, all 212 roots pass the targets and runtime-read checks, and the walk passes the once, fresh-walk and budget checks on every root and on 300 random trees. Each rule's knockout fails exactly its new roots: dropping the clause fails the two walrus roots, reading only "__all__" in declared fails r_getattr_maybe_star_all_global_getter, and counting every star import as sure fails r_getattr_maybe_star_skipped. With the 65e3e7931 review's exact text, its two edits that log a pass as adding each fail 18 roots by the once check. Co-Authored-By: Claude Opus 5.5 --- test_support/microcosm_build/static_reach.py | 13 ++- .../microcosm_build/static_reach_cases.py | 95 +++++++++++++++++++ 2 files changed, 101 insertions(+), 7 deletions(-) diff --git a/test_support/microcosm_build/static_reach.py b/test_support/microcosm_build/static_reach.py index 6c6d0acbf2..0c08cba27c 100644 --- a/test_support/microcosm_build/static_reach.py +++ b/test_support/microcosm_build/static_reach.py @@ -148,13 +148,12 @@ ``del sys.modules[__name__].name``) or that the module removes without ``del`` (``delattr``, ``globals()``); for a name that a star import leaves out through an ``__all__`` the walk can't see (one that another - module sets, that ``globals()`` sets or that a star import brings in, or - one that a ``__getattr__`` the module gets in one of those ways answers - for); for - ``__all__``, which a star import asks a module that doesn't define it - for; for each name that a star import's ``__all__`` lists and the module - doesn't bind; and for ``__path__``, which a ``from`` import asks a module - that isn't a package for; + module sets, that ``globals()`` or ``sys.modules[__name__]`` sets or that + a star import brings in, or one that a ``__getattr__`` the module gets in + one of those ways answers for); for ``__all__``, which a star import asks + a module that doesn't define it for; for each name that a star import's + ``__all__`` lists and the module doesn't bind; and for ``__path__``, which + a ``from`` import asks a module that isn't a package for; - the import-time code of a module that holds no item the walk reaches, such as the ``__init__`` of a package above a reached module, or a module imported only for its effects, unless a statement there changes a reached diff --git a/test_support/microcosm_build/static_reach_cases.py b/test_support/microcosm_build/static_reach_cases.py index 99db0022aa..8be80ae2e2 100644 --- a/test_support/microcosm_build/static_reach_cases.py +++ b/test_support/microcosm_build/static_reach_cases.py @@ -156,9 +156,13 @@ "getattr_maybe_star_all_getter_import", "getattr_maybe_star_all_getter_match", "getattr_maybe_star_all_global", + "getattr_maybe_star_all_global_getter", "getattr_maybe_star_all_import", "getattr_maybe_star_all_match", + "getattr_maybe_star_all_walrus_class", + "getattr_maybe_star_all_walrus_default", "getattr_maybe_star_del", + "getattr_maybe_star_skipped", "getattr_maybe_star_private", "getattr_maybe_target", "getattr_maybe_typing", @@ -172,6 +176,7 @@ "getattr_renamed", "getattr_self", "getattr_self_del", + "getattr_self_set_all", "getattr_settled", "getattr_star_lazy", "getattr_star_noall", @@ -3983,6 +3988,37 @@ def answer(attr): name = 1 """ +# A walrus in a top-level def's default, or in a class's bases, binds +# __getattr__ or __all__ at module level. +SOURCES["gmb_star_all_walrus_default_src"] = """ + from microcosm.walkcases.gmb_answers import answer + + + def _hide(value=(__getattr__ := answer)): + return value + + + name = 1 + """ +SOURCES["gmb_star_all_walrus_class_src"] = """ + class _Hide((__all__ := []) or object): + pass + + + name = 1 + """ +# A function binds the module's __getattr__ through global. +SOURCES["gmb_star_all_global_getter_src"] = """ + name = 1 + + + def _hide(): + global __getattr__ + from microcosm.walkcases.gmb_answers import answer as __getattr__ + + + _hide() + """ SOURCES["gmb_star_all_class_src"] = """ class _Empty(type): def __getitem__(cls, index): @@ -4015,9 +4051,28 @@ def __getattr__(attr): "getter_import", "getter_match", "class", + "walrus_default", + "walrus_class", + "global_getter", ) } ) +# A star import that never runs brings in nothing, so the getter answers. +SOURCES["gmb_star_skipped_src"] = """ + name = 1 + """ +SOURCES["gmb_star_skipped"] = """ + from microcosm.walkcases import t_getattr_maybe_star_skipped + + if False: + from microcosm.walkcases.gmb_star_skipped_src import * + + + def __getattr__(attr): + if attr == "name": + return t_getattr_maybe_star_skipped.X + raise AttributeError(attr) + """ SOURCES["gmb_with_later"] = """ from contextlib import suppress @@ -4077,6 +4132,10 @@ def run(): "star_all_getter_import", "star_all_getter_match", "star_all_class", + "star_all_walrus_default", + "star_all_walrus_class", + "star_all_global_getter", + "star_skipped", "with_later", "with_tuple", ) @@ -4262,6 +4321,30 @@ def __getattr__(attr): from microcosm.walkcases.gsd_m import name + def run(): + return name + """ +# A module sets its own __all__ as an attribute of itself. +SOURCES["gss_src"] = """ + import sys + + sys.modules[__name__].__all__ = [] + name = 1 + """ +SOURCES["gss_m"] = """ + from microcosm.walkcases import t_getattr_self_set_all + from microcosm.walkcases.gss_src import * + + + def __getattr__(attr): + if attr == "name": + return t_getattr_self_set_all.X + raise AttributeError(attr) + """ +SOURCES["r_getattr_self_set_all"] = """ + from microcosm.walkcases.gss_m import name + + def run(): return name """ @@ -4509,6 +4592,16 @@ def run(): "getattr_maybe_star_all_getter_match" }, ("r_getattr_maybe_star_all_class", "run"): {"getattr_maybe_star_all_class"}, + ("r_getattr_maybe_star_all_walrus_default", "run"): { + "getattr_maybe_star_all_walrus_default" + }, + ("r_getattr_maybe_star_all_walrus_class", "run"): { + "getattr_maybe_star_all_walrus_class" + }, + ("r_getattr_maybe_star_all_global_getter", "run"): { + "getattr_maybe_star_all_global_getter" + }, + ("r_getattr_maybe_star_skipped", "run"): {"getattr_maybe_star_skipped"}, ("r_getattr_maybe_with_later", "run"): {"getattr_maybe_with_later"}, ("r_getattr_maybe_with_tuple", "run"): {"getattr_maybe_with_tuple"}, ("r_getattr_sure", "run"): set(), @@ -4719,6 +4812,7 @@ def run(): ("r_getattr_star_lazy", "run"): set(), ("r_getattr_star_partial", "run"): set(), ("r_getattr_self_del", "run"): set(), + ("r_getattr_self_set_all", "run"): set(), ("r_getattr_effect", "run"): set(), ("r_getattr_effect_from", "run"): set(), } @@ -4802,6 +4896,7 @@ def run(): ("r_getattr_star_lazy", "run"): {"getattr_star_lazy"}, ("r_getattr_star_partial", "run"): {"getattr_star_partial"}, ("r_getattr_self_del", "run"): {"getattr_self_del"}, + ("r_getattr_self_set_all", "run"): {"getattr_self_set_all"}, } From 352c1b5331fabe6a612be0ad3c2b228dbb03eccb Mon Sep 17 00:00:00 2001 From: Max Ghenis Date: Wed, 11 Nov 2026 10:47:59 -0500 Subject: [PATCH 2/2] NZ walk: word the hidden-__all__ entry for a module's own object (#1578) Take the optional findings of the 00cee934b review and Belgium's review of round D. No code changes: the walk's AST without docstrings and the cases' AST (with TARGETS sorted) equal 00cee934b's. - static_reach.py, "What it can't see": the hidden-__all__ entry now names one that a module, itself included, sets as an attribute (through sys.modules[__name__], an import of itself or setattr), and one that globals(), locals(), vars() or exec sets. The review found the self-import, sys.modules.get, import_module(__name__), vars() and locals() spellings each blind in the same way as the BLIND root. Belgium's wording (suggested-doc-D.diff). - static_reach_cases.py: TARGETS is sorted again (getattr_maybe_star_private before getattr_maybe_star_skipped), and BLIND's comment block names an __all__ that a module, itself included, sets as an attribute, which covers r_getattr_self_set_all. Belgium's run with both files: all 212 roots pass the walk and Python's reads on 3.14 and 3.13. Co-Authored-By: Claude Opus 5.5 --- test_support/microcosm_build/static_reach.py | 16 +++++++++------- .../microcosm_build/static_reach_cases.py | 12 ++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/test_support/microcosm_build/static_reach.py b/test_support/microcosm_build/static_reach.py index 0c08cba27c..e7920d8196 100644 --- a/test_support/microcosm_build/static_reach.py +++ b/test_support/microcosm_build/static_reach.py @@ -147,13 +147,15 @@ deletes as an attribute (``del module.name``, ``del sys.modules[__name__].name``) or that the module removes without ``del`` (``delattr``, ``globals()``); for a name that a star import - leaves out through an ``__all__`` the walk can't see (one that another - module sets, that ``globals()`` or ``sys.modules[__name__]`` sets or that - a star import brings in, or one that a ``__getattr__`` the module gets in - one of those ways answers for); for ``__all__``, which a star import asks - a module that doesn't define it for; for each name that a star import's - ``__all__`` lists and the module doesn't bind; and for ``__path__``, which - a ``from`` import asks a module that isn't a package for; + leaves out through an ``__all__`` the walk can't see (one that a module, + itself included, sets as an attribute, through ``sys.modules[__name__]``, + an import of itself or ``setattr``; one that ``globals()``, ``locals()``, + ``vars()`` or ``exec`` sets; one that a star import brings in; or one that + a ``__getattr__`` the module gets in one of those ways answers for); for + ``__all__``, which a star import asks a module that doesn't define it for; + for each name that a star import's ``__all__`` lists and the module + doesn't bind; and for ``__path__``, which a ``from`` import asks a module + that isn't a package for; - the import-time code of a module that holds no item the walk reaches, such as the ``__init__`` of a package above a reached module, or a module imported only for its effects, unless a statement there changes a reached diff --git a/test_support/microcosm_build/static_reach_cases.py b/test_support/microcosm_build/static_reach_cases.py index 8be80ae2e2..edac975e6a 100644 --- a/test_support/microcosm_build/static_reach_cases.py +++ b/test_support/microcosm_build/static_reach_cases.py @@ -162,8 +162,8 @@ "getattr_maybe_star_all_walrus_class", "getattr_maybe_star_all_walrus_default", "getattr_maybe_star_del", - "getattr_maybe_star_skipped", "getattr_maybe_star_private", + "getattr_maybe_star_skipped", "getattr_maybe_target", "getattr_maybe_typing", "getattr_maybe_walrus", @@ -4880,11 +4880,11 @@ def run(): # module part-built (through an import cycle, or a function the module # calls while it runs) or reads a star import's source part-built; for a # name a module, itself included, deletes as an attribute; for a name a - # star import leaves out through an __all__ that another module sets; for - # __all__, which a star import asks a module that doesn't define it for; - # for each name that a star import's __all__ lists and the module doesn't - # bind; and for __path__, which a from-import asks a module that isn't a - # package for. + # star import leaves out through an __all__ that a module, itself + # included, sets as an attribute; for __all__, which a star import asks a + # module that doesn't define it for; for each name that a star import's + # __all__ lists and the module doesn't bind; and for __path__, which a + # from-import asks a module that isn't a package for. ("r_getattr_import_cycle", "run"): {"getattr_import_cycle"}, ("r_getattr_import_cycle_from", "run"): {"getattr_import_cycle_from"}, ("r_getattr_init_cycle", "run"): {"getattr_init_cycle"},