fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: use symbol refs

Lubos 675f28eb 593fafe6

+79 -60
+5
dev/playground.py
··· 1 + # import httpx 1 2 from gen.python import OpenCode 2 3 4 + # def log_request(request): 5 + # print(request.method, request.url, request.headers, request.content) 6 + 7 + # client = httpx.Client(event_hooks={"request": [log_request]}) 3 8 4 9 def run(): 5 10 client = OpenCode()
+1
packages/codegen-core/src/planner/planner.ts
··· 419 419 while (true) { 420 420 const kinds = [...(localNames.get(finalName) ?? [])]; 421 421 422 + // TODO: adjust canShareName for language 422 423 const ok = kinds.every((kind) => canShareName(symbol.kind, kind)); 423 424 if (ok) break; 424 425
+1 -1
packages/openapi-python/src/createClient.ts
··· 121 121 }, 122 122 nameConflictResolvers: config.output.nameConflictResolver 123 123 ? { 124 - typescript: config.output.nameConflictResolver, 124 + python: config.output.nameConflictResolver, 125 125 } 126 126 : undefined, 127 127 renderers: [
+3 -1
packages/openapi-python/src/plugins/@hey-api/sdk/shared/operation.ts
··· 37 37 38 38 if (schema.type === 'array') { 39 39 const itemsSchema = schema.items?.[0]; 40 - const itemType = itemsSchema ? schemaToPythonType(itemsSchema, plugin) : 'Any'; 40 + const itemType = itemsSchema 41 + ? schemaToPythonType(itemsSchema, plugin) 42 + : plugin.external('typing.Any'); 41 43 return $('list').slice(itemType); 42 44 } 43 45
+4 -6
packages/openapi-python/src/plugins/@hey-api/sdk/v1/node.ts
··· 133 133 fieldsList.element(fieldDict); 134 134 } 135 135 136 - const kwargs: Array<ReturnType<typeof $.kwarg>> = []; 137 - for (const name of paramNames) { 138 - kwargs.push($.kwarg(name, name)); 139 - } 140 - 141 136 return ( 142 137 node 143 138 .params(...opParameters.parameters) 144 139 // TODO: extract operation statements into a separate function 145 140 .do( 146 141 $.var('params').assign( 147 - $(plugin.external('client.build_client_params')).call(fieldsList, ...kwargs), 142 + $(plugin.external('client.build_client_params')).call( 143 + fieldsList, 144 + ...paramNames.map((name) => $.kwarg(name, name)), 145 + ), 148 146 ), 149 147 ) 150 148 .do(
+4 -4
packages/openapi-python/src/py-dsl/decl/class.ts
··· 1 - import type { AnalysisContext, NodeName } from '@hey-api/codegen-core'; 2 - import { isSymbol } from '@hey-api/codegen-core'; 1 + import type { AnalysisContext, NodeName, Ref } from '@hey-api/codegen-core'; 2 + import { isSymbol, ref } from '@hey-api/codegen-core'; 3 3 4 4 import { py } from '../../ts-python'; 5 5 import { type MaybePyDsl, PyDsl } from '../base'; ··· 18 18 readonly '~dsl' = 'ClassPyDsl'; 19 19 override readonly nameSanitizer = safeRuntimeName; 20 20 21 - protected baseClasses: Array<NodeName> = []; 21 + protected baseClasses: Array<Ref<NodeName>> = []; 22 22 protected body: Body = []; 23 23 24 24 constructor(name: NodeName) { ··· 63 63 64 64 /** Records base classes to extend from. */ 65 65 extends(...baseClass: ReadonlyArray<NodeName>): this { 66 - this.baseClasses.push(...baseClass); 66 + this.baseClasses.push(...baseClass.map((item) => ref(item))); 67 67 return this; 68 68 } 69 69
+6 -5
packages/openapi-python/src/py-dsl/decl/param.ts
··· 1 - import type { AnalysisContext, NodeName } from '@hey-api/codegen-core'; 1 + import type { AnalysisContext, NodeName, Ref } from '@hey-api/codegen-core'; 2 + import { ref } from '@hey-api/codegen-core'; 2 3 3 4 import { py } from '../../ts-python'; 4 5 import { PyDsl } from '../base'; ··· 13 14 export class ParamPyDsl extends PyDsl<py.FunctionParameter> { 14 15 readonly '~dsl' = 'ParamPyDsl'; 15 16 16 - protected _defaultValue?: ParamDefaultValue; 17 - protected _type?: ParamType; 17 + protected _defaultValue?: Ref<ParamDefaultValue>; 18 + protected _type?: Ref<ParamType>; 18 19 19 20 constructor(name: ParamName, fn?: ParamFn) { 20 21 super(); ··· 35 36 36 37 /** Sets the parameter default value. */ 37 38 default(value: ParamDefaultValue): this { 38 - this._defaultValue = value; 39 + this._defaultValue = ref(value); 39 40 return this; 40 41 } 41 42 ··· 45 46 46 47 /** Sets the parameter type. */ 47 48 type(type: ParamType): this { 48 - this._type = type; 49 + this._type = ref(type); 49 50 return this; 50 51 } 51 52
+7 -6
packages/openapi-python/src/py-dsl/expr/binary.ts
··· 1 - import type { AnalysisContext } from '@hey-api/codegen-core'; 1 + import type { AnalysisContext, Ref } from '@hey-api/codegen-core'; 2 + import { ref } from '@hey-api/codegen-core'; 2 3 3 4 import { py } from '../../ts-python'; 4 5 import type { MaybePyDsl } from '../base'; ··· 30 31 export class BinaryPyDsl extends Mixed { 31 32 readonly '~dsl' = 'BinaryPyDsl'; 32 33 33 - protected _left?: MaybePyDsl<py.Expression>; 34 + protected _left?: Ref<MaybePyDsl<py.Expression>>; 34 35 protected _op?: PyBinaryOperator; 35 - protected _right?: MaybePyDsl<py.Expression>; 36 + protected _right?: Ref<MaybePyDsl<py.Expression>>; 36 37 37 38 constructor( 38 39 left: MaybePyDsl<py.Expression>, ··· 40 41 right: MaybePyDsl<py.Expression>, 41 42 ) { 42 43 super(); 43 - this._left = left; 44 + this._left = ref(left); 44 45 this._op = op; 45 - this._right = right; 46 + this._right = ref(right); 46 47 } 47 48 48 49 override analyze(ctx: AnalysisContext): void { ··· 161 162 } 162 163 163 164 private opAndExpr(op: PyBinaryOperator, right: MaybePyDsl<py.Expression>): this { 164 - this._right = right; 165 + this._right = ref(right); 165 166 this._op = op; 166 167 return this; 167 168 }
+6 -5
packages/openapi-python/src/py-dsl/expr/subscript.ts
··· 1 - import type { AnalysisContext, NodeName } from '@hey-api/codegen-core'; 1 + import type { AnalysisContext, NodeName, Ref } from '@hey-api/codegen-core'; 2 + import { ref } from '@hey-api/codegen-core'; 2 3 3 4 import { py } from '../../ts-python'; 4 5 import { PyDsl } from '../base'; ··· 16 17 export class SubscriptPyDsl extends Mixed { 17 18 readonly '~dsl' = 'SubscriptPyDsl'; 18 19 19 - protected _slices: Array<SubscriptExpr>; 20 - protected _value: SubscriptExpr; 20 + protected _slices: Array<Ref<SubscriptExpr>>; 21 + protected _value: Ref<SubscriptExpr>; 21 22 22 23 constructor(value: SubscriptExpr, ...slices: Array<SubscriptExpr>) { 23 24 super(); 24 - this._slices = slices; 25 - this._value = value; 25 + this._slices = slices.map((slice) => ref(slice)); 26 + this._value = ref(value); 26 27 } 27 28 28 29 override analyze(ctx: AnalysisContext): void {
+6 -5
packages/openapi-python/src/py-dsl/expr/tuple.ts
··· 1 - import type { AnalysisContext, NodeName } from '@hey-api/codegen-core'; 1 + import type { AnalysisContext, NodeName, Ref } from '@hey-api/codegen-core'; 2 + import { ref } from '@hey-api/codegen-core'; 2 3 3 4 import { py } from '../../ts-python'; 4 5 import type { MaybePyDsl } from '../base'; ··· 12 13 export class TuplePyDsl extends Mixed { 13 14 readonly '~dsl' = 'TuplePyDsl'; 14 15 15 - protected _elements: Array<TupleElement> = []; 16 + protected _elements: Array<Ref<TupleElement>> = []; 16 17 17 18 constructor(...elements: Array<TupleElement>) { 18 19 super(); 19 - this._elements = elements; 20 + this._elements = elements.map((element) => ref(element)); 20 21 } 21 22 22 23 override analyze(ctx: AnalysisContext): void { ··· 27 28 } 28 29 29 30 element(expr: TupleElement): this { 30 - this._elements.push(expr); 31 + this._elements.push(ref(expr)); 31 32 return this; 32 33 } 33 34 34 35 elements(...exprs: ReadonlyArray<TupleElement>): this { 35 - this._elements.push(...exprs); 36 + this._elements.push(...exprs.map((expr) => ref(expr))); 36 37 return this; 37 38 } 38 39
+18 -10
packages/openapi-python/src/py-dsl/mixins/decorator.ts
··· 1 - import type { AnalysisContext, NodeName } from '@hey-api/codegen-core'; 1 + import type { AnalysisContext, NodeName, Ref } from '@hey-api/codegen-core'; 2 + import { ref } from '@hey-api/codegen-core'; 2 3 3 4 import { py } from '../../ts-python'; 4 5 import type { MaybePyDsl } from '../base'; 5 6 import type { BaseCtor, MixinCtor } from './types'; 6 7 7 - type DecoratorInput = { 8 - args: ReadonlyArray<MaybePyDsl<py.Expression>>; 9 - name: NodeName | MaybePyDsl<py.Expression>; 10 - }; 8 + type DecoratorArg = MaybePyDsl<py.Expression>; 9 + type DecoratorName = NodeName | MaybePyDsl<py.Expression>; 11 10 12 11 export interface DecoratorMethods extends Node { 13 12 $decorators(): ReadonlyArray<py.Expression>; 14 - decorator(name: DecoratorInput['name'], ...args: DecoratorInput['args']): this; 13 + decorator(name: DecoratorName, ...args: ReadonlyArray<DecoratorArg>): this; 15 14 } 16 15 17 16 export function DecoratorMixin<T extends py.Node, TBase extends BaseCtor<T>>(Base: TBase) { 18 17 abstract class Decorator extends Base { 19 - protected _decorators: Array<DecoratorInput> = []; 18 + protected _decorators: Array<{ 19 + args: ReadonlyArray<Ref<DecoratorArg>>; 20 + name: Ref<DecoratorName>; 21 + }> = []; 20 22 21 23 override analyze(ctx: AnalysisContext): void { 22 24 super.analyze(ctx); ··· 28 30 } 29 31 } 30 32 31 - protected decorator(name: DecoratorInput['name'], ...args: DecoratorInput['args']): this { 32 - this._decorators.push({ args, name }); 33 + protected decorator(name: DecoratorName, ...args: ReadonlyArray<DecoratorArg>): this { 34 + this._decorators.push({ 35 + args: args.map((arg) => ref(arg)), 36 + name: ref(name), 37 + }); 33 38 return this; 34 39 } 35 40 36 41 protected $decorators(): ReadonlyArray<py.Expression> { 37 42 return this._decorators.map((decorator) => 38 43 decorator.args.length > 0 39 - ? py.factory.createCallExpression(this.$node(decorator.name), this.$node(decorator.args)) 44 + ? py.factory.createCallExpression( 45 + this.$node(decorator.name), 46 + decorator.args.map((arg) => this.$node(arg)), 47 + ) 40 48 : this.$node(decorator.name), 41 49 ); 42 50 }
+4 -3
packages/openapi-python/src/py-dsl/mixins/returns.ts
··· 1 - import type { AnalysisContext, Node, NodeName } from '@hey-api/codegen-core'; 1 + import type { AnalysisContext, Node, NodeName, Ref } from '@hey-api/codegen-core'; 2 + import { ref } from '@hey-api/codegen-core'; 2 3 3 4 import type { py } from '../../ts-python'; 4 5 import type { BaseCtor, MixinCtor } from './types'; ··· 10 11 11 12 export function ReturnsMixin<T extends py.Node, TBase extends BaseCtor<T>>(Base: TBase) { 12 13 abstract class Returns extends Base { 13 - protected _returns?: NodeName | py.Expression; 14 + protected _returns?: Ref<NodeName | py.Expression>; 14 15 15 16 override analyze(ctx: AnalysisContext): void { 16 17 super.analyze(ctx); ··· 18 19 } 19 20 20 21 returns(type: NodeName | py.Expression): this { 21 - this._returns = type; 22 + this._returns = ref(type); 22 23 return this; 23 24 } 24 25
+4 -3
packages/openapi-python/src/py-dsl/mixins/value.ts
··· 1 - import type { AnalysisContext, Node, NodeName } from '@hey-api/codegen-core'; 1 + import type { AnalysisContext, Node, NodeName, Ref } from '@hey-api/codegen-core'; 2 + import { ref } from '@hey-api/codegen-core'; 2 3 3 4 import type { py } from '../../ts-python'; 4 5 import type { PyDsl } from '../base'; ··· 14 15 15 16 export function ValueMixin<T extends py.Node, TBase extends BaseCtor<T>>(Base: TBase) { 16 17 abstract class Value extends Base { 17 - protected value?: ValueExpr; 18 + protected value?: Ref<ValueExpr>; 18 19 19 20 override analyze(ctx: AnalysisContext): void { 20 21 super.analyze(ctx); ··· 22 23 } 23 24 24 25 protected assign(expr: ValueExpr): this { 25 - this.value = expr; 26 + this.value = ref(expr); 26 27 return this; 27 28 } 28 29
+6 -7
packages/openapi-python/src/py-dsl/stmt/try.ts
··· 1 - import type { AnalysisContext, NodeName } from '@hey-api/codegen-core'; 1 + import type { AnalysisContext, NodeName, Ref } from '@hey-api/codegen-core'; 2 + import { ref } from '@hey-api/codegen-core'; 2 3 import type { MaybeArray } from '@hey-api/types'; 3 4 4 5 import { py } from '../../ts-python'; ··· 13 14 14 15 interface ExceptEntry { 15 16 body: Array<DoExpr>; 16 - name?: NodeName; 17 + name?: Ref<NodeName>; 17 18 types: Array<ExceptType>; 18 19 } 19 20 ··· 107 108 const typeArr = Array.isArray(types) ? types : [types]; 108 109 const key = exceptKey(typeArr); 109 110 110 - let name: NodeName | undefined; 111 + let name: Ref<NodeName> | undefined; 111 112 let bodyItems: Array<DoExpr>; 112 113 113 114 // Disambiguate: if the second arg is a plain string that looks like 114 115 // an identifier (no dots, no spaces, not a DSL node) treat it as 115 116 // the `as` name. Otherwise it's the first body expression. 116 117 if (nameOrBody !== undefined && this._isNodeName(nameOrBody)) { 117 - name = nameOrBody as NodeName; 118 + name = ref(nameOrBody as NodeName); 118 119 bodyItems = body; 119 120 } else if (nameOrBody !== undefined) { 120 121 bodyItems = [nameOrBody as DoExpr, ...body]; ··· 176 177 } 177 178 178 179 const exceptionName = entry.name 179 - ? py.factory.createIdentifier( 180 - this.$name({ current: entry.name } as any) || String(entry.name), 181 - ) 180 + ? py.factory.createIdentifier(this.$name(entry.name) || String(entry.name['~ref'])) 182 181 : undefined; 183 182 184 183 return py.factory.createExceptClause([...bodyStatements], exceptionType, exceptionName);
+4 -4
packages/openapi-python/src/py-dsl/stmt/var.ts
··· 1 - import type { AnalysisContext, NodeName } from '@hey-api/codegen-core'; 2 - import { isSymbol } from '@hey-api/codegen-core'; 1 + import type { AnalysisContext, NodeName, Ref } from '@hey-api/codegen-core'; 2 + import { isSymbol, ref } from '@hey-api/codegen-core'; 3 3 4 4 import { py } from '../../ts-python'; 5 5 import { PyDsl } from '../base'; ··· 14 14 readonly '~dsl' = 'VarPyDsl'; 15 15 override readonly nameSanitizer = safeRuntimeName; 16 16 17 - protected _type?: VarType; 17 + protected _type?: Ref<VarType>; 18 18 19 19 constructor(name?: NodeName) { 20 20 super(); ··· 37 37 38 38 /** Sets the type annotation for the variable. */ 39 39 type(type: VarType): this { 40 - this._type = type; 40 + this._type = ref(type); 41 41 return this; 42 42 } 43 43