-
Notifications
You must be signed in to change notification settings - Fork 6
Add python support #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
87d2b34
Add python generation
just-be-dev a4cad52
Add python client
just-be-dev 8da9104
Add simple python example
just-be-dev 022c133
Add IPC python example
just-be-dev fbc045f
PR Feedback 1
just-be-dev 0848987
PR Feedback 2
just-be-dev 9dd5893
Add remaining examples
just-be-dev 1d17437
Move ruff to dev dependency
just-be-dev 0fbada4
Prefer Union[T | None] over Optional[T]
just-be-dev 82260fa
Make Python client scripts into a Python package (#119)
manzt 79f22a2
[Automated] Generate python clients
just-be-dev 2fe281c
Fixup last import bits to get the example working
just-be-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| /target | ||
| _test* | ||
| .DS_Store | ||
| __pycache__ | ||
| node_modules | ||
| .vscode/mise-tools |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| import { type Doc, isComplexType, type Node } from "./parser.ts"; | ||
| import { match, P } from "npm:ts-pattern"; | ||
| import { Writer } from "./gen-helpers.ts"; | ||
| import { assert } from "jsr:@std/assert"; | ||
|
|
||
| const header = (relativePath: string) => | ||
| `# DO NOT EDIT: This file is auto-generated by ${relativePath}\n` + | ||
| "from enum import Enum\n" + | ||
| "from typing import Any, Literal, Optional, Union\n" + | ||
| "import msgspec\n\n"; | ||
|
|
||
| export function generatePython( | ||
| doc: Doc, | ||
| name: string, | ||
| relativePath: string, | ||
| ): string { | ||
| return header(relativePath) + generateTypes(doc, name); | ||
| } | ||
|
|
||
| function generateTypes( | ||
| doc: Doc, | ||
| name: string, | ||
| ) { | ||
| const writer = new Writer(); | ||
|
|
||
| let definitions = ""; | ||
| const skipAssignments = ["object", "intersection", "enum", "union"]; | ||
| for (const [name, definition] of Object.entries(doc.definitions)) { | ||
| const definitionWriter = new Writer(); | ||
| const { w, wn } = definitionWriter.shorthand(); | ||
| if (!skipAssignments.includes(definition.type)) { | ||
| w(name, " = "); | ||
| } | ||
| generateNode(definition, definitionWriter); | ||
| if (definition.description) { | ||
| wn('"""'); | ||
| wn(`${definition.description}`); | ||
| wn('"""'); | ||
| } | ||
| definitions += definitionWriter.output(); | ||
| } | ||
|
|
||
| const { w, wn } = writer.shorthand(); | ||
|
|
||
| if (!skipAssignments.includes(doc.root.type)) { | ||
| w(name, " = "); | ||
| } | ||
| generateNode(doc.root, writer); | ||
| if (doc.description && doc.root.type !== "object") { | ||
| wn('"""'); | ||
| wn(`${doc.description}`); | ||
| wn('"""'); | ||
| } | ||
|
|
||
| return definitions + writer.output(); | ||
| } | ||
|
|
||
| function sortByRequired<T extends { required: boolean }>( | ||
| properties: T[], | ||
| ): T[] { | ||
| return [...properties].sort((a, b) => { | ||
| if (a.required === b.required) return 0; | ||
| return a.required ? -1 : 1; | ||
| }); | ||
| } | ||
|
|
||
| function generateNode(node: Node, writer: Writer) { | ||
| const { w, wn } = writer.shorthand(); | ||
| using context = new Context(node); | ||
| match(node) | ||
| .with({ type: "reference" }, ({ name }) => w(name)) | ||
| .with({ type: "int" }, () => w("int")) | ||
| .with({ type: "float" }, () => w("float")) | ||
| .with({ type: "boolean" }, () => w("bool")) | ||
| .with({ type: "string" }, () => w("str")) | ||
| .with({ type: "literal" }, (node) => w(`Literal["${node.value}"]`)) | ||
| .with( | ||
| { type: "record" }, | ||
| (node) => w(`dict[str, ${mapPythonType(node.valueType)}]`), | ||
| ) | ||
| .with({ type: "enum" }, (node) => { | ||
| wn(`class ${node.name}(str, Enum):`); | ||
| for (const value of node.members) { | ||
| wn(` ${value} = "${value}"`); | ||
| } | ||
| wn(""); | ||
| }) | ||
| .with({ type: "union" }, (node) => { | ||
| const depWriter = new Writer(); | ||
| const classes = node.members.map((m) => { | ||
| if (isComplexType(m)) { | ||
| generateNode(m, depWriter); | ||
| } | ||
| if (m.name) return m.name; | ||
| const ident = m.type === "object" | ||
| ? m.properties?.find((p) => p.required)?.key ?? "" | ||
| : ""; | ||
| return `${node.name}${cap(ident)}`; | ||
| }); | ||
| writer.append(depWriter.output()); | ||
| wn(`${node.name} = Union[${classes.join(", ")}]`); | ||
| }) | ||
| .with({ type: "object" }, (node) => { | ||
| match(context.parent) | ||
| .with({ type: "union" }, (parent) => { | ||
| const name = context.closestName(); | ||
| const ident = node.properties.find((p) => p.required)?.key ?? ""; | ||
| wn( | ||
| `class ${name}${ | ||
| cap(ident) | ||
| }(msgspec.Struct, kw_only=True, omit_defaults=True):`, | ||
| ); | ||
| }) | ||
| .with(P.nullish, () => { | ||
| wn(`class ${node.name}(msgspec.Struct, omit_defaults=True):`); | ||
| }) | ||
| .otherwise(() => { | ||
| wn( | ||
| `class ${node.name}(msgspec.Struct, kw_only=True, omit_defaults=True):`, | ||
| ); | ||
| }); | ||
| if (node.description) { | ||
| wn(` """`); | ||
| wn(` ${node.description}`); | ||
| wn(` """`); | ||
| } | ||
|
|
||
| const sortedProperties = sortByRequired(node.properties); | ||
|
|
||
| for (const { key, required, description, value } of sortedProperties) { | ||
| w(` ${key}: `); | ||
| if (!required) w("Union["); | ||
| generateNode(value, writer); | ||
| if (!required) w(", None] = None"); | ||
| wn(""); | ||
| if (description) { | ||
| wn(` """${description}"""`); | ||
| } | ||
| } | ||
| wn(""); | ||
| }) | ||
| .with({ type: "descriminated-union" }, (node) => { | ||
| const depWriter = new Writer(); | ||
| const { w: d, wn: dn } = depWriter.shorthand(); | ||
| const classes: string[] = []; | ||
| w("Union["); | ||
| for (const [name, properties] of Object.entries(node.members)) { | ||
| for (const { value } of properties) { | ||
| if (isComplexType(value)) { | ||
| generateNode(value, depWriter); | ||
| } | ||
| } | ||
| const className = `${cap(name)}${cap(node.name!)}`; | ||
| classes.push(className); | ||
| dn( | ||
| `class ${className}(msgspec.Struct, tag_field="${node.descriminator}", tag="${name}"):`, | ||
| ); | ||
| if (properties.length === 0) { | ||
| dn(" pass"); | ||
| } | ||
|
|
||
| const sortedProperties = sortByRequired(properties); | ||
|
|
||
| for (const { key, required, description, value } of sortedProperties) { | ||
| d(` ${key}: `); | ||
| if (!required) d("Union["); | ||
| !isComplexType(value) | ||
| ? generateNode(value, depWriter) | ||
| : d(value.name ?? value.type); | ||
| if (!required) d(", None] = None"); | ||
| dn(""); | ||
| if (description) { | ||
| dn(` """${description}"""`); | ||
| } | ||
| } | ||
| dn(""); | ||
| } | ||
| w(classes.join(", ")); | ||
| writer.prepend(depWriter.output()); | ||
| wn("]"); | ||
| }) | ||
| .with({ type: "intersection" }, (node) => { | ||
| assert( | ||
| node.members.length === 2, | ||
| "Intersection must have exactly 2 members", | ||
| ); | ||
| assert( | ||
| node.members[0]?.type === "object", | ||
| "First member of intersection must be an object", | ||
| ); | ||
| for (const member of node.members) { | ||
| generateNode(member, writer); | ||
| } | ||
| }) | ||
| .with({ type: "unknown" }, () => { | ||
| w("Any"); | ||
| }) | ||
| .exhaustive(); | ||
| } | ||
|
|
||
| class Context { | ||
| private static stack: Node[] = []; | ||
| constructor(public readonly currentNode: Node) { | ||
| Context.stack.push(this.currentNode); | ||
| } | ||
|
|
||
| get parent(): Node | undefined { | ||
| return Context.stack.at(-2); | ||
| } | ||
|
|
||
| /** | ||
| * When `n` is 1, this returns the parent. | ||
| * When `n` is 2, this returns the grandparent. | ||
| * etc. | ||
| */ | ||
| getNthParent(n: number): Node | undefined { | ||
| return Context.stack.at(-(n + 1)); | ||
| } | ||
|
|
||
| closestName(): string | undefined { | ||
| for (const node of [...Context.stack].reverse()) { | ||
| // @ts-expect-error - We're looking for names on roots or declarations, this should be fine. | ||
| const name = node.name || node.title; | ||
| if (name) { | ||
| return name; | ||
| } | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| [Symbol.dispose]() { | ||
| Context.stack = Context.stack.filter((n) => n !== this.currentNode); | ||
| } | ||
| } | ||
|
|
||
| function cap(str: string): string { | ||
| if (!str) return ""; | ||
| return str.charAt(0).toUpperCase() + str.slice(1); | ||
| } | ||
|
|
||
| function mapPythonType(type: string): string { | ||
| return match(type) | ||
| .with("string", () => "str") | ||
| .with("number", () => "float") | ||
| .with("integer", () => "int") | ||
| .with("boolean", () => "bool") | ||
| .otherwise(() => "Any"); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.