Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions test_support/microcosm_build/static_reach.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@
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()`` 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
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
Expand Down
105 changes: 100 additions & 5 deletions test_support/microcosm_build/static_reach_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,14 @@
"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_private",
"getattr_maybe_star_skipped",
"getattr_maybe_target",
"getattr_maybe_typing",
"getattr_maybe_walrus",
Expand All @@ -172,6 +176,7 @@
"getattr_renamed",
"getattr_self",
"getattr_self_del",
"getattr_self_set_all",
"getattr_settled",
"getattr_star_lazy",
"getattr_star_noall",
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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",
)
Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
}
Expand Down Expand Up @@ -4786,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"},
Expand All @@ -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"},
}


Expand Down
Loading