-
Notifications
You must be signed in to change notification settings - Fork 226
Add build time python type stub generation #1817
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
Closed
+107
−0
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7ab4fdf
Add type info compilation
c92e5d7
install numpy as stubgen dependency
9f7b72a
add --user to avoid pipeline permission issue
4ea772d
pipeline compatibility fixes
6b87957
Replace pybind stubgen with mypy
chemwolf6922 a7e68cb
Avoid build system from splitting string
chemwolf6922 db8cfd1
Do most of the work in python instead
chemwolf6922 a4a7dd5
call mypy in subprocess
chemwolf6922 a26b53c
Make copilot happy
chemwolf6922 1afcf84
Force exit 0 when it should
chemwolf6922 55ccaa6
try mask the non zero return code for ado
chemwolf6922 0a1f60c
skip stubgen for win arm64
chemwolf6922 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
Empty file.
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,83 @@ | ||
| ''' | ||
| Use this script to leverage python to find the stubgen installation. | ||
| ''' | ||
|
|
||
| from pathlib import Path | ||
| from argparse import ArgumentParser | ||
| import subprocess | ||
| import sys | ||
| import platform | ||
| import shutil | ||
|
|
||
| def install_package(package_name: str, version: str | None = None) -> bool: | ||
| # Check if the package is already installed, is so, return False, meaning we did not install it. | ||
| try: | ||
| __import__(package_name) | ||
| return False | ||
| except ImportError: | ||
| pass | ||
| # First try install with --user, otherwise try install without --user. | ||
| package_spec = f"{package_name}=={version}" if version else package_name | ||
| print(f"Installing package: {package_spec}") | ||
| try: | ||
| subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--user', package_spec]) | ||
| except subprocess.CalledProcessError: | ||
| subprocess.check_call([sys.executable, '-m', 'pip', 'install', package_spec]) | ||
| return True | ||
|
|
||
| def try_uninstall_package(package_name: str) -> None: | ||
| try: | ||
| subprocess.check_call([sys.executable, '-m', 'pip', 'uninstall', '-y', package_name]) | ||
| except subprocess.CalledProcessError: | ||
| # Ignore any error. | ||
| pass | ||
|
|
||
| def generate_stubs(package: str, output: Path) -> None: | ||
| # Call in a subprocess to update sys.path correctly. | ||
| subprocess.check_call([sys.executable, '-c', f'from mypy.stubgen import main; main(["-p", "{package}", "-o", "{output.as_posix()}"])']) | ||
|
|
||
| def fix_stubs(root: Path) -> None: | ||
| # Try fix all .pyi files, though only the one generated from the native library needs fixing. | ||
| stub_files = root.rglob('*.pyi') | ||
| for stub_file in stub_files: | ||
| content = stub_file.read_text() | ||
| fixed_content = content.replace("Oga", "") | ||
| stub_file.write_text(fixed_content) | ||
|
|
||
| def clean_up_pycache(root: Path) -> None: | ||
| pycache_dirs = root.rglob('__pycache__') | ||
| for pycache_dir in pycache_dirs: | ||
| shutil.rmtree(pycache_dir, ignore_errors=True) | ||
|
|
||
| def main(): | ||
| parser = ArgumentParser() | ||
| parser.add_argument('-r', '--wheel_root', type=Path, required=True, help='Wheel root directory containing __init__.py') | ||
| parser.add_argument('-v', '--ort-version', type=str, help='onnxruntime version to install, if not specified, the latest version will be installed') | ||
| args = parser.parse_args() | ||
|
|
||
| try: | ||
| onnxruntime_installed_by_us = install_package('onnxruntime', args.ort_version) | ||
| mypy_installed_by_us = install_package('mypy') | ||
| except subprocess.CalledProcessError as e: | ||
| print(f"Failed to install dependencies on this platform:\n{e}") | ||
| print("Skipping type stub generation.") | ||
| return | ||
|
|
||
| wheel_root = Path(args.wheel_root).resolve() | ||
| generate_stubs(wheel_root.name, wheel_root.parent) | ||
| fix_stubs(wheel_root) | ||
| clean_up_pycache(wheel_root) | ||
|
|
||
| if onnxruntime_installed_by_us: | ||
| try_uninstall_package('onnxruntime') | ||
| if mypy_installed_by_us: | ||
| try_uninstall_package('mypy') | ||
|
|
||
| if __name__ == '__main__': | ||
| # Explicitly skip windows ARM64. | ||
chemwolf6922 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # There is a cmake + MSVC issue causing the build to fail if the pip install subprocess fails. | ||
| # Even if we handled the exception and exited 0. | ||
| if sys.platform == 'win32' and platform.machine().lower() == 'arm64': | ||
| print('Stub generation is not supported on Windows ARM64 due to lack of onnxruntime package.') | ||
| exit(0) | ||
| main() | ||
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.