-
Notifications
You must be signed in to change notification settings - Fork 318
fix(api): initialize compatibility exports safely #1789
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import ast | ||
| from pathlib import Path | ||
|
|
||
| CLIENT_IMPORTS = """\ | ||
|
|
@@ -141,10 +142,25 @@ def _append_type_exports(path: Path, imports_by_type: dict[str, str]) -> None: | |
| for type_name, module_name in imports_by_type.items() | ||
| ) | ||
| names = ", ".join(repr(type_name) for type_name in TYPE_NAMES) | ||
| path.write_text( | ||
| f"{contents.rstrip()}\n\n{exports_marker}\n{imports}\n" | ||
| f"__all__ = [*__all__, {names}]\n" | ||
| defines_all = any( | ||
| ( | ||
| isinstance(node, ast.Assign) | ||
| and any( | ||
| isinstance(target, ast.Name) and target.id == "__all__" | ||
| for target in node.targets | ||
| ) | ||
| ) | ||
| or ( | ||
| isinstance(node, ast.AnnAssign) | ||
| and isinstance(node.target, ast.Name) | ||
| and node.target.id == "__all__" | ||
| ) | ||
|
Comment on lines
+153
to
+157
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a generated initializer contains an annotation without a value, such as Useful? React with 👍 / 👎. |
||
| for node in ast.parse(contents).body | ||
| ) | ||
| exports = ( | ||
| f"__all__ = [*__all__, {names}]" if defines_all else f"__all__ = [{names}]" | ||
| ) | ||
| path.write_text(f"{contents.rstrip()}\n\n{exports_marker}\n{imports}\n{exports}\n") | ||
|
|
||
|
|
||
| def _create_type_aliases(types_dir: Path) -> None: | ||
|
|
||
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.
When this version runs on output patched by the previous postprocessor, the existing compatibility marker returns before this new detection executes, leaving
__all__ = [*__all__, ...]unchanged in modules without an earlier binding and causing import-timeNameError. The postprocessor supports reruns, so it needs to repair the old marked output rather than treating every marker as proof that no patch is needed.Prompt To Fix With AI