-
Notifications
You must be signed in to change notification settings - Fork 0
fix(python): guard generated typing_extensions imports for python 3.11+ #3
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # ruff: noqa: INP001 # this directory is a script collection, not an importable package | ||
| """Post-process openapi-python-client output to use stdlib ``typing.Self`` on 3.11+. | ||
|
|
||
| openapi-python-client (0.28 at time of writing) emits an unconditional | ||
| ``from typing_extensions import Self`` in every generated model. ``typing.Self`` | ||
| is in the stdlib from Python 3.11 onward (PEP 673), and the project declares | ||
| ``typing-extensions`` as a runtime dep only on ``python_version < '3.11'`` — so | ||
| without this rewrite a fresh install on 3.11+ raises ``ModuleNotFoundError`` at | ||
| ``import kreuzberg_cloud`` time. | ||
|
|
||
| The rewrite uses a ``sys.version_info`` guard (PEP 484 version narrowing) so | ||
| mypy can statically resolve ``Self`` to the stdlib alias on modern Pythons and | ||
| to ``typing_extensions`` on 3.10. The pattern mirrors the one already used by | ||
| the handwritten ``client.py``. Safe to run multiple times on the same tree. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pathlib | ||
| import sys | ||
|
|
||
| _OLD = "from typing_extensions import Self" | ||
| _NEW = ( | ||
| "if sys.version_info >= (3, 11):\n" | ||
| " from typing import Self\n" | ||
| "else:\n" | ||
| " from typing_extensions import Self" | ||
| ) | ||
| _ALREADY_PATCHED_MARKER = "if sys.version_info >= (3, 11):\n from typing import Self" | ||
|
|
||
|
|
||
| def _ensure_import_sys(src: str) -> str: | ||
| """Insert ``import sys`` at the top of the import block if absent.""" | ||
| if "\nimport sys\n" in src or src.startswith("import sys\n"): | ||
| return src | ||
| lines = src.splitlines(keepends=True) | ||
| insert_at = 0 | ||
| for idx, line in enumerate(lines): | ||
| if line.startswith("from __future__"): | ||
| insert_at = idx + 1 | ||
| break | ||
| lines.insert(insert_at, "import sys\n") | ||
| return "".join(lines) | ||
|
|
||
|
|
||
| def _patch_file(path: pathlib.Path) -> bool: | ||
| """Rewrite one generated file. Returns True if it was modified.""" | ||
| src = path.read_text() | ||
| if _ALREADY_PATCHED_MARKER in src: | ||
| return False | ||
| if _OLD not in src: | ||
| return False | ||
| src = _ensure_import_sys(src) | ||
| path.write_text(src.replace(_OLD, _NEW)) | ||
| return True | ||
|
|
||
|
|
||
| def main(argv: list[str]) -> int: | ||
| """Walk ``argv[1]`` and rewrite every generated model that imports ``Self``.""" | ||
| if len(argv) != 2: | ||
| sys.stderr.write(f"usage: {argv[0]} <generated-dir>\n") | ||
| return 2 | ||
| root = pathlib.Path(argv[1]) | ||
| if not root.is_dir(): | ||
| sys.stderr.write(f"error: not a directory: {root}\n") | ||
| return 2 | ||
| patched = sum(_patch_file(p) for p in root.rglob("*.py")) | ||
| sys.stderr.write(f"postprocess_generated: patched {patched} file(s) under {root}\n") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main(sys.argv)) |
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,33 @@ | ||
| """Regression check that the post-codegen ``Self`` import rewrite stays applied. | ||
|
|
||
| ``openapi-python-client`` emits an unconditional ``from typing_extensions import Self`` | ||
| in every generated model. ``tasks/python.yml::generate`` runs | ||
| ``scripts/postprocess_generated.py`` to rewrite those imports to a guarded | ||
| ``try/except`` so stdlib ``typing.Self`` is preferred on Python 3.11+. Without | ||
| that rewrite, fresh installs on 3.11+ raise ``ModuleNotFoundError`` because the | ||
| ``typing-extensions`` runtime dependency is gated to ``python_version < '3.11'``. | ||
|
|
||
| If this test fails, run ``task python:generate`` so the post-codegen step kicks | ||
| in, or check that the script in ``packages/python/scripts/`` still produces the | ||
| expected output for the openapi-python-client version in use. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| _GENERATED = Path(__file__).resolve().parents[1] / "src" / "kreuzberg_cloud" / "_generated" | ||
|
|
||
|
|
||
| def test_generated_models_use_guarded_self_import() -> None: | ||
| offenders: list[str] = [] | ||
| for path in _GENERATED.rglob("*.py"): | ||
| for line in path.read_text().splitlines(): | ||
| if line == "from typing_extensions import Self": | ||
| offenders.append(str(path.relative_to(_GENERATED))) | ||
| break | ||
| assert not offenders, ( | ||
| "Generated files have an unconditional `from typing_extensions import Self` — " | ||
| "the post-codegen rewrite in tasks/python.yml::generate must run after every " | ||
| "regeneration. Offending files:\n " + "\n ".join(offenders) | ||
| ) | ||
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.
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.
Unnecessary - this is only required for python lower than 3.10, which is anyhow unsupported