-
Notifications
You must be signed in to change notification settings - Fork 41
Defer asyncio and typing_extensions imports (lazy package init) #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wolph
wants to merge
4
commits into
develop
Choose a base branch
from
lazy-imports
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−37
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e8c055a
Defer asyncio and typing_extensions imports (lazy package init)
wolph d469a21
Address PR review: accurate cast, restore typing_extensions, dir() & …
wolph 28cbd34
Merge remote-tracking branch 'origin/develop' into lazy-imports
wolph 35a86ff
Keep package namespace clean under lazy init
wolph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """Tests for the lazy-import machinery added to keep `import python_utils` | ||
| light (PEP 562 `__getattr__`/`__dir__` in the package). | ||
| """ | ||
|
|
||
| import os | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| import python_utils | ||
| from python_utils import types | ||
|
|
||
|
|
||
| def test_package_lazy_attribute_access() -> None: | ||
| # Submodule access and exported-name access both resolve via __getattr__. | ||
| assert python_utils.aio is python_utils.aio | ||
| assert callable(python_utils.acount) | ||
| missing = 'definitely_not_a_real_attribute' | ||
| with pytest.raises(AttributeError): | ||
| getattr(python_utils, missing) | ||
|
|
||
|
|
||
| def test_bare_import_stays_light() -> None: | ||
| # Importing the package must not eagerly pull in heavy/optional deps. This | ||
| # runs in a clean interpreter because the test session itself has long | ||
| # since imported asyncio/typing_extensions. | ||
| code = ( | ||
| 'import sys, python_utils\n' | ||
| "assert 'asyncio' not in sys.modules, " | ||
| "sorted(m for m in sys.modules if m.startswith('asyncio'))\n" | ||
| "assert 'typing_extensions' not in sys.modules\n" | ||
| ) | ||
| env = {**os.environ, 'PYTHONPATH': os.pathsep.join(sys.path)} | ||
| result = subprocess.run( | ||
| [sys.executable, '-c', code], | ||
| capture_output=True, | ||
| text=True, | ||
| env=env, | ||
| ) | ||
| assert result.returncode == 0, result.stderr | ||
|
|
||
|
|
||
| def test_first_access_caches_into_module_dict() -> None: | ||
| # PEP 562 __getattr__ runs once: the resolved object is cached in the | ||
| # module namespace so subsequent lookups skip __getattr__ entirely. | ||
| module = python_utils.time | ||
| assert python_utils.__dict__['time'] is module | ||
|
|
||
| func = python_utils.format_time | ||
| assert python_utils.__dict__['format_time'] is func | ||
|
|
||
|
|
||
| def test_dir_lists_lazy_submodules() -> None: | ||
| # Lazy submodules that are not in __all__ (e.g. ``containers`` and | ||
| # ``exceptions``) must still be discoverable via ``dir``; tools such as | ||
| # ``import_global`` intersect requested names with ``dir(module)``. | ||
| names = set(dir(python_utils)) | ||
| assert {'containers', 'exceptions'} <= names | ||
| assert set(python_utils.__all__) <= names | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_aio_timeout_generator_default_iterable() -> None: | ||
| # With no iterable the generator defaults to ``aio.acount`` -- exercising | ||
| # the lazy ``aio``/``asyncio`` import and the None-resolution branch. | ||
| count = 0 | ||
| generator: types.AsyncGenerator[object, None] = ( | ||
| python_utils.aio_timeout_generator(timeout=0.05, interval=0.0) | ||
| ) | ||
| async for _ in generator: | ||
| count += 1 | ||
| if count >= 2: | ||
| break | ||
|
|
||
| assert count == 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.