Skip to content

Commit ddd7c54

Browse files
committed
Do things in SETATTR_TARGET
Add `SETATTR_TARGET` into main API. Add docstring for `SETATTR_TARGET`. Add documentation for `SETATTR_TARGET`.
1 parent a5d52bf commit ddd7c54

3 files changed

Lines changed: 27 additions & 22 deletions

File tree

‎docs/documentation.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ You should rarely need to instantiate `LazyModule` directly — use
206206
| `__status__` | `str` | Lifecycle status marker for this package. See comments in `lazyimports/__init__.py` |
207207
| `NATIVE_LAZY_IMPORT` | `bool` | `True` if the interpreter provides the native `lazy import` syntax (Python 3.15+). |
208208
| `SUPPORT_LAZY_IMPORT` | `bool` | Always `True` — this package provides its own implementation. |
209+
| `SETATTR_TARGET` | `Literal["module", "proxy"]` | Controls where ``LazyModule.__setattr__`` writes non-internal attributes when the real module has not yet been loaded (or when loading has failed). |
209210

210211
---
211212

‎lazyimports/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from lazyimports.core import (
3434
NATIVE_LAZY_IMPORT,
3535
SUPPORT_LAZY_IMPORT,
36+
SETATTR_TARGET,
3637
LazyModule,
3738
lazy_import,
3839
lazy_from,
@@ -75,6 +76,7 @@
7576
"__status__",
7677
"NATIVE_LAZY_IMPORT",
7778
"SUPPORT_LAZY_IMPORT",
79+
"SETATTR_TARGET",
7880
"LazyModule",
7981
"lazy_import",
8082
"lazy_from",

‎lazyimports/core.py‎

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,31 @@
4949
# Module-level configuration
5050
# ---------------------------------------------------------------------------
5151

52-
# Controls where ``LazyModule.__setattr__`` writes non-internal
53-
# attributes when the real module has not yet been loaded (or when
54-
# loading has failed).
55-
#
56-
# Accepted values:
57-
# * ``"module"`` (default, backward-compatible): the attribute is
58-
# forwarded to the underlying module via ``setattr(real, ...)``.
59-
# This means a value written through the proxy becomes visible
60-
# globally (because Python modules are singletons in
61-
# ``sys.modules``). This matches the behaviour of ordinary
62-
# ``import foo`` followed by ``foo.bar = ...``.
63-
# * ``"proxy"``: the attribute is mounted on the proxy shell via
64-
# ``object.__setattr__(self, ...)``. The underlying module is
65-
# **not** loaded as a side effect, and the value is visible only
66-
# through this proxy instance. Use this when you want to attach
67-
# scratch state to a lazy module without paying for the import
68-
# or polluting the real module's namespace.
69-
#
70-
# Why a global instead of a per-instance flag? The two modes are
71-
# not usually mixed in the same project; a single global keeps the
72-
# API surface small. Change at your own risk and reset it before any
73-
# code that expects the default behaviour runs.
52+
7453
SETATTR_TARGET = "module"
54+
"""
55+
Controls where ``LazyModule.__setattr__`` writes non-internal
56+
attributes when the real module has not yet been loaded (or when
57+
loading has failed).
58+
59+
Accepted values:
60+
* ``"module"`` (default, backward-compatible): the attribute is
61+
forwarded to the underlying module via ``setattr(real, ...)``.
62+
This means a value written through the proxy becomes visible
63+
globally (because Python modules are singletons in
64+
``sys.modules``). This matches the behaviour of ordinary
65+
``import foo`` followed by ``foo.bar = ...``.
66+
* ``"proxy"``: the attribute is mounted on the proxy shell via
67+
``object.__setattr__(self, ...)``. The underlying module is
68+
**not** loaded as a side effect, and the value is visible only
69+
through this proxy instance. Use this when you want to attach
70+
scratch state to a lazy module without paying for the import
71+
or polluting the real module's namespace.
72+
73+
Why a global instead of a per-instance flag? The two modes are
74+
not usually mixed in the same project; a single global keeps the
75+
API surface small. Change at your own risk and reset it before any
76+
code that expects the default behaviour runs."""
7577

7678
# GNUv3 License, add in <2018>, by <Evan Yang>
7779
class LazyModule(types.ModuleType):

0 commit comments

Comments
 (0)