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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/build
/yarn-error.log
/dist
.DS_Store
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"js/ts.tsdk.path": "node_modules/typescript/lib"
}
39 changes: 39 additions & 0 deletions src/parseProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,42 @@ export const parseProperty = (state: ParserState, symbol: ts.Symbol) => {
return `${name}: ${definition}${documentationSuffix}`;
}
};

export const parsePropertyForDict = (state: ParserState, symbol: ts.Symbol) => {
const name = JSON.stringify(symbol.getName());
if (symbol.flags & ts.SymbolFlags.Optional) {
state.imports.add("NotRequired");
const definition = parseInlineType(
state,
// since the entry is already options, the inner type can be non-nullable
state.typechecker.getNonNullableType(state.typechecker.getTypeOfSymbol(symbol)),
);
if (state.config.nullableOptionals) {
state.imports.add("Optional");
return `${name}: NotRequired[Optional[${definition}]]`;
} else {
return `${name}: NotRequired[${definition}]`;
}
} else {
const definition = parseInlineType(
state,
// since the entry is already options, the inner type can be non-nullable
state.typechecker.getTypeOfSymbol(symbol),
);
return `${name}: ${definition}`;
}
};

export const getDocumentationStringForDict = (state: ParserState, symbol: ts.Symbol) => {
const name = symbol.getName();
const documentation = symbol
.getDocumentationComment(state.typechecker)
.map((v) => v.text)
.join(" \n");
if (documentation.length > 0) {
return `${JSON.stringify(name)}: ${documentation}`
} else {
return undefined;
}
}

44 changes: 33 additions & 11 deletions src/parseTypeDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ts from "typescript";
import { ParserState } from "./ParserState";
import { parseProperty } from "./parseProperty";
import { getDocumentationStringForDict, parseProperty, parsePropertyForDict } from "./parseProperty";
import { getDocumentationStringForType } from "./getDocumentationStringForType";
import { tryToParseInlineType } from "./parseInlineType";
import { isValidPythonIdentifier } from "./isValidPythonIdentifier";
Expand All @@ -25,18 +25,40 @@ export const parseTypeDefinition = (
state.statements.push(definition);
} else {
state.imports.add("TypedDict");

const properties = type
const allKeysAreValidPythonIdentifiers = type
.getProperties()
.filter((v) => isValidPythonIdentifier(v.getName()))
.map((v) => parseProperty(state, v));
.map((v) => isValidPythonIdentifier(v.getName()))
.reduce((a,b) => a&&b, true);

const definition = `class ${name}(TypedDict):${
documentation
? `\n """\n ${documentation.replaceAll("\n", " \n")}\n """`
: ""
}\n ${properties.length > 0 ? properties.join(`\n `) : "pass"}`;
if (allKeysAreValidPythonIdentifiers) {
const properties = type
.getProperties()
.map((v) => parseProperty(state, v));

const definition = `class ${name}(TypedDict):${
documentation
? `\n """\n ${documentation.replaceAll("\n", " \n")}\n """`
: ""
}\n ${properties.length > 0 ? properties.join(`\n `) : "pass"}`;
state.statements.push(definition);
} else {
const properties = type
.getProperties()
.map((v) => parsePropertyForDict(state, v));

const propertyDocumentation = type
.getProperties()
.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}`;

state.statements.push(definition);
}

state.statements.push(definition);
}
};
41 changes: 38 additions & 3 deletions src/testing/dicts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class A(TypedDict):
expect(result).toContain(`class A(TypedDict):\n foo: str\n bar: float`);
});

it("transpiles optional values as NotRequired[Optional[T]]", async () => {
it("transpiles optional values as NotRequired[T]", async () => {
const result = await transpileString(`export type A = { foo?: string }`);
expect(result).toContain(`class A(TypedDict):\n foo: NotRequired[str]`);
});

it("transpiles optional values as NotRequired[Optional[T]] in strict mode", async () => {
it("transpiles optional values as NotRequired[T] in strict mode", async () => {
const result = await transpileString(
`export type A = { foo?: string }`,
{},
Expand All @@ -73,7 +73,7 @@ class A(TypedDict):
expect(result).toContain(`class A(TypedDict):\n foo: NotRequired[str]`);
});

it("transpiles optional values with non-null optionals as NotRequired[T]", async () => {
it("transpiles optional values with non-null optionals as NotRequired[Optional[T]]", async () => {
const result = await transpileString(`export type A = { foo?: string }`, {
nullableOptionals: true,
});
Expand All @@ -88,4 +88,39 @@ class A(TypedDict):
);
expect(result).toContain(`class A(TypedDict):\n foo: float\n bar: float`);
});

it("falls back to the functional syntax if keys are unsupported", async () => {
const result = await transpileString(`export type A = {
"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 */
export type A = {
/** this is foo.bar */
"foo.bar": string,
/** this is a/b */
"a/b": number,
"undocumented": string,
}
`);
expect(result).toContain(`A = TypedDict("A", {
"foo.bar": str,
"a/b": float,
"undocumented": str
})
"""
This is A
## Entries
"foo.bar": this is foo.bar
"a/b": this is a/b
"""`);
});
});
Loading