Skip to content

Commit e33bd56

Browse files
authored
ensure that no 'undefined' docstrings get added for functional dict definitions (#35)
1 parent 04d2c03 commit e33bd56

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/parseProperty.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ export const getDocumentationStringForDict = (state: ParserState, symbol: ts.Sym
6565
const documentation = symbol
6666
.getDocumentationComment(state.typechecker)
6767
.map((v) => v.text)
68+
.filter(v => !!v)
6869
.join(" \n");
70+
6971
if (documentation.length > 0) {
7072
return `${JSON.stringify(name)}: ${documentation}`
7173
} else {

src/parseTypeDefinition.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ export const parseTypeDefinition = (
5353

5454
const propertyDocumentation = properties
5555
.map((v) => getDocumentationStringForDict(state, v))
56-
.filter(v => v !== undefined)
56+
.filter(v => !!v)
5757
.join("\n");
5858

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

6364
state.statements.push(definition);

src/testing/dicts.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ class A(TypedDict):
9393
const result = await transpileString(`export type A = {
9494
"foo.bar"?: string,
9595
}`);
96-
expect(result).toContain(
97-
`A = TypedDict("A", {
96+
expect(result).toEqual(
97+
`from typing_extensions import NotRequired, TypedDict
98+
99+
A = TypedDict("A", {
98100
"foo.bar": NotRequired[str]
99101
})`,
100102
);

0 commit comments

Comments
 (0)