Skip to content

Commit 48e244d

Browse files
stephentoubCopilot
andauthored
Clean up redundant Python codegen lambdas (#1104)
Unwrap redundant passthrough lambdas in the Python generator, add a regression test, and regenerate the Python session events output. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent dbcea81 commit 48e244d

3 files changed

Lines changed: 489 additions & 430 deletions

File tree

nodejs/test/python-codegen.test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe("python session event codegen", () => {
7474
'action = from_union([from_none, lambda x: parse_enum(SessionSyntheticDataAction, x)], obj.get("action", "store"))'
7575
);
7676
expect(code).toContain(
77-
'summary = from_union([from_none, lambda x: from_str(x)], obj.get("summary", ""))'
77+
'summary = from_union([from_none, from_str], obj.get("summary", ""))'
7878
);
7979
expect(code).toContain("uri: str");
8080
expect(code).toContain("pattern: str");
@@ -83,6 +83,53 @@ describe("python session event codegen", () => {
8383
expect(code).toContain("count: int");
8484
});
8585

86+
it("collapses redundant callable wrapper lambdas", () => {
87+
const schema: JSONSchema7 = {
88+
definitions: {
89+
SessionEvent: {
90+
anyOf: [
91+
{
92+
type: "object",
93+
required: ["type", "data"],
94+
properties: {
95+
type: { const: "session.synthetic" },
96+
data: {
97+
type: "object",
98+
properties: {
99+
summary: { type: "string" },
100+
tags: {
101+
type: "array",
102+
items: { type: "string" },
103+
},
104+
context: {
105+
type: "object",
106+
properties: {
107+
gitRoot: { type: "string" },
108+
},
109+
},
110+
},
111+
},
112+
},
113+
},
114+
],
115+
},
116+
},
117+
};
118+
119+
const code = generatePythonSessionEventsCode(schema);
120+
121+
expect(code).toContain('summary = from_union([from_none, from_str], obj.get("summary"))');
122+
expect(code).toContain(
123+
'tags = from_union([from_none, lambda x: from_list(from_str, x)], obj.get("tags"))'
124+
);
125+
expect(code).toContain(
126+
'context = from_union([from_none, SessionSyntheticDataContext.from_dict], obj.get("context"))'
127+
);
128+
expect(code).not.toContain("lambda x: from_str(x)");
129+
expect(code).not.toContain("lambda x: SessionSyntheticDataContext.from_dict(x)");
130+
expect(code).not.toContain("from_list(lambda x: from_str(x), x)");
131+
});
132+
86133
it("preserves key shortened nested type names", () => {
87134
const schema: JSONSchema7 = {
88135
definitions: {

0 commit comments

Comments
 (0)