Skip to content

Commit 7233015

Browse files
authored
Do not transpile empty string keys in typed dicts (#23)
* do not transpile empty string keys in typed dicts * also give the publish workflow a name
1 parent 2866514 commit 7233015

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

.github/workflows/publish.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
name: Publish
2+
13
on:
24
release:
35
types: [created]

src/parseTypeDefinition.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,20 @@ export const parseTypeDefinition = (
4545
} else {
4646
const properties = type
4747
.getProperties()
48+
// empty strings are not allowed for keys in TypedDicts
49+
.filter(v => v.getName() !== "");
50+
51+
const parsedProperties = properties
4852
.map((v) => parsePropertyForDict(state, v));
4953

50-
const propertyDocumentation = type
51-
.getProperties()
54+
const propertyDocumentation = properties
5255
.map((v) => getDocumentationStringForDict(state, v))
5356
.filter(v => v !== undefined)
5457
.join("\n");
5558

5659
const innerDocstring = documentation?.replaceAll("\n", " \n") + (propertyDocumentation.length > 0 ? "\n## Entries\n" + propertyDocumentation : "");
5760
const docstring = innerDocstring.length > 0 ? `\n"""\n${innerDocstring}\n"""` : "";
58-
const definition = `${name} = TypedDict(${JSON.stringify(name)}, {\n ${properties.join(",\n ")}\n})${docstring}`;
61+
const definition = `${name} = TypedDict(${JSON.stringify(name)}, {\n ${parsedProperties.join(",\n ")}\n})${docstring}`;
5962

6063
state.statements.push(definition);
6164
}

src/testing/dicts.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ class A(TypedDict):
100100
);
101101
});
102102

103+
it("ignores empty string property names in functional dicts", async () => {
104+
const result = await transpileString(`export type A = {
105+
"": string,
106+
"foo.bar"?: string,
107+
}`);
108+
expect(result).toContain(
109+
`A = TypedDict("A", {
110+
"foo.bar": NotRequired[str]
111+
})`,
112+
);
113+
});
114+
103115
it("moves the key/value docstrings to the object docstring in the functional syntax", async () => {
104116
const result = await transpileString(`
105117
/** This is A */

0 commit comments

Comments
 (0)