Skip to content

Commit 8150a64

Browse files
committed
Made suggested changes
1 parent 9593559 commit 8150a64

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

stumpy/__init__.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
from . import cache, config
1212

13-
# Define which functions belong to which submodules
13+
# Define which functions belong to which module
1414
# Key: function name to expose at top level
15-
# Value: name of the submodule
15+
# Value: name of the module
1616
_lazy_imports = {
1717
"aamp": "aamp",
1818
"aamp_mmotifs": "aamp_mmotifs",
@@ -253,18 +253,17 @@ def _get_fastmath_value(module_name, func_name): # pragma: no cover
253253
# PEP 562: module-level __getattr__ for lazy imports
254254
def __getattr__(name): # pragma: no cover
255255
if name in _lazy_imports:
256-
submodule_name = _lazy_imports[name]
257-
full_module_path = f"{__package__}.{submodule_name}"
258-
module = importlib.import_module(full_module_path)
259-
# Retrieve the attribute from the loaded submodule and cache it
256+
mod_name = _lazy_imports[name]
257+
module = importlib.import_module(f"{__package__}.{mod_name}")
258+
# Retrieve the attribute from the loaded module and cache it
260259
attr = getattr(module, name)
261260
globals()[name] = attr
262261
return attr
263262
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
264263

265264

266-
# Ensure that if a submodule was imported during package import
267-
# (causing the package attribute to point to the submodule object), we
265+
# Ensure that if a module was imported during package import
266+
# (causing the package attribute to point to the module object), we
268267
# replace that entry with the actual attribute (e.g., function) so that
269268
# users get the expected callable at `stumpy.aamp` rather than the module.
270269
for _name, _sub in _lazy_imports.items(): # pragma: no cover
@@ -273,28 +272,15 @@ def __getattr__(name): # pragma: no cover
273272
try:
274273
replacement = getattr(val, _name)
275274
except AttributeError:
276-
# Nothing to do if the submodule doesn't define the attribute
275+
# Nothing to do if the module doesn't define the attribute
277276
continue
278277
globals()[_name] = replacement
279278

280279

281-
# Lightweight lazy proxy objects so the attribute exists in the
282-
# module dict immediately. This helps REPLs and completers that
283-
# inspect `module.__dict__` or `dir(module)` prefer the callable/class
284-
# export over a same-named submodule while still deferring the real
285-
# import until first use.
286-
def _resolve_lazy(name, submodule): # pragma: no cover
287-
full_module_path = f"{__package__}.{submodule}"
288-
module = importlib.import_module(full_module_path)
289-
obj = getattr(module, name)
290-
globals()[name] = obj
291-
return obj
292-
293-
294280
# Eagerly import exports that would otherwise collide with
295-
# same-named submodules. This keeps lazy imports for most names but
281+
# same-named modules. This keeps lazy imports for most names but
296282
# ensures that when a top-level exported name exactly matches its
297-
# submodule (e.g., `stump` -> `stump.py`), the exported attribute is
283+
# module (e.g., `stump` -> `stump.py`), the exported attribute is
298284
# available immediately so REPL completers prefer the callable/class
299285
# instead of the module.
300286
for _name, _sub in _lazy_imports.items(): # pragma: no cover
@@ -306,7 +292,7 @@ def _resolve_lazy(name, submodule): # pragma: no cover
306292
try:
307293
globals()[_name] = getattr(module, _name)
308294
except AttributeError:
309-
# If the submodule doesn't define the attribute, keep it lazy
295+
# If the module doesn't define the attribute, keep it lazy
310296
pass
311297
except Exception:
312298
# Be conservative: don't let eager-import attempts raise during package import
@@ -317,14 +303,14 @@ def __dir__(): # pragma: no cover
317303
# Expose lazy names in dir() for discoverability
318304
# Also include __all__ so tools that consult it will see the intended
319305
# top-level exports (this helps some REPL completers prefer the
320-
# callable/class exports over same-named submodules).
306+
# callable/class exports over same-named modules).
321307
all_names = list(globals().keys()) + list(_lazy_imports.keys())
322308
all_names += list(globals().get("__all__", []))
323309
return sorted(all_names)
324310

325311

326312
# Make the lazy-exported names explicit for tools that respect __all__.
327-
# This helps REPL tab-completion prefer functions/classes over submodules
313+
# This helps REPL tab-completion prefer functions/classes over modules
328314
# when names collide (e.g., `stumpy.stump` should point to the function
329315
# rather than the module during completion).
330316
__all__ = sorted(list(_lazy_imports.keys()))

0 commit comments

Comments
 (0)