tywrap 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/README.md +100 -32
  2. package/dist/cli.js +70 -5
  3. package/dist/cli.js.map +1 -1
  4. package/dist/config/index.d.ts +11 -7
  5. package/dist/config/index.d.ts.map +1 -1
  6. package/dist/config/index.js +62 -20
  7. package/dist/config/index.js.map +1 -1
  8. package/dist/core/analyzer.d.ts +1 -0
  9. package/dist/core/analyzer.d.ts.map +1 -1
  10. package/dist/core/analyzer.js +46 -12
  11. package/dist/core/analyzer.js.map +1 -1
  12. package/dist/core/annotation-parser.d.ts +8 -0
  13. package/dist/core/annotation-parser.d.ts.map +1 -0
  14. package/dist/core/annotation-parser.js +409 -0
  15. package/dist/core/annotation-parser.js.map +1 -0
  16. package/dist/core/discovery.d.ts +1 -0
  17. package/dist/core/discovery.d.ts.map +1 -1
  18. package/dist/core/discovery.js +9 -9
  19. package/dist/core/discovery.js.map +1 -1
  20. package/dist/core/generator.d.ts +9 -1
  21. package/dist/core/generator.d.ts.map +1 -1
  22. package/dist/core/generator.js +738 -104
  23. package/dist/core/generator.js.map +1 -1
  24. package/dist/core/mapper.d.ts +9 -7
  25. package/dist/core/mapper.d.ts.map +1 -1
  26. package/dist/core/mapper.js +129 -44
  27. package/dist/core/mapper.js.map +1 -1
  28. package/dist/dev.d.ts +57 -0
  29. package/dist/dev.d.ts.map +1 -0
  30. package/dist/dev.js +743 -0
  31. package/dist/dev.js.map +1 -0
  32. package/dist/index.d.ts +8 -7
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +4 -4
  35. package/dist/index.js.map +1 -1
  36. package/dist/runtime/bridge-core.d.ts +1 -0
  37. package/dist/runtime/bridge-core.d.ts.map +1 -1
  38. package/dist/runtime/bridge-core.js +52 -17
  39. package/dist/runtime/bridge-core.js.map +1 -1
  40. package/dist/runtime/bridge-protocol.d.ts +17 -0
  41. package/dist/runtime/bridge-protocol.d.ts.map +1 -1
  42. package/dist/runtime/bridge-protocol.js +97 -0
  43. package/dist/runtime/bridge-protocol.js.map +1 -1
  44. package/dist/runtime/errors.d.ts +10 -0
  45. package/dist/runtime/errors.d.ts.map +1 -1
  46. package/dist/runtime/errors.js +9 -0
  47. package/dist/runtime/errors.js.map +1 -1
  48. package/dist/runtime/http-io.d.ts.map +1 -1
  49. package/dist/runtime/http-io.js.map +1 -1
  50. package/dist/runtime/http.d.ts +1 -0
  51. package/dist/runtime/http.d.ts.map +1 -1
  52. package/dist/runtime/http.js +6 -0
  53. package/dist/runtime/http.js.map +1 -1
  54. package/dist/runtime/node.d.ts.map +1 -1
  55. package/dist/runtime/node.js +206 -32
  56. package/dist/runtime/node.js.map +1 -1
  57. package/dist/runtime/optimized-node.d.ts +6 -5
  58. package/dist/runtime/optimized-node.d.ts.map +1 -1
  59. package/dist/runtime/optimized-node.js +6 -5
  60. package/dist/runtime/optimized-node.js.map +1 -1
  61. package/dist/runtime/pooled-transport.d.ts +4 -0
  62. package/dist/runtime/pooled-transport.d.ts.map +1 -1
  63. package/dist/runtime/pooled-transport.js +2 -0
  64. package/dist/runtime/pooled-transport.js.map +1 -1
  65. package/dist/runtime/process-io.d.ts +5 -0
  66. package/dist/runtime/process-io.d.ts.map +1 -1
  67. package/dist/runtime/process-io.js +64 -30
  68. package/dist/runtime/process-io.js.map +1 -1
  69. package/dist/runtime/pyodide-io.d.ts +4 -5
  70. package/dist/runtime/pyodide-io.d.ts.map +1 -1
  71. package/dist/runtime/pyodide-io.js +99 -37
  72. package/dist/runtime/pyodide-io.js.map +1 -1
  73. package/dist/runtime/safe-codec.d.ts +25 -3
  74. package/dist/runtime/safe-codec.d.ts.map +1 -1
  75. package/dist/runtime/safe-codec.js +231 -57
  76. package/dist/runtime/safe-codec.js.map +1 -1
  77. package/dist/runtime/transport.d.ts +6 -1
  78. package/dist/runtime/transport.d.ts.map +1 -1
  79. package/dist/runtime/transport.js.map +1 -1
  80. package/dist/runtime/validators.d.ts +0 -11
  81. package/dist/runtime/validators.d.ts.map +1 -1
  82. package/dist/runtime/validators.js +13 -3
  83. package/dist/runtime/validators.js.map +1 -1
  84. package/dist/runtime/worker-pool.d.ts +17 -0
  85. package/dist/runtime/worker-pool.d.ts.map +1 -1
  86. package/dist/runtime/worker-pool.js +97 -22
  87. package/dist/runtime/worker-pool.js.map +1 -1
  88. package/dist/types/index.d.ts +51 -8
  89. package/dist/types/index.d.ts.map +1 -1
  90. package/dist/tywrap.d.ts +6 -0
  91. package/dist/tywrap.d.ts.map +1 -1
  92. package/dist/tywrap.js +237 -331
  93. package/dist/tywrap.js.map +1 -1
  94. package/dist/utils/cache.d.ts.map +1 -1
  95. package/dist/utils/cache.js +53 -5
  96. package/dist/utils/cache.js.map +1 -1
  97. package/dist/utils/codec.d.ts.map +1 -1
  98. package/dist/utils/codec.js +10 -16
  99. package/dist/utils/codec.js.map +1 -1
  100. package/dist/utils/ir-cache.d.ts +28 -0
  101. package/dist/utils/ir-cache.d.ts.map +1 -0
  102. package/dist/utils/ir-cache.js +29 -0
  103. package/dist/utils/ir-cache.js.map +1 -0
  104. package/dist/utils/parallel-processor.d.ts +1 -1
  105. package/dist/utils/parallel-processor.d.ts.map +1 -1
  106. package/dist/utils/parallel-processor.js +111 -40
  107. package/dist/utils/parallel-processor.js.map +1 -1
  108. package/dist/utils/runtime.d.ts +5 -1
  109. package/dist/utils/runtime.d.ts.map +1 -1
  110. package/dist/utils/runtime.js +112 -18
  111. package/dist/utils/runtime.js.map +1 -1
  112. package/package.json +47 -21
  113. package/runtime/python_bridge.py +78 -12
  114. package/src/cli.ts +85 -5
  115. package/src/config/index.ts +91 -32
  116. package/src/core/analyzer.ts +51 -15
  117. package/src/core/annotation-parser.ts +500 -0
  118. package/src/core/discovery.ts +26 -12
  119. package/src/core/generator.ts +956 -116
  120. package/src/core/mapper.ts +158 -51
  121. package/src/dev.ts +983 -0
  122. package/src/index.ts +35 -6
  123. package/src/runtime/bridge-core.ts +58 -18
  124. package/src/runtime/bridge-protocol.ts +164 -0
  125. package/src/runtime/errors.ts +14 -0
  126. package/src/runtime/http-io.ts +2 -1
  127. package/src/runtime/http.ts +7 -0
  128. package/src/runtime/node.ts +312 -46
  129. package/src/runtime/optimized-node.ts +9 -5
  130. package/src/runtime/pooled-transport.ts +12 -1
  131. package/src/runtime/process-io.ts +83 -55
  132. package/src/runtime/pyodide-io.ts +110 -44
  133. package/src/runtime/safe-codec.ts +290 -82
  134. package/src/runtime/transport.ts +6 -1
  135. package/src/runtime/validators.ts +14 -3
  136. package/src/runtime/worker-pool.ts +120 -24
  137. package/src/types/global.d.ts +11 -1
  138. package/src/types/index.ts +63 -8
  139. package/src/tywrap.ts +296 -361
  140. package/src/utils/cache.ts +59 -5
  141. package/src/utils/codec.ts +11 -15
  142. package/src/utils/ir-cache.ts +51 -0
  143. package/src/utils/parallel-processor.ts +119 -43
  144. package/src/utils/runtime.ts +129 -18
@@ -9,7 +9,13 @@ import type {
9
9
  UnionType as PyUnionType,
10
10
  OptionalType as PyOptionalType,
11
11
  GenericType as PyGenericType,
12
+ CallableType as PyCallableType,
12
13
  TypeVarType as PyTypeVarType,
14
+ ParamSpecType as PyParamSpecType,
15
+ ParamSpecArgsType as PyParamSpecArgsType,
16
+ ParamSpecKwargsType as PyParamSpecKwargsType,
17
+ TypeVarTupleType as PyTypeVarTupleType,
18
+ UnpackType as PyUnpackType,
13
19
  FinalType as PyFinalType,
14
20
  ClassVarType as PyClassVarType,
15
21
  TypescriptType,
@@ -62,6 +68,16 @@ export class TypeMapper {
62
68
  return this.mapPythonType(pythonType.base, context);
63
69
  case 'typevar':
64
70
  return this.mapTypeVarType(pythonType, context);
71
+ case 'paramspec':
72
+ return this.mapParamSpecType(pythonType);
73
+ case 'paramspec_args':
74
+ return this.mapParamSpecArgsType(pythonType);
75
+ case 'paramspec_kwargs':
76
+ return this.mapParamSpecKwargsType();
77
+ case 'typevartuple':
78
+ return this.mapTypeVarTupleType(pythonType);
79
+ case 'unpack':
80
+ return this.mapUnpackType(pythonType, context);
65
81
  case 'final':
66
82
  return this.mapFinalType(pythonType, context);
67
83
  case 'classvar':
@@ -103,6 +119,17 @@ export class TypeMapper {
103
119
 
104
120
  // tuple[T1, T2, ...] -> [T1, T2, ...] (exact arity)
105
121
  if (type.name === 'tuple') {
122
+ if (
123
+ type.itemTypes.length === 2 &&
124
+ type.itemTypes[1]?.kind === 'custom' &&
125
+ (type.itemTypes[1] as { kind: 'custom'; name: string }).name === '...'
126
+ ) {
127
+ return {
128
+ kind: 'array',
129
+ elementType: this.mapPythonType(type.itemTypes[0] ?? { kind: 'primitive', name: 'None' }),
130
+ } satisfies TSArrayType;
131
+ }
132
+
106
133
  const elementTypes: TypescriptType[] =
107
134
  type.itemTypes.length > 0
108
135
  ? type.itemTypes.map(t => this.mapPythonType(t))
@@ -167,11 +194,17 @@ export class TypeMapper {
167
194
  };
168
195
  }
169
196
 
170
- mapGenericType(type: PyGenericType, context: MappingContext = 'value'): TSGenericType {
197
+ mapGenericType(type: PyGenericType, context: MappingContext = 'value'): TypescriptType {
198
+ const normalized = this.normalizeCustomType({ name: type.name, module: type.module });
199
+ const typeArgs = type.typeArgs.map(t => this.mapPythonType(t, context));
200
+ const knownGenericType = this.mapKnownGenericType(normalized, typeArgs);
201
+ if (knownGenericType) {
202
+ return knownGenericType;
203
+ }
171
204
  return {
172
205
  kind: 'generic',
173
- name: type.name,
174
- typeArgs: type.typeArgs.map(t => this.mapPythonType(t, context)),
206
+ name: normalized.name,
207
+ typeArgs,
175
208
  };
176
209
  }
177
210
 
@@ -226,39 +259,9 @@ export class TypeMapper {
226
259
  };
227
260
  }
228
261
 
229
- // Async types
230
- if (name === 'Awaitable' || fullName === 'typing.Awaitable') {
231
- return {
232
- kind: 'generic',
233
- name: 'Promise',
234
- typeArgs: [{ kind: 'primitive', name: 'unknown' }],
235
- };
236
- }
237
- if (name === 'Coroutine' || fullName === 'typing.Coroutine') {
238
- return {
239
- kind: 'generic',
240
- name: 'Promise',
241
- typeArgs: [{ kind: 'primitive', name: 'unknown' }],
242
- };
243
- }
244
-
245
- // Collection types that should be generics
246
- if (name === 'Sequence' || fullName === 'typing.Sequence') {
247
- return {
248
- kind: 'generic',
249
- name: 'Array',
250
- typeArgs: [{ kind: 'primitive', name: 'unknown' }],
251
- };
252
- }
253
- if (name === 'Mapping' || fullName === 'typing.Mapping') {
254
- return {
255
- kind: 'object',
256
- properties: [],
257
- indexSignature: {
258
- keyType: { kind: 'primitive', name: 'string' },
259
- valueType: { kind: 'primitive', name: 'unknown' },
260
- },
261
- };
262
+ const knownGenericType = this.mapKnownGenericType(this.normalizeCustomType(type));
263
+ if (knownGenericType) {
264
+ return knownGenericType;
262
265
  }
263
266
 
264
267
  const presetType = this.mapPresetType(type);
@@ -266,16 +269,19 @@ export class TypeMapper {
266
269
  return presetType;
267
270
  }
268
271
 
272
+ // The generator does not synthesize TS generic parameters from Python typing
273
+ // helpers, so unsupported placeholders must degrade to unknown instead of
274
+ // leaking undeclared identifiers like T, P, or Ts into emitted code.
275
+ if (type.module === 'typing' || type.module === 'typing_extensions') {
276
+ return { kind: 'primitive', name: 'unknown' };
277
+ }
278
+
269
279
  // Forward references and user types
270
280
  const normalized = this.normalizeCustomType(type);
271
281
  return { kind: 'custom', name: normalized.name, module: normalized.module };
272
282
  }
273
283
 
274
- mapCallableType(type: {
275
- kind: 'callable';
276
- parameters: PythonType[];
277
- returnType: PythonType;
278
- }): TSFunctionType {
284
+ mapCallableType(type: PyCallableType): TSFunctionType {
279
285
  // Support Callable[[...], R] → (...args: unknown[]) => R
280
286
  const onlyEllipsis =
281
287
  type.parameters.length === 1 &&
@@ -285,21 +291,30 @@ export class TypeMapper {
285
291
  return {
286
292
  kind: 'function',
287
293
  isAsync: false,
288
- parameters: onlyEllipsis
294
+ parameters: type.parameterSpec
289
295
  ? ([
290
296
  {
291
297
  name: 'args',
292
- type: { kind: 'array', elementType: { kind: 'primitive', name: 'unknown' } },
298
+ type: this.mapParamSpecType(type.parameterSpec),
293
299
  optional: false,
294
300
  rest: true,
295
301
  },
296
302
  ] as const satisfies TSFunctionType['parameters'])
297
- : type.parameters.map((p, i) => ({
298
- name: `arg${i}`,
299
- type: this.mapPythonType(p, 'value'),
300
- optional: false,
301
- rest: false,
302
- })),
303
+ : onlyEllipsis
304
+ ? ([
305
+ {
306
+ name: 'args',
307
+ type: { kind: 'array', elementType: { kind: 'primitive', name: 'unknown' } },
308
+ optional: false,
309
+ rest: true,
310
+ },
311
+ ] as const satisfies TSFunctionType['parameters'])
312
+ : type.parameters.map((p, i) => ({
313
+ name: `arg${i}`,
314
+ type: this.mapPythonType(p, 'value'),
315
+ optional: false,
316
+ rest: false,
317
+ })),
303
318
  returnType: this.mapPythonType(type.returnType, 'return'),
304
319
  } satisfies TSFunctionType;
305
320
  }
@@ -312,8 +327,15 @@ export class TypeMapper {
312
327
  }
313
328
 
314
329
  mapTypeVarType(type: PyTypeVarType, _context: MappingContext = 'value'): TSCustomType {
315
- // TypeVar maps to a generic type parameter in TypeScript
316
- // Bounds and constraints are not directly expressible in TypeScript type system
330
+ // TypeVar maps to a generic type parameter in TypeScript.
331
+ return {
332
+ kind: 'custom',
333
+ name: type.name,
334
+ module: 'typing',
335
+ };
336
+ }
337
+
338
+ mapParamSpecType(type: PyParamSpecType): TSCustomType {
317
339
  return {
318
340
  kind: 'custom',
319
341
  name: type.name,
@@ -321,6 +343,32 @@ export class TypeMapper {
321
343
  };
322
344
  }
323
345
 
346
+ mapParamSpecArgsType(_type: PyParamSpecArgsType): TSArrayType {
347
+ return {
348
+ kind: 'array',
349
+ elementType: { kind: 'primitive', name: 'unknown' },
350
+ };
351
+ }
352
+
353
+ mapParamSpecKwargsType(): TSObjectType {
354
+ return {
355
+ kind: 'object',
356
+ properties: [],
357
+ indexSignature: {
358
+ keyType: { kind: 'primitive', name: 'string' },
359
+ valueType: { kind: 'primitive', name: 'unknown' },
360
+ },
361
+ };
362
+ }
363
+
364
+ mapTypeVarTupleType(_type: PyTypeVarTupleType): TSPrimitiveType {
365
+ return { kind: 'primitive', name: 'unknown' };
366
+ }
367
+
368
+ mapUnpackType(_type: PyUnpackType, _context: MappingContext = 'value'): TSPrimitiveType {
369
+ return { kind: 'primitive', name: 'unknown' };
370
+ }
371
+
324
372
  mapFinalType(type: PyFinalType, context: MappingContext = 'value'): TypescriptType {
325
373
  // Final[T] maps to T in TypeScript (no direct Final equivalent)
326
374
  // The Final qualifier is more of a static analysis hint
@@ -333,6 +381,58 @@ export class TypeMapper {
333
381
  return this.mapPythonType(type.type, context);
334
382
  }
335
383
 
384
+ private mapKnownGenericType(
385
+ type: { name: string; module?: string },
386
+ typeArgs: TypescriptType[] = []
387
+ ): TypescriptType | undefined {
388
+ const unknownType: TSPrimitiveType = { kind: 'primitive', name: 'unknown' };
389
+ const isKnownTypingModule =
390
+ type.module === undefined ||
391
+ type.module === 'typing' ||
392
+ type.module === 'typing_extensions' ||
393
+ type.module === 'collections.abc';
394
+
395
+ if (!isKnownTypingModule) {
396
+ return undefined;
397
+ }
398
+
399
+ if (type.name === 'Awaitable') {
400
+ return {
401
+ kind: 'generic',
402
+ name: 'Promise',
403
+ typeArgs: [typeArgs[0] ?? unknownType],
404
+ } satisfies TSGenericType;
405
+ }
406
+
407
+ if (type.name === 'Coroutine') {
408
+ return {
409
+ kind: 'generic',
410
+ name: 'Promise',
411
+ typeArgs: [typeArgs[typeArgs.length - 1] ?? unknownType],
412
+ } satisfies TSGenericType;
413
+ }
414
+
415
+ if (type.name === 'Sequence') {
416
+ return {
417
+ kind: 'array',
418
+ elementType: typeArgs[0] ?? unknownType,
419
+ } satisfies TSArrayType;
420
+ }
421
+
422
+ if (type.name === 'Mapping') {
423
+ return {
424
+ kind: 'object',
425
+ properties: [],
426
+ indexSignature: {
427
+ keyType: this.asIndexKeyType(typeArgs[0] ?? unknownType),
428
+ valueType: typeArgs[1] ?? unknownType,
429
+ },
430
+ } satisfies TSObjectType;
431
+ }
432
+
433
+ return undefined;
434
+ }
435
+
336
436
  private asIndexKeyType(key: TypescriptType): TSPrimitiveType {
337
437
  if (key.kind === 'primitive' && (key.name === 'string' || key.name === 'number')) {
338
438
  return key;
@@ -449,6 +549,13 @@ export class TypeMapper {
449
549
  }
450
550
  }
451
551
 
552
+ if (this.hasPreset('pydantic')) {
553
+ const allowModule = !moduleName || moduleName.startsWith('pydantic');
554
+ if (allowModule && name === 'BaseModel') {
555
+ return recordObject;
556
+ }
557
+ }
558
+
452
559
  if (this.hasPreset('scipy')) {
453
560
  const allowModule = !moduleName || moduleName.startsWith('scipy');
454
561
  if (allowModule) {