tywrap 0.4.0 → 0.5.1

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 (89) hide show
  1. package/README.md +1 -0
  2. package/dist/index.d.ts +3 -3
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +6 -6
  5. package/dist/index.js.map +1 -1
  6. package/dist/runtime/base.d.ts +7 -7
  7. package/dist/runtime/base.js +8 -8
  8. package/dist/runtime/bounded-context.d.ts +15 -31
  9. package/dist/runtime/bounded-context.d.ts.map +1 -1
  10. package/dist/runtime/bounded-context.js +16 -15
  11. package/dist/runtime/bounded-context.js.map +1 -1
  12. package/dist/runtime/http.d.ts +24 -5
  13. package/dist/runtime/http.d.ts.map +1 -1
  14. package/dist/runtime/http.js +57 -12
  15. package/dist/runtime/http.js.map +1 -1
  16. package/dist/runtime/node.d.ts +33 -7
  17. package/dist/runtime/node.d.ts.map +1 -1
  18. package/dist/runtime/node.js +139 -151
  19. package/dist/runtime/node.js.map +1 -1
  20. package/dist/runtime/pooled-transport.d.ts +2 -18
  21. package/dist/runtime/pooled-transport.d.ts.map +1 -1
  22. package/dist/runtime/pooled-transport.js +2 -29
  23. package/dist/runtime/pooled-transport.js.map +1 -1
  24. package/dist/runtime/process-io.d.ts +2 -18
  25. package/dist/runtime/process-io.d.ts.map +1 -1
  26. package/dist/runtime/process-io.js +3 -30
  27. package/dist/runtime/process-io.js.map +1 -1
  28. package/dist/runtime/pyodide-bootstrap-core.generated.d.ts +12 -0
  29. package/dist/runtime/pyodide-bootstrap-core.generated.d.ts.map +1 -0
  30. package/dist/runtime/pyodide-bootstrap-core.generated.js +12 -0
  31. package/dist/runtime/pyodide-bootstrap-core.generated.js.map +1 -0
  32. package/dist/runtime/pyodide-io.d.ts +7 -37
  33. package/dist/runtime/pyodide-io.d.ts.map +1 -1
  34. package/dist/runtime/pyodide-io.js +90 -214
  35. package/dist/runtime/pyodide-io.js.map +1 -1
  36. package/dist/runtime/pyodide.d.ts +25 -5
  37. package/dist/runtime/pyodide.d.ts.map +1 -1
  38. package/dist/runtime/pyodide.js +59 -10
  39. package/dist/runtime/pyodide.js.map +1 -1
  40. package/dist/runtime/{bridge-protocol.d.ts → rpc-client.d.ts} +59 -46
  41. package/dist/runtime/rpc-client.d.ts.map +1 -0
  42. package/dist/runtime/{bridge-protocol.js → rpc-client.js} +60 -42
  43. package/dist/runtime/rpc-client.js.map +1 -0
  44. package/dist/runtime/worker-pool.d.ts +2 -19
  45. package/dist/runtime/worker-pool.d.ts.map +1 -1
  46. package/dist/runtime/worker-pool.js +2 -30
  47. package/dist/runtime/worker-pool.js.map +1 -1
  48. package/dist/types/index.d.ts +8 -3
  49. package/dist/types/index.d.ts.map +1 -1
  50. package/dist/tywrap.d.ts.map +1 -1
  51. package/dist/tywrap.js +1 -3
  52. package/dist/tywrap.js.map +1 -1
  53. package/package.json +9 -12
  54. package/runtime/__pycache__/safe_codec.cpython-311.pyc +0 -0
  55. package/runtime/__pycache__/tywrap_bridge_core.cpython-311.pyc +0 -0
  56. package/runtime/python_bridge.py +85 -702
  57. package/runtime/safe_codec.py +10 -2
  58. package/runtime/tywrap_bridge_core.py +875 -0
  59. package/src/index.ts +6 -6
  60. package/src/runtime/base.ts +8 -8
  61. package/src/runtime/bounded-context.ts +17 -56
  62. package/src/runtime/http.ts +85 -13
  63. package/src/runtime/node.ts +165 -217
  64. package/src/runtime/pooled-transport.ts +2 -57
  65. package/src/runtime/process-io.ts +3 -55
  66. package/src/runtime/pyodide-bootstrap-core.generated.ts +12 -0
  67. package/src/runtime/pyodide-io.ts +92 -243
  68. package/src/runtime/pyodide.ts +87 -10
  69. package/src/runtime/{bridge-protocol.ts → rpc-client.ts} +76 -49
  70. package/src/runtime/worker-pool.ts +2 -58
  71. package/src/types/index.ts +20 -4
  72. package/src/tywrap.ts +1 -3
  73. package/dist/core/analyzer.d.ts +0 -64
  74. package/dist/core/analyzer.d.ts.map +0 -1
  75. package/dist/core/analyzer.js +0 -710
  76. package/dist/core/analyzer.js.map +0 -1
  77. package/dist/runtime/bridge-core.d.ts +0 -65
  78. package/dist/runtime/bridge-core.d.ts.map +0 -1
  79. package/dist/runtime/bridge-core.js +0 -379
  80. package/dist/runtime/bridge-core.js.map +0 -1
  81. package/dist/runtime/bridge-protocol.d.ts.map +0 -1
  82. package/dist/runtime/bridge-protocol.js.map +0 -1
  83. package/dist/utils/parallel-processor.d.ts +0 -146
  84. package/dist/utils/parallel-processor.d.ts.map +0 -1
  85. package/dist/utils/parallel-processor.js +0 -707
  86. package/dist/utils/parallel-processor.js.map +0 -1
  87. package/src/core/analyzer.ts +0 -824
  88. package/src/runtime/bridge-core.ts +0 -494
  89. package/src/utils/parallel-processor.ts +0 -955
@@ -1,824 +0,0 @@
1
- /**
2
- * PyAnalyzer - Python AST analysis and type extraction
3
- */
4
-
5
- import type { SyntaxNode } from 'tree-sitter';
6
- import Parser from 'tree-sitter';
7
- import Python from 'tree-sitter-python';
8
-
9
- import type {
10
- AnalysisResult,
11
- AnalysisError,
12
- AnalysisWarning,
13
- AnalysisStatistics,
14
- PythonModule,
15
- PythonFunction,
16
- PythonClass,
17
- PythonImport,
18
- PythonType,
19
- Parameter,
20
- Property,
21
- FunctionSignature,
22
- } from '../types/index.js';
23
- import { globalCache } from '../utils/cache.js';
24
- import { getComponentLogger } from '../utils/logger.js';
25
-
26
- const log = getComponentLogger('Analyzer');
27
-
28
- const UNKNOWN_TYPE: PythonType = { kind: 'custom', name: 'Any', module: 'typing' };
29
- const PYTHON_LANGUAGE = Python as unknown as Parameters<Parser['setLanguage']>[0];
30
-
31
- export class PyAnalyzer {
32
- private parser: Parser;
33
- private initialized = false;
34
-
35
- constructor() {
36
- this.parser = new Parser();
37
- }
38
-
39
- /**
40
- * Initialize the tree-sitter parser with Python grammar
41
- */
42
- async initialize(): Promise<void> {
43
- if (this.initialized) {
44
- return;
45
- }
46
-
47
- try {
48
- await this.parser.setLanguage(PYTHON_LANGUAGE);
49
- this.initialized = true;
50
- } catch (error) {
51
- throw new Error(`Failed to initialize Python parser: ${error}`);
52
- }
53
- }
54
-
55
- /**
56
- * Analyze Python module source code and extract structure with caching
57
- */
58
- async analyzePythonModule(source: string, modulePath?: string): Promise<AnalysisResult> {
59
- await this.initialize();
60
-
61
- // Check cache first
62
- const cached = await globalCache.getCachedAnalysis(source, modulePath ?? 'unknown');
63
- if (cached) {
64
- return cached;
65
- }
66
-
67
- const startTime = performance.now();
68
- const errors: AnalysisError[] = [];
69
- const warnings: AnalysisWarning[] = [];
70
- const dependencies: string[] = [];
71
-
72
- try {
73
- const tree = this.parser.parse(source);
74
- const rootNode = tree.rootNode;
75
-
76
- // Check for syntax errors
77
- if (rootNode.hasError) {
78
- this.collectSyntaxErrors(rootNode, errors);
79
- }
80
-
81
- // Extract module components
82
- const functions = await this.extractFunctions(rootNode);
83
- const classes = await this.extractClasses(rootNode);
84
- const imports = this.extractImports(rootNode);
85
-
86
- // Collect dependencies from imports (deduplicated)
87
- const uniqueDependencies = [...new Set(imports.map(imp => imp.module))];
88
- dependencies.push(...uniqueDependencies);
89
-
90
- // Generate statistics
91
- const statistics = this.generateStatistics(functions, classes, source);
92
-
93
- const module: PythonModule = {
94
- name: this.extractModuleName(modulePath),
95
- path: modulePath,
96
- functions,
97
- classes,
98
- imports,
99
- exports: this.extractExports(rootNode),
100
- };
101
-
102
- const result = {
103
- module,
104
- errors,
105
- warnings,
106
- dependencies,
107
- statistics,
108
- };
109
-
110
- // Cache the successful result
111
- const computeTime = performance.now() - startTime;
112
- await globalCache.setCachedAnalysis(source, modulePath ?? 'unknown', result, computeTime);
113
-
114
- return result;
115
- } catch (error) {
116
- errors.push({
117
- type: 'syntax',
118
- message: `Failed to parse Python module: ${error}`,
119
- file: modulePath,
120
- });
121
-
122
- return {
123
- module: {
124
- name: this.extractModuleName(modulePath),
125
- path: modulePath,
126
- functions: [],
127
- classes: [],
128
- imports: [],
129
- exports: [],
130
- },
131
- errors,
132
- warnings,
133
- dependencies,
134
- statistics: this.generateStatistics([], [], source),
135
- };
136
- }
137
- }
138
-
139
- /**
140
- * Extract function definitions from AST
141
- */
142
- async extractFunctions(node: SyntaxNode): Promise<PythonFunction[]> {
143
- const functions: PythonFunction[] = [];
144
-
145
- const functionNodes = this.findDirectDefinitions(node, 'function_definition');
146
-
147
- for (const funcNode of functionNodes) {
148
- try {
149
- const func = await this.extractFunction(funcNode);
150
- functions.push(func);
151
- } catch (error) {
152
- log.warn('Failed to extract function', { error: String(error) });
153
- }
154
- }
155
-
156
- return functions;
157
- }
158
-
159
- /**
160
- * Extract class definitions from AST
161
- */
162
- async extractClasses(node: SyntaxNode): Promise<PythonClass[]> {
163
- const classes: PythonClass[] = [];
164
-
165
- const classNodes = this.findDirectDefinitions(node, 'class_definition');
166
-
167
- for (const classNode of classNodes) {
168
- try {
169
- const cls = await this.extractClass(classNode);
170
- classes.push(cls);
171
- } catch (error) {
172
- log.warn('Failed to extract class', { error: String(error) });
173
- }
174
- }
175
-
176
- return classes;
177
- }
178
-
179
- /**
180
- * Extract single function from function_definition node
181
- */
182
- private async extractFunction(node: SyntaxNode): Promise<PythonFunction> {
183
- const nameNode = node.childForFieldName('name');
184
- const parametersNode = node.childForFieldName('parameters');
185
- const returnTypeNode = node.childForFieldName('return_type');
186
- const bodyNode = node.childForFieldName('body');
187
-
188
- const name = nameNode?.text ?? 'unknown';
189
- const parameters = parametersNode ? this.extractParameters(parametersNode) : [];
190
- const returnType = returnTypeNode ? this.parseTypeAnnotation(returnTypeNode) : UNKNOWN_TYPE;
191
-
192
- // Extract decorators
193
- const decorators = this.extractDecorators(node);
194
-
195
- // Determine if async
196
- const isAsync = node.text.trimStart().startsWith('async def');
197
-
198
- // Check if generator (contains yield)
199
- const isGenerator = bodyNode ? this.containsYield(bodyNode) : false;
200
-
201
- // Extract docstring
202
- const docstring = bodyNode ? this.extractDocstring(bodyNode) : undefined;
203
-
204
- const signature: FunctionSignature = {
205
- parameters,
206
- returnType,
207
- isAsync,
208
- isGenerator,
209
- };
210
-
211
- return {
212
- name,
213
- signature,
214
- docstring,
215
- decorators,
216
- isAsync,
217
- isGenerator,
218
- returnType,
219
- parameters,
220
- };
221
- }
222
-
223
- /**
224
- * Extract single class from class_definition node
225
- */
226
- private async extractClass(node: SyntaxNode): Promise<PythonClass> {
227
- const nameNode = node.childForFieldName('name');
228
- const superclassesNode = node.childForFieldName('superclasses');
229
- const bodyNode = node.childForFieldName('body');
230
-
231
- const name = nameNode?.text ?? 'unknown';
232
- const bases = superclassesNode ? this.extractBases(superclassesNode) : [];
233
-
234
- const decorators = this.extractDecorators(node);
235
- const docstring = bodyNode ? this.extractDocstring(bodyNode) : undefined;
236
-
237
- // Extract methods and properties from body
238
- const methods: PythonFunction[] = [];
239
- const properties: Property[] = [];
240
-
241
- if (bodyNode) {
242
- const methodNodes = this.findDirectDefinitions(bodyNode, 'function_definition');
243
- for (const methodNode of methodNodes) {
244
- try {
245
- const method = await this.extractFunction(methodNode);
246
- methods.push(method);
247
- } catch (error) {
248
- log.warn('Failed to extract method', { error: String(error) });
249
- }
250
- }
251
-
252
- // Extract properties (assignments with type annotations or @property decorators)
253
- for (const stmt of bodyNode.namedChildren) {
254
- // Only consider class-body statements, never descend into method bodies.
255
- const assignmentNode =
256
- stmt.type === 'assignment'
257
- ? stmt
258
- : stmt.type === 'expression_statement'
259
- ? (stmt.namedChildren.find(c => c.type === 'assignment') ?? null)
260
- : null;
261
- if (!assignmentNode) {
262
- continue;
263
- }
264
- const prop = this.extractProperty(assignmentNode);
265
- if (prop) {
266
- properties.push(prop);
267
- }
268
- }
269
- }
270
-
271
- return {
272
- name,
273
- bases,
274
- methods,
275
- properties,
276
- docstring,
277
- decorators,
278
- };
279
- }
280
-
281
- /**
282
- * Extract function parameters from parameters node
283
- */
284
- private extractParameters(node: SyntaxNode): Parameter[] {
285
- const parameters: Parameter[] = [];
286
-
287
- // Handle different parameter types
288
- for (const child of node.namedChildren) {
289
- if (child.type === 'identifier') {
290
- // Simple parameter
291
- parameters.push({
292
- name: child.text,
293
- type: UNKNOWN_TYPE,
294
- optional: false,
295
- varArgs: false,
296
- kwArgs: false,
297
- });
298
- } else if (child.type === 'typed_parameter') {
299
- // Parameter with type annotation - check multiple possible field names
300
- const patternNode = child.childForFieldName('pattern');
301
- let nameNode = patternNode ?? child.child(0) ?? null;
302
- const typeFieldNode = child.childForFieldName('type');
303
- const typeNode = typeFieldNode ?? child.child(2) ?? null;
304
-
305
- // If pattern field doesn't exist, try to find identifier directly
306
- if (!nameNode || nameNode.type !== 'identifier') {
307
- nameNode = this.findNodesByType(child, 'identifier')[0] ?? null;
308
- }
309
-
310
- parameters.push({
311
- name: nameNode?.text ?? 'unknown',
312
- type: typeNode ? this.parseTypeAnnotation(typeNode) : UNKNOWN_TYPE,
313
- optional: false,
314
- varArgs: false,
315
- kwArgs: false,
316
- });
317
- } else if (child.type === 'default_parameter') {
318
- // Parameter with default value
319
- const nameFieldNode = child.childForFieldName('name');
320
- const nameNode = nameFieldNode ?? child.child(0) ?? null;
321
- let valueNode = child.childForFieldName('value') ?? null;
322
-
323
- // Find value node if field name doesn't work
324
- if (!valueNode) {
325
- const equalIndex = child.children.findIndex(c => c.text === '=');
326
- if (equalIndex >= 0 && equalIndex + 1 < child.children.length) {
327
- valueNode = child.children[equalIndex + 1] ?? null;
328
- }
329
- }
330
-
331
- parameters.push({
332
- name: nameNode?.text ?? 'unknown',
333
- type: UNKNOWN_TYPE,
334
- optional: true,
335
- defaultValue: valueNode?.text,
336
- varArgs: false,
337
- kwArgs: false,
338
- });
339
- } else if (child.type === 'typed_default_parameter') {
340
- // Parameter with type and default
341
- const patternNode = child.childForFieldName('pattern');
342
- let nameNode = patternNode ?? child.child(0) ?? null;
343
- let typeNode = child.childForFieldName('type') ?? null;
344
- let valueNode = child.childForFieldName('value') ?? null;
345
-
346
- // If pattern field doesn't exist, try to find identifier directly
347
- if (!nameNode || nameNode.type !== 'identifier') {
348
- nameNode = this.findNodesByType(child, 'identifier')[0] ?? null;
349
- }
350
-
351
- // Find type and value nodes if field names don't work
352
- if (!typeNode || !valueNode) {
353
- const colonIndex = child.children.findIndex(c => c.text === ':');
354
- const equalIndex = child.children.findIndex(c => c.text === '=');
355
-
356
- if (colonIndex >= 0 && colonIndex + 1 < child.children.length) {
357
- typeNode = child.children[colonIndex + 1] ?? null;
358
- }
359
- if (equalIndex >= 0 && equalIndex + 1 < child.children.length) {
360
- valueNode = child.children[equalIndex + 1] ?? null;
361
- }
362
- }
363
-
364
- parameters.push({
365
- name: nameNode?.text ?? 'unknown',
366
- type: typeNode ? this.parseTypeAnnotation(typeNode) : UNKNOWN_TYPE,
367
- optional: true,
368
- defaultValue: valueNode?.text,
369
- varArgs: false,
370
- kwArgs: false,
371
- });
372
- } else if (child.type === 'list_splat_pattern') {
373
- // *args parameter
374
- const nameNode = child.child(1); // Skip the *
375
- parameters.push({
376
- name: nameNode?.text ?? 'args',
377
- type: { kind: 'collection', name: 'tuple', itemTypes: [] },
378
- optional: false,
379
- varArgs: true,
380
- kwArgs: false,
381
- });
382
- } else if (child.type === 'dictionary_splat_pattern') {
383
- // **kwargs parameter
384
- const nameNode = child.child(1); // Skip the **
385
- parameters.push({
386
- name: nameNode?.text ?? 'kwargs',
387
- type: { kind: 'collection', name: 'dict', itemTypes: [] },
388
- optional: false,
389
- varArgs: false,
390
- kwArgs: true,
391
- });
392
- }
393
- }
394
-
395
- return parameters;
396
- }
397
-
398
- /**
399
- * Parse type annotation node into PythonType
400
- */
401
- parseTypeAnnotation(node: SyntaxNode): PythonType {
402
- const typeText = node.text.trim();
403
-
404
- // Handle primitive types
405
- const primitiveTypes = ['int', 'float', 'str', 'bool', 'bytes', 'None'] as const;
406
- if (primitiveTypes.includes(typeText as (typeof primitiveTypes)[number])) {
407
- return {
408
- kind: 'primitive',
409
- name: typeText as 'int' | 'float' | 'str' | 'bool' | 'bytes' | 'None',
410
- };
411
- }
412
-
413
- // Handle collection types
414
- const collectionTypes = [
415
- 'dict',
416
- 'Dict',
417
- 'list',
418
- 'List',
419
- 'tuple',
420
- 'Tuple',
421
- 'set',
422
- 'Set',
423
- ] as const;
424
- if (collectionTypes.includes(typeText as (typeof collectionTypes)[number])) {
425
- const normalizedName =
426
- typeText.toLowerCase() === 'dict' || typeText === 'Dict'
427
- ? 'dict'
428
- : typeText.toLowerCase() === 'list' || typeText === 'List'
429
- ? 'list'
430
- : typeText.toLowerCase() === 'tuple' || typeText === 'Tuple'
431
- ? 'tuple'
432
- : typeText.toLowerCase() === 'set' || typeText === 'Set'
433
- ? 'set'
434
- : 'dict';
435
- return { kind: 'collection', name: normalizedName, itemTypes: [] };
436
- }
437
-
438
- // Handle List, Dict, etc.
439
- if (typeText.startsWith('List[') || typeText.startsWith('list[')) {
440
- return this.parseGenericType(typeText, 'list');
441
- }
442
- if (typeText.startsWith('Dict[') || typeText.startsWith('dict[')) {
443
- return this.parseGenericType(typeText, 'dict');
444
- }
445
- if (typeText.startsWith('Tuple[') || typeText.startsWith('tuple[')) {
446
- return this.parseGenericType(typeText, 'tuple');
447
- }
448
- if (typeText.startsWith('Set[') || typeText.startsWith('set[')) {
449
- return this.parseGenericType(typeText, 'set');
450
- }
451
-
452
- // Handle Union types
453
- if (typeText.includes(' | ') || typeText.startsWith('Union[')) {
454
- return this.parseUnionType(typeText);
455
- }
456
-
457
- // Handle Optional
458
- if (typeText.startsWith('Optional[')) {
459
- const innerType = typeText.slice(9, -1);
460
- const parsedInnerType = this.parseTypeAnnotation({ text: innerType } as SyntaxNode);
461
-
462
- // For Optional[Dict] -> ensure Dict is treated as collection, not custom
463
- if (innerType === 'Dict' || innerType === 'dict') {
464
- return {
465
- kind: 'optional',
466
- type: { kind: 'collection', name: 'dict', itemTypes: [] },
467
- };
468
- }
469
-
470
- return {
471
- kind: 'optional',
472
- type: parsedInnerType,
473
- };
474
- }
475
-
476
- // Custom/module type - clean up quotes
477
- let cleanedName = typeText;
478
- if (
479
- (cleanedName.startsWith("'") && cleanedName.endsWith("'")) ||
480
- (cleanedName.startsWith('"') && cleanedName.endsWith('"'))
481
- ) {
482
- cleanedName = cleanedName.slice(1, -1);
483
- }
484
-
485
- return {
486
- kind: 'custom',
487
- name: cleanedName,
488
- };
489
- }
490
-
491
- private parseGenericType(
492
- typeText: string,
493
- collectionName: 'list' | 'dict' | 'tuple' | 'set'
494
- ): PythonType {
495
- const bracketStart = typeText.indexOf('[');
496
- const bracketEnd = typeText.lastIndexOf(']');
497
-
498
- if (bracketStart === -1 || bracketEnd === -1) {
499
- return { kind: 'collection', name: collectionName, itemTypes: [] };
500
- }
501
-
502
- const genericPart = typeText.slice(bracketStart + 1, bracketEnd);
503
- const itemTypes = this.parseGenericArguments(genericPart);
504
-
505
- return {
506
- kind: 'collection',
507
- name: collectionName,
508
- itemTypes,
509
- };
510
- }
511
-
512
- private parseUnionType(typeText: string): PythonType {
513
- let unionPart: string;
514
-
515
- if (typeText.startsWith('Union[')) {
516
- unionPart = typeText.slice(6, -1);
517
- } else {
518
- unionPart = typeText;
519
- }
520
-
521
- const types = this.parseGenericArguments(unionPart);
522
- return { kind: 'union', types };
523
- }
524
-
525
- private parseGenericArguments(argsText: string): PythonType[] {
526
- // Nested-aware argument splitter for generics (handles [] and ())
527
- const results: string[] = [];
528
- let depthSquare = 0;
529
- let depthParen = 0;
530
- let current = '';
531
- for (let i = 0; i < argsText.length; i++) {
532
- const ch = String(argsText.charAt(i));
533
- if (ch === '[') {
534
- depthSquare++;
535
- }
536
- if (ch === ']') {
537
- depthSquare = Math.max(0, depthSquare - 1);
538
- }
539
- if (ch === '(') {
540
- depthParen++;
541
- }
542
- if (ch === ')') {
543
- depthParen = Math.max(0, depthParen - 1);
544
- }
545
- if (ch === ',' && depthSquare === 0 && depthParen === 0) {
546
- results.push(current.trim());
547
- current = '';
548
- continue;
549
- }
550
- current += ch;
551
- }
552
- if (current.trim().length > 0) {
553
- results.push(current.trim());
554
- }
555
- return results.map(arg => this.parseTypeAnnotation({ text: arg } as SyntaxNode));
556
- }
557
-
558
- /**
559
- * Extract import statements
560
- */
561
- private extractImports(node: SyntaxNode): PythonImport[] {
562
- const imports: PythonImport[] = [];
563
- const seen = new Set<string>();
564
-
565
- // Only look for import statements at the module level (not nested)
566
- const moduleChildren = node.namedChildren;
567
- const importNodes: SyntaxNode[] = [];
568
-
569
- for (const child of moduleChildren) {
570
- if (child.type === 'import_statement' || child.type === 'import_from_statement') {
571
- importNodes.push(child);
572
- }
573
- }
574
-
575
- for (const importNode of importNodes) {
576
- if (importNode.type === 'import_statement') {
577
- // import module [as alias]
578
- const nameNode = importNode.child(1);
579
- if (nameNode && (nameNode.type === 'dotted_name' || nameNode.type === 'aliased_import')) {
580
- let moduleName: string | undefined;
581
- if (nameNode.type === 'aliased_import') {
582
- // Handle 'import module as alias' - extract module name before 'as'
583
- const parts = nameNode.text.split(' as ');
584
- const firstPart = parts[0];
585
- if (firstPart) {
586
- moduleName = firstPart.split('.')[0]; // Get base module name
587
- }
588
- } else {
589
- // Handle 'import module' - extract module name
590
- moduleName = nameNode.text.split('.')[0]; // Get base module name
591
- }
592
-
593
- if (moduleName) {
594
- const key = `${moduleName}:import`;
595
- if (!seen.has(key)) {
596
- seen.add(key);
597
- imports.push({
598
- module: moduleName,
599
- fromImport: false,
600
- });
601
- }
602
- }
603
- }
604
- } else if (importNode.type === 'import_from_statement') {
605
- // from module import name [as alias]
606
- const moduleNameIndex = importNode.children.findIndex(c => c.text === 'from');
607
- const importIndex = importNode.children.findIndex(c => c.text === 'import');
608
- if (moduleNameIndex >= 0 && importIndex > moduleNameIndex) {
609
- const moduleNode = importNode.children[moduleNameIndex + 1];
610
-
611
- if (moduleNode && moduleNode.type === 'dotted_name') {
612
- const moduleName = moduleNode.text.split('.')[0];
613
-
614
- if (moduleName) {
615
- // Collect all import names after the 'import' keyword
616
- const importNames: string[] = [];
617
-
618
- for (let i = importIndex + 1; i < importNode.children.length; i++) {
619
- const child = importNode.children.at(i);
620
- if (child && (child.type === 'dotted_name' || child.type === 'identifier')) {
621
- importNames.push(child.text);
622
- } else if (child && child.type === 'wildcard_import') {
623
- importNames.push('*');
624
- }
625
- // Skip comma separators
626
- }
627
-
628
- // Create separate import entries for each imported name
629
- for (const importName of importNames) {
630
- const key = `${moduleName}:${importName}`;
631
- if (!seen.has(key)) {
632
- seen.add(key);
633
- imports.push({
634
- module: moduleName,
635
- name: importName,
636
- fromImport: true,
637
- });
638
- }
639
- }
640
- }
641
- }
642
- }
643
- }
644
- }
645
-
646
- return imports;
647
- }
648
-
649
- /**
650
- * Utility methods
651
- */
652
- private findNodesByType(node: SyntaxNode, type: string): SyntaxNode[] {
653
- const results: SyntaxNode[] = [];
654
- const stack: SyntaxNode[] = [node];
655
- while (stack.length > 0) {
656
- const n = stack.pop();
657
- if (!n) {
658
- break;
659
- }
660
- if (n.type === type) {
661
- results.push(n);
662
- }
663
- for (let i = n.children.length - 1; i >= 0; i--) {
664
- const child = n.children[i];
665
- if (child) {
666
- stack.push(child);
667
- }
668
- }
669
- }
670
- return results;
671
- }
672
-
673
- private findDirectDefinitions(
674
- node: SyntaxNode,
675
- type: 'function_definition' | 'class_definition'
676
- ): SyntaxNode[] {
677
- const out: SyntaxNode[] = [];
678
- for (const child of node.namedChildren) {
679
- if (child.type === type) {
680
- out.push(child);
681
- continue;
682
- }
683
- if (child.type === 'decorated_definition') {
684
- const def = child.namedChildren.find(c => c.type === type) ?? null;
685
- if (def) {
686
- out.push(def);
687
- }
688
- }
689
- }
690
- return out;
691
- }
692
-
693
- private extractModuleName(path?: string): string {
694
- if (!path) {
695
- return 'unknown';
696
- }
697
- const parts = path.split('/').pop()?.replace('.py', '') ?? 'unknown';
698
- return parts;
699
- }
700
-
701
- private extractDecorators(node: SyntaxNode): string[] {
702
- const decorators: string[] = [];
703
-
704
- // Look for decorated_definition parent
705
- if (node.parent?.type === 'decorated_definition') {
706
- const decoratorNodes = this.findNodesByType(node.parent, 'decorator');
707
- decorators.push(...decoratorNodes.map(d => d.text));
708
- }
709
-
710
- return decorators;
711
- }
712
-
713
- private extractDocstring(bodyNode: SyntaxNode): string | undefined {
714
- // First statement in body might be a string (docstring)
715
- const firstChild = bodyNode.namedChildren[0];
716
- if (firstChild?.type === 'expression_statement') {
717
- const expr = firstChild.child(0);
718
- if (expr?.type === 'string') {
719
- let docstring = expr.text;
720
- // Strip docstring prefixes like r/u/f/b and combos (rf/fr/br/...).
721
- docstring = docstring.replace(/^(?:[rRuUbBfF]{1,2})(?=(\"\"\"|'''|\"|'))/, '');
722
- // Remove outer quotes and any remaining inner quotes
723
- if (docstring.startsWith('"""') && docstring.endsWith('"""')) {
724
- docstring = docstring.slice(3, -3);
725
- } else if (docstring.startsWith("'''") && docstring.endsWith("'''")) {
726
- docstring = docstring.slice(3, -3);
727
- } else if (docstring.startsWith('"') && docstring.endsWith('"')) {
728
- docstring = docstring.slice(1, -1);
729
- } else if (docstring.startsWith("'") && docstring.endsWith("'")) {
730
- docstring = docstring.slice(1, -1);
731
- }
732
- return docstring.trim();
733
- }
734
- }
735
- return undefined;
736
- }
737
-
738
- private containsYield(node: SyntaxNode): boolean {
739
- const yieldNodes = this.findNodesByType(node, 'yield');
740
- return yieldNodes.length > 0;
741
- }
742
-
743
- private extractBases(node: SyntaxNode): string[] {
744
- const bases: string[] = [];
745
- for (const child of node.namedChildren) {
746
- if (child.type === 'identifier') {
747
- bases.push(child.text);
748
- }
749
- }
750
- return bases;
751
- }
752
-
753
- private extractProperty(node: SyntaxNode): Property | null {
754
- // Simple property extraction from assignments
755
- const leftNode = node.child(0);
756
- if (leftNode?.type === 'identifier') {
757
- return {
758
- name: leftNode.text,
759
- type: UNKNOWN_TYPE,
760
- readonly: false,
761
- };
762
- }
763
- return null;
764
- }
765
-
766
- private extractExports(node: SyntaxNode): string[] {
767
- // Look for __all__ definition
768
- const assignments = this.findNodesByType(node, 'assignment');
769
- for (const assignment of assignments) {
770
- const leftNode = assignment.child(0);
771
- if (leftNode?.text === '__all__') {
772
- const rightNode = assignment.child(2);
773
- if (rightNode?.type === 'list') {
774
- return rightNode.namedChildren
775
- .filter(child => child.type === 'string')
776
- .map(child => child.text.slice(1, -1)); // Remove quotes
777
- }
778
- }
779
- }
780
- return [];
781
- }
782
-
783
- private collectSyntaxErrors(node: SyntaxNode, errors: AnalysisError[]): void {
784
- if (node.hasError) {
785
- errors.push({
786
- type: 'syntax',
787
- message: 'Syntax error in Python code',
788
- line: node.startPosition.row + 1,
789
- column: node.startPosition.column + 1,
790
- });
791
- }
792
-
793
- for (const child of node.children) {
794
- this.collectSyntaxErrors(child, errors);
795
- }
796
- }
797
-
798
- private generateStatistics(
799
- functions: PythonFunction[],
800
- classes: PythonClass[],
801
- source: string
802
- ): AnalysisStatistics {
803
- // Count only module-level functions, not class methods in the main count
804
- const totalFunctions = functions.length;
805
- const totalClasses = classes.length;
806
-
807
- // Simple type hint coverage calculation
808
- const functionsWithTypes = functions.filter(
809
- f =>
810
- f.parameters.some(p => p.type.kind !== 'primitive' || p.type.name !== 'None') ||
811
- f.returnType.kind !== 'primitive' ||
812
- f.returnType.name !== 'None'
813
- ).length;
814
-
815
- const typeHintsCoverage = totalFunctions > 0 ? (functionsWithTypes / totalFunctions) * 100 : 0;
816
-
817
- return {
818
- functionsAnalyzed: totalFunctions,
819
- classesAnalyzed: totalClasses,
820
- typeHintsCoverage,
821
- estimatedComplexity: Math.min(source.length / 1000, 10), // Simple complexity estimate
822
- };
823
- }
824
- }