Skip to content

Commit 183abba

Browse files
Fix Python codegen
Co-Authored-By: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a4229b4 commit 183abba

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

python/copilot/generated/rpc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ class SessionUIElicitationResult:
15931593
action: Action
15941594
"""The user's response: accept (submitted), decline (rejected), or cancel (dismissed)"""
15951595

1596-
content: Optional[dict[str, float | bool | list[str] | str]] = None
1596+
content: dict[str, float | bool | list[str] | str] | None = None
15971597
"""The form values submitted by the user (present when action is 'accept')"""
15981598

15991599
@staticmethod
@@ -1696,7 +1696,7 @@ class PropertyType(Enum):
16961696
@dataclass
16971697
class Property:
16981698
type: PropertyType
1699-
default: Optional[float | bool | list[str] | str] = None
1699+
default: float | bool | list[str] | str | None = None
17001700
description: str | None = None
17011701
enum: list[str] | None = None
17021702
enum_names: list[str] | None = None

scripts/codegen/python.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,37 @@ import {
3232
* - Callable from collections.abc instead of typing
3333
* - Clean up unused typing imports
3434
*/
35+
function replaceBalancedBrackets(code: string, prefix: string, replacer: (inner: string) => string): string {
36+
let result = "";
37+
let i = 0;
38+
while (i < code.length) {
39+
const idx = code.indexOf(prefix + "[", i);
40+
if (idx === -1) {
41+
result += code.slice(i);
42+
break;
43+
}
44+
result += code.slice(i, idx);
45+
const start = idx + prefix.length + 1; // after '['
46+
let depth = 1;
47+
let j = start;
48+
while (j < code.length && depth > 0) {
49+
if (code[j] === "[") depth++;
50+
else if (code[j] === "]") depth--;
51+
j++;
52+
}
53+
const inner = code.slice(start, j - 1);
54+
result += replacer(inner);
55+
i = j;
56+
}
57+
return result;
58+
}
59+
3560
function modernizePython(code: string): string {
36-
// Replace Optional[X] with X | None (handles nested brackets)
37-
code = code.replace(/Optional\[([^\[\]]*(?:\[[^\[\]]*\])*[^\[\]]*)\]/g, "$1 | None");
61+
// Replace Optional[X] with X | None (handles arbitrarily nested brackets)
62+
code = replaceBalancedBrackets(code, "Optional", (inner) => `${inner} | None`);
3863

3964
// Replace Union[X, Y] with X | Y
40-
code = code.replace(/Union\[([^\[\]]*(?:\[[^\[\]]*\])*[^\[\]]*)\]/g, (_match, inner: string) => {
65+
code = replaceBalancedBrackets(code, "Union", (inner) => {
4166
return inner.split(",").map((s: string) => s.trim()).join(" | ");
4267
});
4368

0 commit comments

Comments
 (0)