Skip to content

Commit ccd7867

Browse files
authored
Merge branch 'main' into ci/precommit
2 parents 66a3af5 + 9f05c1e commit ccd7867

File tree

7 files changed

+59
-44
lines changed

7 files changed

+59
-44
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ jobs:
3434
# python -m pytest --cov=lazy_loader --doctest-modules --durations=20
3535
3636
- name: Upload coverage to Codecov
37-
uses: codecov/codecov-action@v4
37+
uses: codecov/codecov-action@v5
3838
with:
3939
token: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/milestone-merged-prs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
name: attach to PR
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: scientific-python/attach-next-milestone-action@bc07be829f693829263e57d5e8489f4e57d3d420
15+
- uses: scientific-python/attach-next-milestone-action@c9cfab10ad0c67fed91b01103db26b7f16634639
1616
with:
1717
token: ${{ secrets.MILESTONE_LABELER_TOKEN }}
1818
force: true

.github/workflows/test.yml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,7 @@ jobs:
1313
matrix:
1414
os: [ubuntu, macos, windows]
1515
python-version:
16-
[
17-
"3.8",
18-
"3.9",
19-
"3.10",
20-
"3.11",
21-
"3.12",
22-
"3.13-dev",
23-
"pypy-3.8",
24-
"pypy-3.9",
25-
]
16+
["3.9", "3.10", "3.11", "3.12", "3.13", "pypy-3.9", "pypy-3.10"]
2617

2718
steps:
2819
- uses: actions/checkout@v4

.pre-commit-config.yaml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
# Install pre-commit hooks via
22
# pre-commit install
3-
43
default_language_version:
54
python: python3
65

76
ci:
8-
autofix_prs: true
9-
autoupdate_commit_msg: "[pre-commit.ci] pre-commit suggestions"
7+
autofix_prs: false
8+
autofix_commit_msg: |
9+
'[pre-commit.ci 🤖] Apply code format tools to PR'
1010
autoupdate_schedule: quarterly
11-
# submodules: true
1211

1312
repos:
1413
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: 2c9f875913ee60ca25ce70243dc24d5b6415598c # frozen: v4.6.0
14+
rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
1615
hooks:
1716
- id: trailing-whitespace
1817
- id: end-of-file-fixer
@@ -25,15 +24,15 @@ repos:
2524
- id: check-toml
2625
- id: check-added-large-files
2726

28-
- repo: https://github.com/pre-commit/mirrors-prettier
29-
rev: ffb6a759a979008c0e6dff86e39f4745a2d9eac4 # frozen: v3.1.0
27+
- repo: https://github.com/rbubley/mirrors-prettier
28+
rev: bc7af46104f0f5368b95878decf720f9f00c2559 # frozen: v3.4.2
3029
hooks:
3130
- id: prettier
3231
files: \.(html|md|yml|yaml|toml)
3332
args: [--prose-wrap=preserve]
3433

3534
- repo: https://github.com/astral-sh/ruff-pre-commit
36-
rev: 1dc9eb131c2ea4816c708e4d85820d2cc8542683 # frozen: v0.5.0
35+
rev: 89c421dff2e1026ba12cdb9ebd731f4a83aa8021 # frozen: v0.8.6
3736
hooks:
3837
- id: ruff
3938
args: ["--fix", "--show-fixes", "--exit-non-zero-on-fix"]

lazy_loader/__init__.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import warnings
1616

1717
__version__ = "0.5rc0.dev0"
18-
__all__ = ["attach", "load", "attach_stub"]
18+
__all__ = ["attach", "attach_stub", "load"]
1919

2020

2121
threadlock = threading.Lock()
@@ -90,13 +90,13 @@ def __getattr__(name):
9090
raise AttributeError(f"No {package_name} attribute {name}")
9191

9292
def __dir__():
93-
return __all__
93+
return __all__.copy()
9494

9595
if os.environ.get("EAGER_IMPORT", ""):
9696
for attr in set(attr_to_modules.keys()) | submodules:
9797
__getattr__(attr)
9898

99-
return __getattr__, __dir__, list(__all__)
99+
return __getattr__, __dir__, __all__.copy()
100100

101101

102102
class DelayedImportErrorModule(types.ModuleType):
@@ -118,7 +118,7 @@ def __getattr__(self, x):
118118
)
119119

120120

121-
def load(fullname, *, require=None, error_on_import=False):
121+
def load(fullname, *, require=None, error_on_import=False, suppress_warning=False):
122122
"""Return a lazily imported proxy for a module.
123123
124124
We often see the following pattern::
@@ -174,6 +174,10 @@ def myfunc():
174174
Whether to postpone raising import errors until the module is accessed.
175175
If set to `True`, import errors are raised as soon as `load` is called.
176176
177+
suppress_warning : bool
178+
Whether to prevent emitting a warning when loading subpackages.
179+
If set to `True`, no warning will occur.
180+
177181
Returns
178182
-------
179183
pm : importlib.util._LazyModule
@@ -189,10 +193,10 @@ def myfunc():
189193
if have_module and require is None:
190194
return module
191195

192-
if "." in fullname:
196+
if not suppress_warning and "." in fullname:
193197
msg = (
194198
"subpackages can technically be lazily loaded, but it causes the "
195-
"package to be eagerly loaded even if it is already lazily loaded."
199+
"package to be eagerly loaded even if it is already lazily loaded. "
196200
"So, you probably shouldn't use subpackages with this lazy feature."
197201
)
198202
warnings.warn(msg, RuntimeWarning)
@@ -222,21 +226,19 @@ def myfunc():
222226
raise ModuleNotFoundError(not_found_message)
223227
import inspect
224228

225-
try:
226-
parent = inspect.stack()[1]
227-
frame_data = {
228-
"filename": parent.filename,
229-
"lineno": parent.lineno,
230-
"function": parent.function,
231-
"code_context": parent.code_context,
232-
}
233-
return DelayedImportErrorModule(
234-
frame_data,
235-
"DelayedImportErrorModule",
236-
message=not_found_message,
237-
)
238-
finally:
239-
del parent
229+
parent = inspect.stack()[1]
230+
frame_data = {
231+
"filename": parent.filename,
232+
"lineno": parent.lineno,
233+
"function": parent.function,
234+
"code_context": parent.code_context,
235+
}
236+
del parent
237+
return DelayedImportErrorModule(
238+
frame_data,
239+
"DelayedImportErrorModule",
240+
message=not_found_message,
241+
)
240242

241243
if spec is not None:
242244
module = importlib.util.module_from_spec(spec)

lazy_loader/tests/test_lazy_loader.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@ def test_lazy_attach():
104104
assert locls[k] == v
105105

106106

107+
def test_lazy_attach_returns_copies():
108+
_get, _dir, _all = lazy.attach(
109+
__name__, ["my_submodule", "another_submodule"], {"foo": ["some_attr"]}
110+
)
111+
assert _dir() is not _dir()
112+
assert _dir() == _all
113+
assert _dir() is not _all
114+
115+
expected = ["another_submodule", "my_submodule", "some_attr"]
116+
assert _dir() == expected
117+
assert _all == expected
118+
assert _dir() is not _all
119+
120+
_dir().append("modify_returned_list")
121+
assert _dir() == expected
122+
assert _all == expected
123+
assert _dir() is not _all
124+
125+
_all.append("modify_returned_all")
126+
assert _dir() == expected
127+
assert _all == [*expected, "modify_returned_all"]
128+
129+
107130
def test_attach_same_module_and_attr_name():
108131
from lazy_loader.tests import fake_pkg
109132

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "lazy_loader"
7-
requires-python = ">=3.8"
7+
requires-python = ">=3.9"
88
authors = [{name = "Scientific Python Developers"}]
99
readme = "README.md"
1010
license = {file = "LICENSE.md"}
@@ -13,11 +13,11 @@ classifiers = [
1313
"Development Status :: 4 - Beta",
1414
"License :: OSI Approved :: BSD License",
1515
"Programming Language :: Python :: 3",
16-
"Programming Language :: Python :: 3.8",
1716
"Programming Language :: Python :: 3.9",
1817
"Programming Language :: Python :: 3.10",
1918
"Programming Language :: Python :: 3.11",
2019
"Programming Language :: Python :: 3.12",
20+
"Programming Language :: Python :: 3.13",
2121
]
2222
description = "Makes it easy to load subpackages and functions on demand."
2323
dependencies = [
@@ -26,7 +26,7 @@ dependencies = [
2626

2727
[project.optional-dependencies]
2828
test = ["pytest >= 8.0", "pytest-cov >= 5.0"]
29-
lint = ["pre-commit == 4.0.1"]
29+
lint = ["pre-commit == 4.1.0"]
3030
dev = ["changelist == 0.5"]
3131

3232
[project.urls]

0 commit comments

Comments
 (0)