Repository navigation
Python: Declare every lazy namespace export in its type stub #9750
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
Draft
Naveen Chatlapalli (1aifanatic)
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
1aifanatic:contrib/python-namespace-stub-drift
base: main
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.
Draft
Changes from all commits
Commits
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
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
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
19 changes: 19 additions & 0 deletions
19
python/packages/core/agent_framework/hyperlight/__init__.pyi
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,19 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| from agent_framework_hyperlight import ( | ||
| AllowedDomain, | ||
| AllowedDomainInput, | ||
| FileMount, | ||
| FileMountInput, | ||
| HyperlightCodeActProvider, | ||
| HyperlightExecuteCodeTool, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "AllowedDomain", | ||
| "AllowedDomainInput", | ||
| "FileMount", | ||
| "FileMountInput", | ||
| "HyperlightCodeActProvider", | ||
| "HyperlightExecuteCodeTool", | ||
| ] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| """Keep the lazy ``agent_framework.<namespace>`` modules and their type stubs in sync. | ||
|
|
||
| Each namespace re-exports connector classes lazily through ``_IMPORTS`` and declares | ||
| the same names in ``__init__.pyi`` for type checkers. A name missing from the stub | ||
| imports fine at runtime but is rejected by type checkers, so compare them statically. | ||
| """ | ||
|
|
||
| import ast | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| _PACKAGE_ROOT = Path(__file__).parents[2] / "agent_framework" | ||
|
|
||
|
|
||
| def _lazy_namespaces() -> list[Path]: | ||
| namespaces: list[Path] = [] | ||
| for init in sorted(_PACKAGE_ROOT.glob("*/__init__.py")): | ||
| if _lazy_imports(init) is not None: | ||
| namespaces.append(init.parent) | ||
| return namespaces | ||
|
|
||
|
|
||
| def _lazy_imports(init: Path) -> dict[str, str | None] | None: | ||
| """Return the lazily exported names mapped to their source module, if declared.""" | ||
| module = ast.parse(init.read_text(encoding="utf-8")) | ||
| import_path: str | None = None | ||
| for node in module.body: | ||
| if isinstance(node, ast.Assign) and len(node.targets) == 1: | ||
| target, assigned = node.targets[0], node.value | ||
| elif isinstance(node, ast.AnnAssign) and node.value is not None: | ||
| target, assigned = node.target, node.value | ||
| else: | ||
| continue | ||
| if not isinstance(target, ast.Name): | ||
| continue | ||
| if target.id == "IMPORT_PATH": | ||
| import_path = ast.literal_eval(assigned) | ||
| elif target.id == "_IMPORTS": | ||
| value = ast.literal_eval(assigned) | ||
| if isinstance(value, dict): | ||
| return {name: source[0] for name, source in value.items()} | ||
| return dict.fromkeys(value, import_path) | ||
| return None | ||
|
|
||
|
|
||
| def _stub_exports(stub: Path) -> tuple[dict[str, str | None], set[str]]: | ||
| module = ast.parse(stub.read_text(encoding="utf-8")) | ||
| imported: dict[str, str | None] = {} | ||
| exported: set[str] = set() | ||
| for node in module.body: | ||
| if isinstance(node, ast.ImportFrom): | ||
| for alias in node.names: | ||
| imported[alias.asname or alias.name] = node.module | ||
| elif isinstance(node, ast.Assign) and any( | ||
| isinstance(target, ast.Name) and target.id == "__all__" for target in node.targets | ||
| ): | ||
| exported = set(ast.literal_eval(node.value)) | ||
| return imported, exported | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("namespace", _lazy_namespaces(), ids=lambda path: path.name) | ||
| def test_namespace_stub_declares_every_lazy_export(namespace: Path) -> None: | ||
| lazy_imports = _lazy_imports(namespace / "__init__.py") | ||
| assert lazy_imports is not None | ||
| stub = namespace / "__init__.pyi" | ||
| assert stub.exists(), f"agent_framework.{namespace.name} has lazy exports but no __init__.pyi" | ||
|
|
||
| stub_imports, stub_all = _stub_exports(stub) | ||
|
|
||
| assert set(stub_imports) == set(lazy_imports) | ||
| assert stub_all == set(lazy_imports) | ||
| for name, source in lazy_imports.items(): | ||
| stub_source = stub_imports[name] | ||
| assert source is not None | ||
| assert stub_source is not None | ||
| # The stub may import from a submodule of the same distribution package. | ||
| assert stub_source.split(".")[0] == source.split(".")[0], name | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've left these in the test module. My reading of that section is that it's about where shared helpers go: fixtures in
conftest.py, other reusable helpers in an imported module rather thanconftest.py. These three functions are only used by this file. That matches the existing pattern in this directory: 30test_*.pymodules underpackages/core/tests/coredefine module-private helpers, including the closest sibling,test_docstrings.py(6 helpers). Happy to move them into a helper module if maintainers read the rule more strictly.