Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name: Publish

on:
release:
types: [created]
Expand Down
9 changes: 6 additions & 3 deletions src/parseTypeDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@ export const parseTypeDefinition = (
} else {
const properties = type
.getProperties()
// empty strings are not allowed for keys in TypedDicts
.filter(v => v.getName() !== "");

const parsedProperties = properties
.map((v) => parsePropertyForDict(state, v));

const propertyDocumentation = type
.getProperties()
const propertyDocumentation = properties
.map((v) => getDocumentationStringForDict(state, v))
.filter(v => v !== undefined)
.join("\n");

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

state.statements.push(definition);
}
Expand Down
12 changes: 12 additions & 0 deletions src/testing/dicts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ class A(TypedDict):
);
});

it("ignores empty string property names in functional dicts", async () => {
const result = await transpileString(`export type A = {
"": string,
"foo.bar"?: string,
}`);
expect(result).toContain(
`A = TypedDict("A", {
"foo.bar": NotRequired[str]
})`,
);
});

it("moves the key/value docstrings to the object docstring in the functional syntax", async () => {
const result = await transpileString(`
/** This is A */
Expand Down
Loading