Skip to content

Commit b2de023

Browse files
authored
Merge pull request #3607 from hey-api/feat/py-dsl-method
feat: add class method node
2 parents a92a301 + 4f42c50 commit b2de023

6 files changed

Lines changed: 38 additions & 20 deletions

File tree

packages/openapi-python-tests/sdks/__snapshots__/opencode/default/sdk_gen.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def dispose(self):
3535

3636

3737
class Project(Client):
38-
def list_(self):
38+
def list(self):
3939
"""
4040
List all projects
4141
@@ -64,7 +64,7 @@ def update(self):
6464

6565

6666
class Pty(Client):
67-
def list_(self):
67+
def list(self):
6868
"""
6969
List PTY sessions
7070
@@ -158,7 +158,7 @@ def ids(self):
158158

159159
return self.client.get("/experimental/tool/ids")
160160

161-
def list_(self):
161+
def list(self):
162162
"""
163163
List tools
164164
@@ -202,7 +202,7 @@ def get(self):
202202

203203

204204
class Session(Client):
205-
def list_(self):
205+
def list(self):
206206
"""
207207
List sessions
208208
@@ -443,7 +443,7 @@ def reply(self):
443443

444444
return self.client.post("/permission/{requestID}/reply")
445445

446-
def list_(self):
446+
def list(self):
447447
"""
448448
List pending permissions
449449
@@ -454,7 +454,7 @@ def list_(self):
454454

455455

456456
class Command(Client):
457-
def list_(self):
457+
def list(self):
458458
"""
459459
List commands
460460
@@ -485,7 +485,7 @@ def callback(self):
485485

486486

487487
class Provider(Client):
488-
def list_(self):
488+
def list(self):
489489
"""
490490
List providers
491491
@@ -538,7 +538,7 @@ def symbols(self):
538538

539539

540540
class File(Client):
541-
def list_(self):
541+
def list(self):
542542
"""
543543
List files
544544
@@ -681,7 +681,7 @@ def status(self):
681681

682682

683683
class Control(Client):
684-
def next_(self):
684+
def next(self):
685685
"""
686686
Get next TUI request
687687
@@ -797,7 +797,7 @@ def control(self) -> Control:
797797

798798

799799
class Auth_2(Client):
800-
def set_(self):
800+
def set(self):
801801
"""
802802
Set auth credentials
803803

packages/openapi-python/src/plugins/@hey-api/sdk/v1/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function childToNode(
7272
const cachedProp = plugin.external('functools.cached_property');
7373

7474
return [
75-
$.func(memberName)
75+
$.method(memberName)
7676
.decorator(cachedProp)
7777
.param('self')
7878
.returns(refChild)
@@ -196,7 +196,7 @@ export function toNode(
196196
} else {
197197
if (index > 0 || node.hasBody) node.newline();
198198
const method = implementFn({
199-
node: $.func(createFnSymbol(plugin, item), (m) =>
199+
node: $.method(createFnSymbol(plugin, item), (m) =>
200200
attachComment({
201201
node: m,
202202
operation,

packages/openapi-python/src/py-dsl/decl/func.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AnalysisContext, NodeName } from '@hey-api/codegen-core';
1+
import type { AnalysisContext, NodeName, NodeNameSanitizer } from '@hey-api/codegen-core';
22
import { isSymbol } from '@hey-api/codegen-core';
33

44
import { py } from '../../ts-python';
@@ -22,10 +22,15 @@ const Mixed = AsyncMixin(
2222

2323
export class FuncPyDsl extends Mixed {
2424
readonly '~dsl' = 'FuncPyDsl';
25-
override readonly nameSanitizer = safeRuntimeName;
25+
override readonly nameSanitizer: NodeNameSanitizer;
2626

27-
constructor(name: NodeName, fn?: (f: FuncPyDsl) => void) {
27+
constructor(
28+
name: NodeName,
29+
fn?: (f: FuncPyDsl) => void,
30+
options?: { nameSanitizer?: NodeNameSanitizer },
31+
) {
2832
super();
33+
this.nameSanitizer = options?.nameSanitizer ?? safeRuntimeName;
2934
this.name.set(name);
3035
if (isSymbol(name)) {
3136
name.setKind('function');

packages/openapi-python/src/py-dsl/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import { WithPyDsl } from './stmt/with';
7171
// import { TypeTemplatePyDsl } from './type/template';
7272
// import { TypeTuplePyDsl } from './type/tuple';
7373
import { LazyPyDsl } from './utils/lazy';
74+
import { safeKeywordName } from './utils/name';
7475

7576
const pyDsl = {
7677
/** Creates an array literal expression (e.g. `[1, 2, 3]`). */
@@ -147,12 +148,12 @@ const pyDsl = {
147148
/** Creates an import statement. */
148149
import: (...args: ConstructorParameters<typeof ImportPyDsl>) => new ImportPyDsl(...args),
149150

150-
/** Creates a keyword argument expression (e.g. `name=value`). */
151-
kwarg: (...args: ConstructorParameters<typeof KwargPyDsl>) => new KwargPyDsl(...args),
152-
153151
/** Creates an initialization block or statement. */
154152
// init: (...args: ConstructorParameters<typeof InitTsDsl>) => new InitTsDsl(...args),
155153

154+
/** Creates a keyword argument expression (e.g. `name=value`). */
155+
kwarg: (...args: ConstructorParameters<typeof KwargPyDsl>) => new KwargPyDsl(...args),
156+
156157
/** Creates a lazy, context-aware node with deferred evaluation. */
157158
lazy: <T extends py.Node>(...args: ConstructorParameters<typeof LazyPyDsl<T>>) =>
158159
new LazyPyDsl<T>(...args),
@@ -166,8 +167,12 @@ const pyDsl = {
166167
/** Creates an enum member declaration. */
167168
// member: (...args: ConstructorParameters<typeof EnumMemberTsDsl>) => new EnumMemberTsDsl(...args),
168169

169-
/** Creates a method declaration inside a class or object. */
170-
// method: (...args: ConstructorParameters<typeof MethodTsDsl>) => new MethodTsDsl(...args),
170+
/** Creates a class method declaration. */
171+
method: ((name: NodeName, fn?: (f: FuncPyDsl) => void) =>
172+
new FuncPyDsl(name, fn, { nameSanitizer: safeKeywordName })) as {
173+
(name: NodeName): FuncPyDsl;
174+
(name: NodeName, fn: (f: FuncPyDsl) => void): FuncPyDsl;
175+
},
171176

172177
/** Creates a negation expression (`-x`). */
173178
// neg: (...args: ConstructorParameters<typeof PrefixTsDsl>) => new PrefixTsDsl(...args).neg(),

packages/openapi-python/src/py-dsl/utils/name.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ const safeName = (name: string, reserved: ReservedList): string => {
5151
};
5252

5353
export const safeRuntimeName = (name: string): string => safeName(name, reserved.runtime);
54+
55+
export const safeKeywordName = (name: string): string => safeName(name, reserved.keywords);

packages/openapi-python/src/py-dsl/utils/reserved.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ export class ReservedList {
2929
}
3030

3131
const runtimeReserved = new ReservedList([...keywords.pythonKeywords, ...keywords.pythonBuiltins]);
32+
const keywordReserved = new ReservedList([...keywords.pythonKeywords]);
3233

3334
/**
3435
* Reserved names for identifiers. These names will not be used
3536
* for variables, functions, classes, or other identifiers in generated code.
3637
*/
3738
export const reserved = {
39+
/**
40+
* Reserved names for Python language keywords. These names are syntactically
41+
* invalid as identifiers in any scope.
42+
*/
43+
keywords: keywordReserved,
3844
/**
3945
* Reserved names for runtime identifiers. These names will not be used
4046
* for variables, functions, classes, or other runtime identifiers in

0 commit comments

Comments
 (0)