-
Notifications
You must be signed in to change notification settings - Fork 1
prevent generation crash in mixed TS/Python environments and harden s… #101
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
palimad
wants to merge
4
commits into
develop
Choose a base branch
from
4230-mixed-ts-python-returntype
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.
Open
Changes from all commits
Commits
Show all changes
4 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
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
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 |
|---|---|---|
|
|
@@ -71,11 +71,57 @@ def print_red(s: str): | |
| print(Fore.RED + s + Style.RESET_ALL) | ||
|
|
||
|
|
||
| def normalize_cross_language_type(type_name: str) -> str: | ||
| """Normalize TS-flavored type strings into safe Python typing hints. | ||
|
|
||
| This prevents TypeScript utility types (e.g. ReturnType<...>) from leaking | ||
| into Python codegen where they would be treated as importable symbols. | ||
| """ | ||
| value = (type_name or "").strip() | ||
| if not value: | ||
| return "Any" | ||
|
|
||
| primitive_map = { | ||
| "string": "str", | ||
| "number": "float", | ||
| "integer": "int", | ||
| "boolean": "bool", | ||
| "null": "None", | ||
| "void": "None", | ||
| "any": "Any", | ||
| "object": "Dict", | ||
| } | ||
|
|
||
| if value.startswith("Promise<") and value.endswith(">"): | ||
| return normalize_cross_language_type(value[len("Promise<"):-1]) | ||
|
|
||
| if value.startswith("Awaited<") and value.endswith(">"): | ||
| return normalize_cross_language_type(value[len("Awaited<"):-1]) | ||
|
|
||
| if "|" in value: | ||
| parts = [p.strip() for p in value.split("|") if p.strip()] | ||
| normalized = [normalize_cross_language_type(part) for part in parts] | ||
| return " | ".join(normalized) if normalized else "Any" | ||
|
|
||
| if value == "ReturnType" or value.startswith("ReturnType<") or "typeof" in value: | ||
| return "Any" | ||
|
|
||
| return primitive_map.get(value, value) | ||
|
|
||
|
|
||
| def add_type_import_path(function_name: str, arg: str) -> str: | ||
| """if not basic type, coerce to camelCase and add the import path""" | ||
| # from now, we start qualifying non-basic types :)) | ||
| # e.g. Callable[[EmailAddress, Dict, Dict, Dict], None] | ||
| # becomes Callable[[Set_profile_email.EmailAddress, Dict, Dict, Dict], None] | ||
| arg = normalize_cross_language_type(arg) | ||
|
|
||
| if "|" in arg: | ||
| return " | ".join( | ||
| add_type_import_path(function_name, token.strip()) | ||
| for token in arg.split("|") | ||
| if token.strip() | ||
| ) | ||
|
|
||
| if arg.startswith("Callable"): | ||
| inner = arg[len("Callable["):-1] # strip outer Callable[...] | ||
|
|
@@ -90,7 +136,7 @@ def add_type_import_path(function_name: str, arg: str) -> str: | |
| return "Callable[" + ",".join(qualified) + "]" | ||
| # return arg | ||
|
|
||
| if arg in BASIC_PYTHON_TYPES: | ||
| if arg == "Any" or arg in BASIC_PYTHON_TYPES: | ||
| return arg | ||
|
|
||
| if arg.startswith("List["): | ||
|
|
@@ -121,14 +167,23 @@ def get_type_and_def( | |
| return "Any", "" | ||
|
|
||
| if type_spec["kind"] == "plain": | ||
| value = type_spec.get("value", "") | ||
| value = normalize_cross_language_type(type_spec.get("value", "")) | ||
|
Member
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. This now normalizes |
||
|
|
||
| if "|" in value or value in BASIC_PYTHON_TYPES: | ||
| return value, "" | ||
|
|
||
| if value.endswith("[]"): | ||
| primitive = map_primitive_types(value[:-2]) | ||
| primitive = normalize_cross_language_type(value[:-2]) | ||
| if primitive not in BASIC_PYTHON_TYPES: | ||
| primitive = map_primitive_types(primitive) | ||
| return f"List[{primitive}]", "" | ||
| else: | ||
| return map_primitive_types(value), "" | ||
| elif type_spec["kind"] == "primitive": | ||
| return map_primitive_types(type_spec.get("type", "any")), "" | ||
| primitive = normalize_cross_language_type(type_spec.get("type", "any")) | ||
| if primitive in BASIC_PYTHON_TYPES: | ||
| return primitive, "" | ||
| return map_primitive_types(primitive), "" | ||
| elif type_spec["kind"] == "array": | ||
| if type_spec.get("items"): | ||
| items = type_spec["items"] | ||
|
|
||
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
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.
Does bare
ReturnTyperesolve at import time in generated__init__.py?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.
Great catch, thanks. I updated the fix so utility types are normalized before Python annotation generation, which removes the bare ReturnType import-time dependency. So generated imports in init should not be part of the resolution path anymore.