Skip to content

Commit 8757c84

Browse files
committed
minor change
1 parent 5709857 commit 8757c84

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

packages/core/src/python/get-config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def _deref_from_defs(ref: str, defs: dict | None):
4545
return copy.deepcopy(defs[key])
4646
else:
4747
return None
48-
48+
49+
# TODO : review
4950
def _get_ts_type(s: str) -> str:
5051
if s == "string":
5152
return "string"

packages/core/src/types/generate-python-types.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ const generateApiRequest = (
2727
const api_req_root_name = "_" + handlerName + "_ApiRequest_type_root";
2828
let generated = "";
2929

30+
if(requestBodySchema === 'Record<string, unknown>'){
31+
generated +=
32+
`${handlerName}_ApiRequest_type: TypeAlias = ApiRequest[Dict[str, Any]]` + "\n\n";
33+
34+
exportedSymbols.push(`${handlerName}_ApiRequest_type`);
35+
return generated;
36+
}
37+
3038
try {
3139

3240
generated +=
@@ -49,6 +57,15 @@ const generateApiResponse = (
4957
handlerName: string,
5058
exportedSymbols: string[]
5159
) => {
60+
61+
if(responseSchema === 'unknown'){
62+
const generated =
63+
`${handlerName}_ApiResponse_Type: TypeAlias = Any`+ "\n\n";
64+
65+
exportedSymbols.push(`${handlerName}_ApiResponse_Type`);
66+
return generated;
67+
}
68+
5269
try {
5370
let depth = 0,
5471
current = "",
@@ -107,6 +124,7 @@ const generateFlowContext = (
107124
name: string,
108125
exportedSymbols: string[]
109126
) => {
127+
110128
try {
111129
name = name + "_FlowContext";
112130

@@ -192,6 +210,14 @@ const generateInput = (
192210
) => {
193211
const rootName = safeRootName(name + "_Input_Type");
194212

213+
if(schema == 'never'){
214+
215+
const result =
216+
`${rootName}: TypeAlias = Never \n\n`
217+
exportedSymbols.push(rootName)
218+
return result
219+
}
220+
195221
try {
196222
const result = schema_to_typeddict(schema, rootName) + "\n";
197223
exportedSymbols.push(rootName);

packages/core/src/types/schema-to-typedDict.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
// Generates Python TypedDict code from a concise schema language.
22

3-
import { unknown } from "zod";
4-
53
// ---------------- AST ----------------
64

7-
export type TypeNode = Base | MappingType | ArrayT | Obj | UnknownT;
8-
95
export interface Base {
106
node: "Base";
11-
kind: string; // "string" | "number" | "int" | "boolean" | "emptyobj" (unused)
7+
kind: string;
8+
}
9+
10+
export interface UnknownT {
11+
node: "Unknown";
1212
}
1313

14+
export type TypeNode = Base | MappingType | ArrayT | Obj | UnknownT;
15+
1416
export interface MappingType {
1517
node: "MappingType";
16-
value: TypeNode; // Record<string, Value>
18+
value: TypeNode;
1719
}
1820

1921
export interface ArrayT {
@@ -32,10 +34,6 @@ export interface Obj {
3234
fields: ReadonlyArray<Field>;
3335
}
3436

35-
export interface UnknownT {
36-
node: "Unknown";
37-
}
38-
3937
// ---------------- Tokenizer ----------------
4038

4139
type TokKind =
@@ -88,6 +86,8 @@ export function tokenize(src: string): Tok[] {
8886
i = j;
8987
continue;
9088
}else{
89+
90+
// Scope for improvement
9191
// catch-all: consume until whitespace or a stop char
9292
const STOP = new Set(["{","}","[","]",":",";",",","<",">","?"]);
9393
let j = i;
@@ -146,6 +146,8 @@ class Parser {
146146
}
147147

148148
private parse_object(): Obj {
149+
150+
// Scope for change - What about Record<> Type ??
149151
this.want("LBRACE");
150152
const fields: Field[] = [];
151153
while (this.peek() && this.peek()!.kind !== "RBRACE") {
@@ -226,7 +228,7 @@ export function parse_schema(src: string): Obj {
226228
return root;
227229
}
228230

229-
// ---------------- Shape signatures & naming ----------------
231+
// ---------------- RENDERING THE TYPES ----------------
230232

231233
const PYTHON_KEYWORDS = new Set([
232234
"False","None","True","and","as","assert","async","await","break","class","continue","def","del","elif","else","except","finally","for","from","global","if","import","in","is","lambda","nonlocal","not","or","pass","raise","return","try","while","with","yield"
@@ -338,7 +340,9 @@ export function py_type(t: TypeNode, name_of: Map<string, string>): string {
338340
if (t.kind === "number") return "float";
339341
if (t.kind === "int") return "int";
340342
if (t.kind === "boolean") return "bool";
341-
if (t.kind === "emptyobj") return "Mapping[str, object]"; // (unused here)
343+
344+
// Check this
345+
if (t.kind === "emptyobj") return "Mapping[str, object]";
342346
return "Any";
343347

344348
// throw new Error(`Unknown base ${t.kind}`);
@@ -375,7 +379,7 @@ export function render_typeddicts(root: Obj, rootName: string = "Root"): string
375379

376380
// map signatures to names for type resolution
377381
const name_of = new Map<string, string>();
378-
for (const cls of classes) {
382+
for (const cls of classes) {
379383
const sig = type_signature({ node: "Obj", fields: cls.fields });
380384
name_of.set(JSON.stringify(sig), cls.name);
381385
}
@@ -385,13 +389,19 @@ export function render_typeddicts(root: Obj, rootName: string = "Root"): string
385389

386390
const field_type_str = (t: TypeNode): string => {
387391
const s = py_type(t, name_of);
388-
if (s.includes("Mapping[")) needed.add("Mapping");
392+
if (s.includes("Mapping["))
393+
needed.add("Mapping");
389394
// list[...] needs no extra import in Python 3.9+
390395
return s;
391396
};
392397

393398
const opt_wrap = (s: string, optional: boolean): string => {
394-
if (optional) { needed.add("NotRequired"); return `NotRequired[${s}]`; }
399+
400+
if (optional) {
401+
needed.add("NotRequired");
402+
return `NotRequired[${s}]`;
403+
}
404+
395405
return s;
396406
};
397407

0 commit comments

Comments
 (0)