Skip to content

Commit 35a86ff

Browse files
committed
Keep package namespace clean under lazy init
Alias the internal 'importlib'/'typing' imports to '_importlib'/'_typing' so they no longer leak as public attributes of 'python_utils' (they appeared in dir() and as python_utils.importlib / python_utils.typing under the lazy __init__, which develop did not expose). Add an explicit '# pragma: no cover' to the TYPE_CHECKING block since coverage's auto-exclusion only matches the unaliased 'typing.TYPE_CHECKING' spelling. Verified via a full public-API manifest diff against develop: the only remaining differences are the two inherent consequences of deferring asyncio (aio_timeout_generator's default iterable is now None instead of aio.acount, and python_utils.time no longer re-exposes the 'aio'/'asyncio' modules).
1 parent 28cbd34 commit 35a86ff

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

python_utils/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@
5555
LoggerBase
5656
"""
5757

58-
import importlib
59-
import typing
58+
import importlib as _importlib
59+
import typing as _typing
6060

61-
if typing.TYPE_CHECKING:
61+
if _typing.TYPE_CHECKING: # pragma: no cover
6262
# Eager imports for type checkers only; the runtime equivalents are loaded
6363
# lazily by ``__getattr__`` below. Names appear in ``__all__`` so they are
6464
# treated as re-exports (not unused imports).
@@ -155,12 +155,14 @@
155155
}
156156

157157

158-
def __getattr__(name: str) -> typing.Any:
158+
def __getattr__(name: str) -> _typing.Any:
159159
"""Lazily import submodules and their exported names on first access."""
160160
if name in _SUBMODULES:
161-
module = importlib.import_module(f'.{name}', __name__)
161+
module = _importlib.import_module(f'.{name}', __name__)
162162
elif name in _NAME_TO_MODULE:
163-
module = importlib.import_module(f'.{_NAME_TO_MODULE[name]}', __name__)
163+
module = _importlib.import_module(
164+
f'.{_NAME_TO_MODULE[name]}', __name__
165+
)
164166
value = getattr(module, name)
165167
globals()[name] = value # cache so __getattr__ runs only once
166168
return value

0 commit comments

Comments
 (0)