This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
make_stub_files.leo
2013 lines (1788 loc) · 84.1 KB
/
make_stub_files.leo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: http://leoeditor.com/leo_toc.html -->
<leo_file xmlns:leo="http://leoeditor.com/namespaces/leo-python-editor/1.1" >
<leo_header file_format="2"/>
<globals/>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="ekr.20160207181637.1"><vh>Startup</vh>
<v t="ekr.20160207181648.1"><vh>@settings</vh>
<v t="ekr.20180706073424.1"><vh>@bool run-pyflakes-on-write = False</vh></v>
<v t="ekr.20160207182535.1"><vh>@bool preload-find-pattern = False</vh></v>
</v>
<v t="ekr.20210805061637.1"><vh>buttons</vh>
<v t="ekr.20210804020706.1"><vh>@button coverage</vh></v>
<v t="ekr.20210804060105.1"><vh>@button unitttest</vh></v>
<v t="ekr.20210807133351.1"><vh>@button unitttest1</vh></v>
<v t="ekr.20210804021331.1"><vh>@button msf</vh></v>
</v>
</v>
<v t="ekr.20210810052527.1"><vh>Notes</vh>
<v t="ekr.20210810052454.1"><vh>No longer used</vh>
<v t="ekr.20210810052805.1"><vh> Unused python 2 visitors</vh>
<v t="ekr.20160318141204.65"><vh>f.Exec</vh></v>
<v t="ekr.20160318141204.41"><vh>f.Index</vh></v>
<v t="ekr.20160318141204.74"><vh>f.Print</vh></v>
<v t="ekr.20160318141204.46"><vh>f.Repr</vh></v>
<v t="ekr.20160318141204.80"><vh>f.TryExcept</vh></v>
<v t="ekr.20160318141204.81"><vh>f.TryFinally</vh></v>
</v>
<v t="ekr.20210809094659.1"><vh>launch_msf.py</vh></v>
<v t="ekr.20160318141204.5"><vh>merge_types</vh></v>
<v t="ekr.20210804153200.1"><vh>script convert-at-test (no longer necessary)</vh></v>
<v t="ekr.20160207051429.1"><vh>test_merge_types</vh></v>
</v>
<v t="ekr.20210805053830.1"><vh>Notes for #18: full coverage</vh>
<v t="ekr.20210806162349.1"><vh>--- changed & new AstFormatter visitors</vh>
<v t="ekr.20160318141204.91"><vh>arg_formatter.Constants & Name</vh></v>
<v t="ekr.20160318141204.59"><vh>f.AnnAssign & Assign</vh></v>
<v t="ekr.20160318141204.31"><vh>f.arguments</vh></v>
<v t="ekr.20160318141204.35"><vh>f.Call & f.keyword</vh>
<v t="ekr.20160318141204.36"><vh>f.keyword</vh></v>
</v>
<v t="ekr.20160318141204.20"><vh>f.ClassDef</vh></v>
<v t="ekr.20210804214511.1"><vh>f.Constant</vh></v>
<v t="ekr.20160318141204.75"><vh>f.Raise</vh></v>
<v t="ekr.20160318141204.18"><vh>f.visit</vh></v>
<v t="ekr.20160318141204.83"><vh>f.With</vh></v>
<v t="ekr.20160318141204.84"><vh>f.Yield</vh></v>
<v t="ekr.20160318141204.85"><vh>f.YieldFrom</vh></v>
</v>
<v t="ekr.20210807151105.1"><vh>--- changed StubFormatter visitors</vh>
<v t="ekr.20160318141204.168"><vh>sf.Return</vh></v>
<v t="ekr.20160318141204.167"><vh>sf.UnaryOp</vh></v>
</v>
<v t="ekr.20210807213637.1"><vh>--- changed StubTraverser methods</vh>
<v t="ekr.20160318141204.191"><vh>st.format_return_expressions</vh></v>
</v>
</v>
</v>
<v t="ekr.20210810115607.1"><vh>Files</vh>
<v t="ekr.20160128225533.1"><vh>@clean .gitignore</vh></v>
<v t="ekr.20160128102557.1"><vh>@clean example.cfg</vh></v>
<v t="ekr.20160126153220.1"><vh>@clean make_stub_files.cfg</vh></v>
<v t="ekr.20160330201030.1"><vh>@clean PKG-INFO.TXT</vh></v>
<v t="ekr.20160211110739.1"><vh>@clean README.md</vh>
<v t="ekr.20160211110807.1"><vh>Overview</vh></v>
<v t="ekr.20160211113019.1"><vh>Quick start</vh></v>
<v t="ekr.20160211110810.1"><vh>Command-line arguments</vh></v>
<v t="ekr.20160211110810.2"><vh>The configuration file</vh>
<v t="ekr.20160211111807.1"><vh>Patterns</vh></v>
<v t="ekr.20160211111823.1"><vh>[Global]</vh></v>
<v t="ekr.20160211111839.1"><vh>[Def Name Patterns]</vh></v>
<v t="ekr.20160211111901.1"><vh>[General Patterns]</vh></v>
</v>
<v t="ekr.20160211110810.3"><vh>Why this script is important</vh></v>
<v t="ekr.20160211110811.1"><vh>Summary</vh></v>
<v t="ekr.20210810053434.1"><vh>Release notes</vh></v>
</v>
<v t="ekr.20160207101607.1"><vh>@clean theory.md</vh></v>
<v t="ekr.20210809124558.1"><vh>@clean make_stub_files.pyi</vh>
<v t="ekr.20210809124613.1"><vh>Declarations</vh></v>
<v t="ekr.20210809124613.2"><vh>dump</vh></v>
<v t="ekr.20210809124613.3"><vh>dump_dict</vh></v>
<v t="ekr.20210809124613.4"><vh>dump_list</vh></v>
<v t="ekr.20210809124613.5"><vh>finalize</vh></v>
<v t="ekr.20210809124613.6"><vh>is_known_type</vh></v>
<v t="ekr.20210809124613.7"><vh>main</vh></v>
<v t="ekr.20210809124613.8"><vh>reduce_types</vh></v>
<v t="ekr.20210809124613.9"><vh>truncate</vh></v>
<v t="ekr.20210809124613.10"><vh>class AstFormatter</vh>
<v t="ekr.20210809124613.11"><vh>AstFormatter.format</vh></v>
<v t="ekr.20210809124613.12"><vh>AstFormatter.visit</vh></v>
<v t="ekr.20210809124613.13"><vh>AstFormatter.do_ClassDef</vh></v>
<v t="ekr.20210809124613.14"><vh>AstFormatter.do_FunctionDef</vh></v>
<v t="ekr.20210809124613.15"><vh>AstFormatter.do_Interactive</vh></v>
<v t="ekr.20210809124613.16"><vh>AstFormatter.do_Module</vh></v>
<v t="ekr.20210809124613.17"><vh>AstFormatter.do_Lambda</vh></v>
<v t="ekr.20210809124613.18"><vh>AstFormatter.do_Expr</vh></v>
<v t="ekr.20210809124613.19"><vh>AstFormatter.do_Expression</vh></v>
<v t="ekr.20210809124613.20"><vh>AstFormatter.do_GeneratorExp</vh></v>
<v t="ekr.20210809124613.21"><vh>AstFormatter.do_AugLoad</vh></v>
<v t="ekr.20210809124613.22"><vh>AstFormatter.do_Del</vh></v>
<v t="ekr.20210809124613.23"><vh>AstFormatter.do_Load</vh></v>
<v t="ekr.20210809124613.24"><vh>AstFormatter.do_Param</vh></v>
<v t="ekr.20210809124613.25"><vh>AstFormatter.do_Store</vh></v>
<v t="ekr.20210809124613.26"><vh>AstFormatter.do_arguments</vh></v>
<v t="ekr.20210809124613.27"><vh>AstFormatter.do_arg</vh></v>
<v t="ekr.20210809124613.28"><vh>AstFormatter.do_Attribute</vh></v>
<v t="ekr.20210809124613.29"><vh>AstFormatter.do_Bytes</vh></v>
<v t="ekr.20210809124613.30"><vh>AstFormatter.do_Call</vh></v>
<v t="ekr.20210809124613.31"><vh>AstFormatter.do_keyword</vh></v>
<v t="ekr.20210809124613.32"><vh>AstFormatter.do_Constant</vh></v>
<v t="ekr.20210809124613.33"><vh>AstFormatter.do_comprehension</vh></v>
<v t="ekr.20210809124613.34"><vh>AstFormatter.do_Dict</vh></v>
<v t="ekr.20210809124613.35"><vh>AstFormatter.do_Ellipsis</vh></v>
<v t="ekr.20210809124613.36"><vh>AstFormatter.do_ExtSlice</vh></v>
<v t="ekr.20210809124613.37"><vh>AstFormatter.do_FormattedValue</vh></v>
<v t="ekr.20210809124613.38"><vh>AstFormatter.do_JoinedStr</vh></v>
<v t="ekr.20210809124613.39"><vh>AstFormatter.do_List</vh></v>
<v t="ekr.20210809124613.40"><vh>AstFormatter.do_ListComp</vh></v>
<v t="ekr.20210809124613.41"><vh>AstFormatter.do_Name</vh></v>
<v t="ekr.20210809124613.42"><vh>AstFormatter.do_NameConstant</vh></v>
<v t="ekr.20210809124613.43"><vh>AstFormatter.do_Num</vh></v>
<v t="ekr.20210809124613.44"><vh>AstFormatter.do_Slice</vh></v>
<v t="ekr.20210809124613.45"><vh>AstFormatter.do_Str</vh></v>
<v t="ekr.20210809124613.46"><vh>AstFormatter.do_Subscript</vh></v>
<v t="ekr.20210809124613.47"><vh>AstFormatter.do_Tuple</vh></v>
<v t="ekr.20210809124613.48"><vh>AstFormatter.do_BinOp</vh></v>
<v t="ekr.20210809124613.49"><vh>AstFormatter.do_BoolOp</vh></v>
<v t="ekr.20210809124613.50"><vh>AstFormatter.do_Compare</vh></v>
<v t="ekr.20210809124613.51"><vh>AstFormatter.do_UnaryOp</vh></v>
<v t="ekr.20210809124613.52"><vh>AstFormatter.do_IfExp</vh></v>
<v t="ekr.20210809124613.53"><vh>AstFormatter.do_Assert</vh></v>
<v t="ekr.20210809124613.54"><vh>AstFormatter.do_AnnAssign</vh></v>
<v t="ekr.20210809124613.55"><vh>AstFormatter.do_Assign</vh></v>
<v t="ekr.20210809124613.56"><vh>AstFormatter.do_AugAssign</vh></v>
<v t="ekr.20210809124613.57"><vh>AstFormatter.do_Break</vh></v>
<v t="ekr.20210809124613.58"><vh>AstFormatter.do_Continue</vh></v>
<v t="ekr.20210809124613.59"><vh>AstFormatter.do_Delete</vh></v>
<v t="ekr.20210809124613.60"><vh>AstFormatter.do_ExceptHandler</vh></v>
<v t="ekr.20210809124613.61"><vh>AstFormatter.do_For</vh></v>
<v t="ekr.20210809124613.62"><vh>AstFormatter.do_Global</vh></v>
<v t="ekr.20210809124613.63"><vh>AstFormatter.do_If</vh></v>
<v t="ekr.20210809124613.64"><vh>AstFormatter.do_Import</vh></v>
<v t="ekr.20210809124613.65"><vh>AstFormatter.get_import_names</vh></v>
<v t="ekr.20210809124613.66"><vh>AstFormatter.do_ImportFrom</vh></v>
<v t="ekr.20210809124613.67"><vh>AstFormatter.do_Nonlocal</vh></v>
<v t="ekr.20210809124613.68"><vh>AstFormatter.do_Pass</vh></v>
<v t="ekr.20210809124613.69"><vh>AstFormatter.do_Raise</vh></v>
<v t="ekr.20210809124613.70"><vh>AstFormatter.do_Return</vh></v>
<v t="ekr.20210809124613.71"><vh>AstFormatter.do_Starred</vh></v>
<v t="ekr.20210809124613.72"><vh>AstFormatter.do_Try</vh></v>
<v t="ekr.20210809124613.73"><vh>AstFormatter.do_While</vh></v>
<v t="ekr.20210809124613.74"><vh>AstFormatter.do_With</vh></v>
<v t="ekr.20210809124613.75"><vh>AstFormatter.do_Yield</vh></v>
<v t="ekr.20210809124613.76"><vh>AstFormatter.do_YieldFrom</vh></v>
<v t="ekr.20210809124613.77"><vh>AstFormatter.kind</vh></v>
<v t="ekr.20210809124613.78"><vh>AstFormatter.indent</vh></v>
<v t="ekr.20210809124613.79"><vh>AstFormatter.op_name</vh></v>
</v>
<v t="ekr.20210809124613.80"><vh>class AstArgFormatter(AstFormatter)</vh>
<v t="ekr.20210809124613.81"><vh>AstArgFormatter(AstFormatter).do_Constant</vh></v>
<v t="ekr.20210809124613.82"><vh>AstArgFormatter(AstFormatter).do_BoolOp</vh></v>
<v t="ekr.20210809124613.83"><vh>AstArgFormatter(AstFormatter).do_Bytes</vh></v>
<v t="ekr.20210809124613.84"><vh>AstArgFormatter(AstFormatter).do_Name</vh></v>
<v t="ekr.20210809124613.85"><vh>AstArgFormatter(AstFormatter).do_Num</vh></v>
<v t="ekr.20210809124613.86"><vh>AstArgFormatter(AstFormatter).do_Str</vh></v>
</v>
<v t="ekr.20210809124613.87"><vh>class Controller</vh>
<v t="ekr.20210809124613.88"><vh>Controller.__init__</vh></v>
<v t="ekr.20210809124613.89"><vh>Controller.make_stub_file</vh></v>
<v t="ekr.20210809124613.90"><vh>Controller.scan_command_line</vh></v>
<v t="ekr.20210809124613.91"><vh>Controller.scan_options</vh></v>
<v t="ekr.20210809124613.92"><vh>Controller.make_op_name_dict</vh></v>
<v t="ekr.20210809124613.93"><vh>Controller.create_parser</vh></v>
<v t="ekr.20210809124613.94"><vh>Controller.find_pattern_ops</vh></v>
<v t="ekr.20210809124613.95"><vh>Controller.get_config_string</vh></v>
<v t="ekr.20210809124613.96"><vh>Controller.init_parser</vh></v>
<v t="ekr.20210809124613.97"><vh>Controller.is_section_name</vh></v>
<v t="ekr.20210809124613.98"><vh>Controller.make_patterns_dict</vh></v>
<v t="ekr.20210809124613.99"><vh>Controller.scan_patterns</vh></v>
</v>
<v t="ekr.20210809124613.100"><vh>class LeoGlobals</vh>
<v t="ekr.20210809124613.101"><vh>LeoGlobals._callerName</vh></v>
<v t="ekr.20210809124613.102"><vh>LeoGlobals.caller</vh></v>
<v t="ekr.20210809124613.103"><vh>LeoGlobals.callers</vh></v>
<v t="ekr.20210809124613.104"><vh>LeoGlobals.cls</vh></v>
<v t="ekr.20210809124613.105"><vh>LeoGlobals.objToString</vh></v>
<v t="ekr.20210809124613.106"><vh>LeoGlobals.dictToString</vh></v>
<v t="ekr.20210809124613.107"><vh>LeoGlobals.listToString</vh></v>
<v t="ekr.20210809124613.108"><vh>LeoGlobals.tupleToString</vh></v>
<v t="ekr.20210809124613.109"><vh>LeoGlobals.pdb</vh></v>
<v t="ekr.20210809124613.110"><vh>LeoGlobals.printObj</vh></v>
<v t="ekr.20210809124613.111"><vh>LeoGlobals.shortFileName</vh></v>
<v t="ekr.20210809124613.112"><vh>LeoGlobals.splitLines</vh></v>
<v t="ekr.20210809124613.113"><vh>LeoGlobals.trace</vh></v>
</v>
<v t="ekr.20210809124613.114"><vh>class Pattern</vh>
<v t="ekr.20210809124613.115"><vh>Pattern.__init__</vh></v>
<v t="ekr.20210809124613.116"><vh>Pattern.__eq__</vh></v>
<v t="ekr.20210809124613.117"><vh>Pattern.__ne__</vh></v>
<v t="ekr.20210809124613.118"><vh>Pattern.__hash__</vh></v>
<v t="ekr.20210809124613.119"><vh>Pattern.__repr__</vh></v>
<v t="ekr.20210809124613.120"><vh>Pattern.is_balanced</vh></v>
<v t="ekr.20210809124613.121"><vh>Pattern.is_regex</vh></v>
<v t="ekr.20210809124613.122"><vh>Pattern.all_matches</vh></v>
<v t="ekr.20210809124613.123"><vh>Pattern.full_balanced_match</vh></v>
<v t="ekr.20210809124613.124"><vh>Pattern.match_balanced</vh></v>
<v t="ekr.20210809124613.125"><vh>Pattern.match</vh></v>
<v t="ekr.20210809124613.126"><vh>Pattern.match_entire_string</vh></v>
<v t="ekr.20210809124613.127"><vh>Pattern.replace</vh></v>
<v t="ekr.20210809124613.128"><vh>Pattern.replace_balanced</vh></v>
<v t="ekr.20210809124613.129"><vh>Pattern.replace_regex</vh></v>
</v>
<v t="ekr.20210809124613.130"><vh>class ReduceTypes</vh>
<v t="ekr.20210809124613.131"><vh>ReduceTypes.__init__</vh></v>
<v t="ekr.20210809124613.132"><vh>ReduceTypes.is_known_type</vh></v>
<v t="ekr.20210809124613.133"><vh>ReduceTypes.reduce_collection</vh></v>
<v t="ekr.20210809124613.134"><vh>ReduceTypes.reduce_numbers</vh></v>
<v t="ekr.20210809124613.135"><vh>ReduceTypes.reduce_types</vh></v>
<v t="ekr.20210809124613.136"><vh>ReduceTypes.reduce_unknowns</vh></v>
<v t="ekr.20210809124613.137"><vh>ReduceTypes.show</vh></v>
<v t="ekr.20210809124613.138"><vh>ReduceTypes.split_types</vh></v>
</v>
<v t="ekr.20210809124613.139"><vh>class Stub</vh>
<v t="ekr.20210809124613.140"><vh>Stub.__init__</vh></v>
<v t="ekr.20210809124613.141"><vh>Stub.__eq__</vh></v>
<v t="ekr.20210809124613.142"><vh>Stub.__ne__</vh></v>
<v t="ekr.20210809124613.143"><vh>Stub.__hash__</vh></v>
<v t="ekr.20210809124613.144"><vh>Stub.__repr__</vh></v>
<v t="ekr.20210809124613.145"><vh>Stub.level</vh></v>
<v t="ekr.20210809124613.146"><vh>Stub.parents</vh></v>
</v>
<v t="ekr.20210809124613.147"><vh>class StubFormatter(AstFormatter)</vh>
<v t="ekr.20210809124613.148"><vh>StubFormatter(AstFormatter).__init__</vh></v>
<v t="ekr.20210809124613.149"><vh>StubFormatter(AstFormatter).match_all</vh></v>
<v t="ekr.20210809124613.150"><vh>StubFormatter(AstFormatter).trace_visitor</vh></v>
<v t="ekr.20210809124613.151"><vh>StubFormatter(AstFormatter).do_Attribute</vh></v>
<v t="ekr.20210809124613.152"><vh>StubFormatter(AstFormatter).do_Constant</vh></v>
<v t="ekr.20210809124613.153"><vh>StubFormatter(AstFormatter).do_Bytes</vh></v>
<v t="ekr.20210809124613.154"><vh>StubFormatter(AstFormatter).do_Num</vh></v>
<v t="ekr.20210809124613.155"><vh>StubFormatter(AstFormatter).do_Str</vh></v>
<v t="ekr.20210809124613.156"><vh>StubFormatter(AstFormatter).do_Dict</vh></v>
<v t="ekr.20210809124613.157"><vh>StubFormatter(AstFormatter).do_List</vh></v>
<v t="ekr.20210809124613.158"><vh>StubFormatter(AstFormatter).do_Name</vh></v>
<v t="ekr.20210809124613.159"><vh>StubFormatter(AstFormatter).do_Tuple</vh></v>
<v t="ekr.20210809124613.160"><vh>StubFormatter(AstFormatter).do_BinOp</vh></v>
<v t="ekr.20210809124613.161"><vh>StubFormatter(AstFormatter).do_BoolOp</vh></v>
<v t="ekr.20210809124613.162"><vh>StubFormatter(AstFormatter).do_Compare</vh></v>
<v t="ekr.20210809124613.163"><vh>StubFormatter(AstFormatter).do_IfExp</vh></v>
<v t="ekr.20210809124613.164"><vh>StubFormatter(AstFormatter).do_Subscript</vh></v>
<v t="ekr.20210809124613.165"><vh>StubFormatter(AstFormatter).do_UnaryOp</vh></v>
<v t="ekr.20210809124613.166"><vh>StubFormatter(AstFormatter).do_Call</vh></v>
<v t="ekr.20210809124613.167"><vh>StubFormatter(AstFormatter).do_Return</vh></v>
</v>
<v t="ekr.20210809124613.168"><vh>class StubTraverser(ast.NodeVisitor)</vh>
<v t="ekr.20210809124613.169"><vh>StubTraverser(ast.NodeVisitor).__init__</vh></v>
<v t="ekr.20210809124613.170"><vh>StubTraverser(ast.NodeVisitor).add_stub</vh></v>
<v t="ekr.20210809124613.171"><vh>StubTraverser(ast.NodeVisitor).indent</vh></v>
<v t="ekr.20210809124613.172"><vh>StubTraverser(ast.NodeVisitor).out</vh></v>
<v t="ekr.20210809124613.173"><vh>StubTraverser(ast.NodeVisitor).run</vh></v>
<v t="ekr.20210809124613.174"><vh>StubTraverser(ast.NodeVisitor).output_stubs</vh></v>
<v t="ekr.20210809124613.175"><vh>StubTraverser(ast.NodeVisitor).output_time_stamp</vh></v>
<v t="ekr.20210809124613.176"><vh>StubTraverser(ast.NodeVisitor).update</vh></v>
<v t="ekr.20210809124613.177"><vh>StubTraverser(ast.NodeVisitor).get_stub_file</vh></v>
<v t="ekr.20210809124613.178"><vh>StubTraverser(ast.NodeVisitor).parse_stub_file</vh></v>
<v t="ekr.20210809124613.179"><vh>StubTraverser(ast.NodeVisitor).merge_stubs</vh></v>
<v t="ekr.20210809124613.180"><vh>StubTraverser(ast.NodeVisitor).check_delete</vh></v>
<v t="ekr.20210809124613.181"><vh>StubTraverser(ast.NodeVisitor).flatten_stubs</vh></v>
<v t="ekr.20210809124613.182"><vh>StubTraverser(ast.NodeVisitor).flatten_stubs_helper</vh></v>
<v t="ekr.20210809124613.183"><vh>StubTraverser(ast.NodeVisitor).find_parent_stub</vh></v>
<v t="ekr.20210809124613.184"><vh>StubTraverser(ast.NodeVisitor).find_stub</vh></v>
<v t="ekr.20210809124613.185"><vh>StubTraverser(ast.NodeVisitor).sort_stubs_by_hierarchy</vh></v>
<v t="ekr.20210809124613.186"><vh>StubTraverser(ast.NodeVisitor).trace_stubs</vh></v>
<v t="ekr.20210809124613.187"><vh>StubTraverser(ast.NodeVisitor).visit_ClassDef</vh></v>
<v t="ekr.20210809124613.188"><vh>StubTraverser(ast.NodeVisitor).visit_FunctionDef</vh></v>
<v t="ekr.20210809124613.189"><vh>StubTraverser(ast.NodeVisitor).format_arguments</vh></v>
<v t="ekr.20210809124613.190"><vh>StubTraverser(ast.NodeVisitor).munge_arg</vh></v>
<v t="ekr.20210809124613.191"><vh>StubTraverser(ast.NodeVisitor).format_returns</vh></v>
<v t="ekr.20210809124613.192"><vh>StubTraverser(ast.NodeVisitor).format_return_expressions</vh></v>
<v t="ekr.20210809124613.193"><vh>StubTraverser(ast.NodeVisitor).get_def_name</vh></v>
<v t="ekr.20210809124613.194"><vh>StubTraverser(ast.NodeVisitor).remove_recursive_calls</vh></v>
<v t="ekr.20210809124613.195"><vh>StubTraverser(ast.NodeVisitor).visit_Return</vh></v>
</v>
<v t="ekr.20210809124613.196"><vh>class TestMakeStubFiles(unittest.TestCase)</vh>
<v t="ekr.20210809124613.197"><vh>TestMakeStubFiles(unittest.TestCase).test_rt_is_known_type</vh></v>
<v t="ekr.20210809124613.198"><vh>TestMakeStubFiles(unittest.TestCase).test_rt_reduce_numbers</vh></v>
<v t="ekr.20210809124613.199"><vh>TestMakeStubFiles(unittest.TestCase).test_rt_reduce_types</vh></v>
<v t="ekr.20210809124613.200"><vh>TestMakeStubFiles(unittest.TestCase).test_rt_split_types</vh></v>
<v t="ekr.20210809124613.201"><vh>TestMakeStubFiles(unittest.TestCase).test_st_find</vh></v>
<v t="ekr.20210809124613.202"><vh>TestMakeStubFiles(unittest.TestCase).test_st_flatten_stubs</vh></v>
<v t="ekr.20210809124613.203"><vh>TestMakeStubFiles(unittest.TestCase).test_st_merge_stubs</vh></v>
<v t="ekr.20210809124613.204"><vh>TestMakeStubFiles(unittest.TestCase).test_st_format_returns</vh></v>
<v t="ekr.20210809124613.205"><vh>TestMakeStubFiles(unittest.TestCase).test_bug2_empty</vh></v>
<v t="ekr.20210809124613.206"><vh>TestMakeStubFiles(unittest.TestCase).test_bug2_non_empty</vh></v>
<v t="ekr.20210809124613.207"><vh>TestMakeStubFiles(unittest.TestCase).test_bug3</vh></v>
<v t="ekr.20210809124613.208"><vh>TestMakeStubFiles(unittest.TestCase).test_finalize</vh></v>
<v t="ekr.20210809124613.209"><vh>TestMakeStubFiles(unittest.TestCase).test_is_known_type</vh></v>
<v t="ekr.20210809124613.210"><vh>TestMakeStubFiles(unittest.TestCase).test_truncate</vh></v>
<v t="ekr.20210809124613.211"><vh>TestMakeStubFiles(unittest.TestCase).test_ast_arg_formatter_class</vh></v>
<v t="ekr.20210809124613.212"><vh>TestMakeStubFiles(unittest.TestCase).test_ast_formatter_class</vh></v>
<v t="ekr.20210809124613.213"><vh>TestMakeStubFiles(unittest.TestCase).test_ast_formatter_class_on_file</vh></v>
<v t="ekr.20210809124613.214"><vh>TestMakeStubFiles(unittest.TestCase).test_controller_class</vh></v>
<v t="ekr.20210809124613.215"><vh>TestMakeStubFiles(unittest.TestCase).test_file_msb</vh></v>
<v t="ekr.20210809124613.216"><vh>TestMakeStubFiles(unittest.TestCase).test_pattern_class</vh></v>
<v t="ekr.20210809124613.217"><vh>TestMakeStubFiles(unittest.TestCase).test_stub_class</vh></v>
<v t="ekr.20210809124613.218"><vh>TestMakeStubFiles(unittest.TestCase).test_stub_formatter_class</vh></v>
<v t="ekr.20210809124613.219"><vh>TestMakeStubFiles(unittest.TestCase).test_stub_traverser_class</vh></v>
</v>
</v>
<v t="ekr.20210810102041.1"><vh>@file scripts/wax_off.py</vh></v>
<v t="ekr.20160318141204.1"><vh>@file make_stub_files.py</vh></v>
</v>
<v t="ekr.20210810053434.1"></v>
</vnodes>
<tnodes>
<t tx="ekr.20160126153220.1">@language config
# A configuration file to make stubs for make_stub_files.py itself.
[Global]
files: make_stub_files.py
output_directory: .
prefix_lines:
from typing import Any, Dict, Optional, Sequence, Tuple, Union
# At present, I don't understand how to tell mypy about ast.Node
# import ast
# Node = ast.Node
Node = Any
[Def Name Patterns]
# The returns are inherent in the design of make_stub_files.py:
AstFormatter.do_*: str
StubFormatter.do_*: str
StubTraverser.do_*: str
# Test of regex pattern.
.*__hash__$: int
# Pattern.all_matches: Sequence
# Pattern.full_balanced_match: Optional[int]
# Pattern.match_balanced: int
# Pattern.match_entire_string: bool
# StandAloneMakeStubFile.scan_types: Dict[str, str]
# StubTraverser.format_returns: str
# StubTraverser.match_return_patterns: Tuple[bool,str]
# StubTraverser.match_return_pattern: Optional[str]
# StubTraverser.match_balanced: int
[General Patterns]
# Declarations of names...
NotImplemented: bool
a: str
aList: List[Any]
comments: str
controller: StandAloneMakeStubFile
find_s: str
fn: str
found: str
general_patterns: List[Pattern]
group: str
i1: int
i2: int
i: int
j1: int
j2: int
j: int
n1: int
n2: int
n: int
name: str
ndots: int
node: Node
parser: optparse.OptionParser
patterns: List
repl_s: str
s: str
s1: str
s2: str
# s[1-2]?$: str
strict: bool
trace: bool
# New known functions
endswith(*): bool
# Known functions...
os.path.basename(*): str
os.sep.join(*): str
sep.join(*): str
self.__eq__(*): bool
self.__ne__(*): bool
self.__gt__(*): bool
self.__lt__(*): bool
all(*): bool
any(*): bool
int(*): int
hash(*): int
len(*): int
repr(*): str
sorted(*): str
str%(*): str
str.join(*): str
r[*]: str
# Put this in the code.
str[*]: str
###.*\.__name__$: str
###.*\.hash()$: int
</t>
<t tx="ekr.20160128102557.1"># An example configuration file for make_stub_files.py.
# By default, make_stub_files.py uses ~/stubs/make_stub_files.cfg.
# Can be changed using the --config=path command-line option.
[Global]
files:
# Files to be used *only* if no files are given on the command line.
# glob.glob wildcards are supported.
output_directory: ~/stubs
prefix_lines:
# Lines to be inserted at the start of each stub file.
from typing import TypeVar
T = TypeVar('T', int, float, complex)
# Notes about patterns used below:
#
# **Balanced patterns** contain either (*), [*], or {*}.
# Unlike regular expressions, balanced patterns match only balanced brackets.
#
# Both regex and balanced patterns may appear in each section.
# However, balanced patterns will never match argument names.
#
# Patterns are matched in the order they appear in each section,
# but the .* pattern (if present) will match last, regardless of its
# position in the section.
[Def Name Patterns]
# These regex patterns give the return types of functions or methods.
#
# Patterns for methods should match class_name.method_name.
#
# Patterns in this section *override* all other patterns,
# so you should use these patterns only if:
#
# - No other pattern properly handles the function or method, or
#
# - The pattern specifies functions that should all return the same value.
# For example, all ast tree traversers should have the same signatures.
#
# It may be unwise to use .* in this section, but the choice is yours.
[Argument Patterns]
# The regex patterns in this section apply only when assigning types
# to *arguments* to functions or methods. Patterns match argument names.
# Typically, most patterns can be put [General Patterns] section instead.
[General Patterns]
# The patterns in this section may be either regex or balanced patterns.
# Patterns in this section are applied both to arguments and return expressions.
# These patterns are applied *once* to argument names and *repeatedly* to
# return types until no further matches can be made.
aList[1-3]?: Sequence
i: int
j: int
k: int
node: ast.Ast
s[1-3]?: str
[Return Patterns]
# The patterns in this section may be either regex or balanced patterns.
# Patterns in this section are applied only to return expressions.
# These patterns are applied *repeatedly* to return expressions
# until no further matches can be made.
# Balanced patterns...
repr(*): str
str.join(*): str
str.replace(*): str
str%(*): str
str%str: str
# Regex patterns...
.*__name__: str
</t>
<t tx="ekr.20160128225533.1">*.pyc
*.pyi
test/*.pyc
__pycache__/
.mypy_cache/
.cache/
.coverage
htmlcov/
*.egg-info/
dist/
</t>
<t tx="ekr.20160207051429.1">def test_merge_types(self):
a, c, f, i, l, n = ('Any', 'complex', 'float', 'int', 'long', 'number')
none = 'None'
La, Lc = ['Any'], ['complex']
Lac, Lai, Lan = ['Any', 'complex'], ['Any', 'int'], ['Any', 'None']
Laci = ['Any', 'complex', 'int']
Lnone = ['None']
table = (
(none, Lnone, Lnone),
(none, none, Lnone),
(a, none, Lan),
(a, a, La),
(La, a, La),
(Lac, a, Lac),
(Lac, i, Laci),
(Lac, Lai, Laci),
)
for a1, a2, expected in table:
got = merge_types(a1, a2)
self.assertEqual(expected, got)
</t>
<t tx="ekr.20160207101607.1">@language rest
@wrap
This is the theory-of-operation document for the `make_stub_files` script. It is intentionally brief. Please [ask questions](#summary) if anything is unclear.
### Prerequisites
Maintainers should be familiar with the following:
- The [Python 3 ast class](https://docs.python.org/3/library/ast.html).
You should know what a tree traversal is.
- [Pep 484](https://www.python.org/dev/peps/pep-0484/) and
[Python's typing module](https://docs.python.org/3/library/typing.html).
Having a clear **target language** greatly simplifies this project.
You don't need to know anything about type inference.
### High level description
This script is a modified code formatter. This script traverses the incoming ast tree *once* from the top down, generating results from the bottom up. There is only a *single* traversal, composed of four traversal classes. (See [below](#traversers) for details). This traversal produces a stub for every class and def line. To do this, it **replaces expressions with type hints**. In other words, the goal is to **reduce** expressions to **known types**, as defined by Pep 484.
The StubFormatter visitors do most of the work of type reduction. They delegate type reduction to the following helpers:
1. **`ReduceTypes.reduce_types(aList)`** reduces a *list* of 0 or more types to a *string* representing a type hint. It returns 'Any' for unknown types. At the top of the traversal, StubTraverser.do_FunctionDef also calls reduce_types (via helpers) on the list of all return expressions.
2. **`StubFormatter.match_all(node, s)`** applies all user-patterns to s and returns the result.
3. **`ReduceTypes.is_known_type(s)`** embodies the known types as defined in Pep 484 and the typing module.
In short, visitors are hardly more complex than the corresponding AstFormatter methods.
**Notes**:
- The `sf.do_Attribute` and `sf.do_Name` visitors look up names in `sf.names_dict`. This is much faster than matching patterns.
- `sf.match_all` is very fast because it only applies patterns that *could possibly* match at the node being visited. Those patterns are:
self.patterns_dict.get(node.__class__.__name__, []) + self.regex_patterns
That is, all regex patterns are applied "everywhere" in return expressions.
- The startup code create `names_dict`, `patterns_dict` and `regex_patterns` data structures. That's all you have to know about the startup code.
- The Pattern class handles almost all details of pattern matching. This shields the rest of the code from knowledge of patterns. In particular, `sf.match_all` knows nothing about patterns.
### Examples
A few examples may make this script's operation clearer. The --trace-matches and --trace-reduce switches turn on detailed traces that show exactly when and where reductions happen, and what the resulting type hints are. These traces are the truth. Believe them, not words here.
Given the file truncate.py:
def truncate(s, n):
"""Return s truncated to n characters."""
return s if len(s) <= n else s[:n-3] + '...'
The script produces this output with the --verbose option in effect:
def truncate(s: str, n: int) -> str: ...
# 0: return s if len(s)<=n else s[:n-3]+'...'
# 0: return str
Here is the output with --trace-reduce --trace-matches in effect:
make_stub_files.py -c make_stub_files.cfg truncate.py -v -o --trace-reduce --trace-matches
callers pattern types ==> hint
======= ======= ========================
reduce_types: do_BinOp [int, number] ==> number
match_all: do_Subscript str[*]: str str[:number] ==> str
reduce_types: do_IfExp str] ==> str
Finally, here is *part* of the result of tracing make_stub_files.py itself::
context pattern types ==> hint
=============================== ================ =========================================================================
reduce_types: do_IfExp [bool, is_known_type(inner)] ==> ? Any
reduce_types: do_IfExp [bool, is_known_type(inner)] ==> ? Any
match_all: do_Call all(*): bool all(is_known_type(z.strip()) for z in... ==> bool
reduce_types: is_known_type [Any, bool] ==> Union[Any, bool]
match_all: do_Call sorted(*): str sorted(Set[r1+r2]) ==> str
reduce_types: show [show_helper(List[Any][:], known, str, str, bool)] ==> ? Any
match_all: do_Subscript r[*]: str r[number] ==> str
match_all: do_Call str.join(*): str str.join(str) ==> str
reduce_types: reduce_types [show(str), show(str, known=bool), show_helper(Li...] ==> ? Any
reduce_types: do_BinOp [int, number] ==> number
match_all: do_Subscript str[*]: str str[:number] ==> str
reduce_types: do_IfExp [str] ==> str
class AstFormatter
reduce_types: do_BoolOp [val, val.strip()] ==> ? Any
reduce_types: do_BoolOp [Any, str] ==> Union[Any, str]
reduce_types: visit [str] ==> str
reduce_types: do_IfExp [str] ==> str
match_all: do_Call repr(*): str repr(Node.n) ==> str
reduce_types: get_import_names [result] ==> ? Any
reduce_types: kind [Node.__class__.__name__] ==> ? Any
This trace contains all essential data concerning pattern matching and type reduction.
Enable tracing in various visitors if you need more data.
<a name="traversers"/>
### Traversers
As stated above, this script traverses the parse tree *once*, using four different traversal classes. Each traverser produces the results needed at a particular point of the traversal. Imo, using separate traversal classes is good style, even though it would be straightforward to use a single class. Indeed, each class has a distinct purpose...
#### AstFormatter
This is the base formatter class. It defines the default formatting for each kind of node. More importantly, it emphasizes that subclasses must return strings, *never* lists. The `AstFormatter.visit` method checks that this is so. This assertion guarantees that subclasses must call `st.reduce_types` to convert a list of possible types into a single string representing their union.
#### StubTraverser
This class drives the traversal. It is a subclass of ast.NodeVisitor. No custom visit method is needed. Visitors are *not* formatters--they *use* formatters to produce stubs. This class overrides only the visitors for ClassDef, FunctionDef and Return ast nodes. The FunctionDef visitor invokes the StubFormatter class to format all the functions return statements. The FunctionDef visitor invokes the AstArgFormatter to format names in argument lists.
#### StubFormatter
This class formats return statements. The overridden visitors of this class replace constants and operators with their corresponding type hints. The do_BinOp method contains hard-coded patterns for creating type hints. More could be added. The script is truly simple because the visitor methods of this class are hardly more complex than the corresponding methods of the AstFormatter class.
### AstArgFormatter
This class works just like the StubFormatter class except that does *not* apply patterns to Name nodes. As the name implies, it is used to format arguments in function definitions. It could easily be merged into the StubFormatter class, but imo having a separate class is cleaner and even a bit safer.
### Unit testing
August, 2021: make_stub_files.py now contains traditional unit tests. See the TestMakeStubFiles class.
Run these unit tests with:
cd make_stub_files
python -m unittest make_stub_files
Run coverage tests with:
cd make_stub_files
python -m pytest --cov-report html --cov-report term-missing --cov make_stub_files make_stub_files.py
<a name="summary"/>
### Summary
make_stub_files.py is a straightforward tree traversal. Or so it seems to me.
Please feel free to ask questions.
Edward K. Ream
<t tx="ekr.20160207181637.1"></t>
<t tx="ekr.20160207181648.1"></t>
<t tx="ekr.20160207182535.1"></t>
<t tx="ekr.20160211110739.1">@language rest
@wrap
This is the readme file for `make_stub_files.py (msf)`. This file explains what msf does and how it works.
Are you sure you want to use this program? For python 3 programs, including mypy annotations directly in your sources is a better alternative. Leo's new add-mypy-annotations command handles most of the tedious details of inserting annotations.
Also, several other tools create mypy stubs:
- [MonkeyType](https://monkeytype.readthedocs.io/en/latest/index.html) (Python 3),
- [PyAnnotate](https://github.com/dropbox/pyannotate),
- [stubgen](https://mypy.readthedocs.io/en/stable/stubgen.html).
After a brief overview, a step-by-step section will get you started. Full source code for make_stub_files.py is in its [github repository](https://github.com/edreamleo/make-stub-files). Everything is in the public domain.
@others</t>
<t tx="ekr.20160211110807.1">
### Overview
This script makes a stub (.pyi) file in the **output directory** for each **source file** listed on the command line (wildcard file names are supported). This script never creates directories automatically, nor does it overwrite stub files unless the --overwrite command-line option is in effect.
GvR says, "We actually do have a [stub generator](https://github.com/JukkaL/mypy/blob/master/mypy/stubgen.py) as part of mypy now (it has a few options) but yours has the advantage of providing a way to tune the generated signatures...This allows for a nice iterative way of developing stubs."
The script does no type inference. Instead, the user supplies **patterns** in a configuration file. The script matches these patterns to:
1. The names of arguments in functions and methods and
2. The text of **return expressions**. Return expressions are the actual text of whatever follows the "return" keyword. The script removes all comments in return expressions and converts all strings to "str". This **preprocessing** greatly simplifies pattern matching.
As a first example, given the method:
def foo(self, i, s):
if i:
return "abc" # a comment
else:
return s
and the patterns:
i: int
s: str
the script produces the stub:
def foo(i: int, s: str) --> str: ...
`make_stub_files` eliminates much of the drudgery of creating [python stub (.pyi) files](https://www.python.org/dev/peps/pep-0484/#stub-files) from python source files. Stub files can be used by people who use Python 2.x code bases.
</t>
<t tx="ekr.20160211110810.1">
### Command-line arguments
usage: make_stub_files.py [options] file1, file2, ...
make_stub_file: Create stub (.pyi) files from python files
positional arguments:
FILE input files
optional arguments:
-h, --help show this help message and exit
-c FILE, --config FILE
full path to configuration file
-d DIR, --dir DIR full path to the output directory
-f, --force-pyx force the parsing of .pyx files
-o, --overwrite overwrite existing stub (.pyi) files
-s, --silent run without messages
--trace-matches trace Pattern.matches
--trace-patterns trace pattern creation
--trace-reduce trace st.reduce_types
--trace-visitors trace visitor methods
-u, --update update stubs in existing stub file
-v, --verbose verbose output in .pyi file
-w, --warn warn about unannotated args
*Note*: glob.glob wildcards can be used in file1, file2, ...
</t>
<t tx="ekr.20160211110810.2">
### The configuration file
The --config command-line option specifies the full path to the optional configuration file. The configuration file uses the .ini format. It has several configuration sections, all optional.
</t>
<t tx="ekr.20160211110810.3">
### Why this script is important
The script eliminates most of the drudgery from creating stub files. The script produces syntactically and semantically correct stub files without any patterns at all. Patterns make it easy to make stubs more specific.
Once we create stub files, mypy will check them by doing real type inference. This will find errors both in the stub files and in the program under test. There is now an easy way to use mypy!
Stubs express design intentions and intuitions as well as types. Until now, there has been no practical way of expressing and *testing* these assumptions. Now there is.
Using mypy, we can be as specific as we like about types. We can simply annotate that d is a dict, or we can say that d is a dict whose keys are strings and whose values are executables with a union of possible signatures. Stubs are the easy way to play with type inference.
Finally, stubs can simplify the general type inference problem. Without type hints or annotations, the type of everything depends on the type of everything else. Stubs could allow robust, maybe even complete, type inference to be done locally. Stubs help mypy to work faster.
</t>
<t tx="ekr.20160211110811.1">
### Summary
The make-stub-files script does for type/design analysis what Leo's c2py command did for converting C sources to python. It eliminates much of the drudgery associated with creating stub files, leaving the programmer to make non-trivial inferences.
Stub files allow us to explore type checking using mypy as a guide and helper. Stub files are both a design document and an executable, checkable, type specification. Stub files allow those with a Python 2 code base to use mypy.
One could imagine a similar insert_annotations script that would inject function annotations into source files using stub files as data. The "reverse" script should be more straightforward than this script.
Edward K. Ream
January 25 to February 15, 2016
Revised, August 5, 2021.
</t>
<t tx="ekr.20160211111807.1">
#### Patterns
The [Def Name Patterns] and [General Patterns] configuration sections
specify patterns. All patterns have the form:
find-string: replacement-string
Colons are not allowed in the find-string. This is a limitation of .ini files.
There are three kinds of patterns: balanced, regex and plain.
**Balanced patterns** are patterns whose find string that:
A: contain either `(*)`, `[*]`, or `{*}` or
B: ends with `*`.
Unlike regular expressions, `(*)`, `[*]`, or `{*}` match only
balanced brackets. A trailing `*` matches the rest of the string.
Examples:
str(*): str
StubTraverser.do_*
Balanced patterns such as:
[*]: List[*]
work as expected. The script replaces the `*` in replacement-strings with
whatever matched `*` in the find-string.
**Regex patterns** (regular expression patterns) are denoted by a
find-string that ends with `$`. The trailing `$` does not become part of
the find-string. For example:
ab(.*)de$: de\1\1ab
A pattern is a **plain pattern** if it is neither a balanced nor a regex
pattern.
The script matches patterns to *all parts* of return expressions.
*Important*: The script applies patterns *separately* to each return
expression. Comments never appear in return expressions, and all strings in
return values appear as str. As a result, there is no context to worry
about context in which patterns are matched. Very short patterns suffice.
</t>
<t tx="ekr.20160211111823.1">
#### [Global]
This configuration section specifies the files list, prefix lines and
output directory. For example:
[Global]
files:
# Files to be used *only* if no files are given on the command line.
# glob.glob wildcards are supported.
~/leo-editor/leo/core/*.py
output_directory:
# The output directory to be used if no --dir option is given.
~/stubs
prefix:
# Lines to be inserted at the start of each stub file.
from typing import TypeVar, Iterable, Tuple
T = TypeVar('T', int, float, complex)
</t>
<t tx="ekr.20160211111839.1">
#### [Def Name Patterns]
The script matches the find-strings in this section against names of
functions and methods. For methods, the script matches find-strings against
names of the form:
class_name.method_name
When a find-string matches, the replacement-string becomes the return type
in the stub, without any further pattern matching. That is, this section
*overrides* [General Patterns].
Example 1:
[Def Name Patterns]
myFunction: List[str]
Any function named myFunction returns List[str].
Example 2:
[Def Name Patterns]
MyClass.myMethod: str
The myMethod method of the MyClass class returns str.
Example 3:
[Def Name Patterns]
MyClass.do_*: str
All methods of the MyClass class whose names start with "do_" return str.
</t>
<t tx="ekr.20160211111901.1">
#### [General Patterns]
For each function or method, the script matches the patterns in this
section against **all parts** of all return expressions in each function or method.
The intent of the patterns in this section should be to **reduce** return
expressions to **known types**. A known type is a either a name of a type
class, such as int, str, long, etc. or a **type hint**, as per
[Pep 484](https://www.python.org/dev/peps/pep-0484/).
The script *always* produces a syntactically correct stub, even if the
patterns do not reduce the return expression to a known type. For unknown
types, the script does the following:
1. Uses Any as the type of the function or method.
2. Follows the stub with a list of comments giving all the return
expressions in the function or method.
For example, suppose that the patterns are not sufficient to resolve the
return type of:
def foo(a):
if a:
return a+frungify(a)
else:
return defrungify(a)
The script will create this stub:
def foo(a) --> Any: ...
# return a+frungify(a)
# return defrungify(a)
The comments preserve maximal information about return types, which should
help the user to supply a more specific return type. The user can do this
in two ways by altering the stub files by hand or by adding new patterns to
the config file.
</t>
<t tx="ekr.20160211113019.1">
### Quick Start
1. Put `make_stub_files.py` on your path.
2. Enter a directory containing .py files:
cd myDirectory
3. Generate stubs for foo.py in foo.pyi:
make_stub_files foo.py
4. Look at foo.pyi to see the generated stubs.
5. Regenerate foo.pyi with more verbose output:
make_stub_files foo.py -o -v
The -o (--overwrite) option allows the script to overwrite foo.pyi.
The -v (--verbose) options generates return comments for all stubs in foo.pyi.
6. Update foo.pyi:
make_stub_files -o -u
The -u (--update) options updates foo.pyi as follows:
- adds stubs to foo.pyi for classes and defs that are new in foo.py.
- deletes stubs in foo.pyi for classes and defs that no longer exist in foo.py.
- leaves all other stubs in foo.pyi unchanged.
7. Specify a configuration file containing patterns:
make_stub_files -c myConfigFile.cfg -o
</t>
<t tx="ekr.20160318141204.167"># UnaryOp(unaryop op, expr operand)
def do_UnaryOp(self, node: Node) -> str:
"""StubFormatter.UnaryOp for unary +, -, ~ and 'not' operators."""
op = self.op_name(node.op)
if op.strip() == 'not':
return 'bool'
s = op + self.visit(node.operand) # bug fix: 2021/08/07.
s = self.match_all(node, s)
self.trace_visitor(node, op, s)
return s
</t>
<t tx="ekr.20160318141204.168">def do_Return(self, node: Node) -> str:
"""
StubFormatter ast.Return vsitor.
Return only the return expression itself.
"""
s = AstFormatter.do_Return(self, node)
s = s.strip()
assert s.startswith('return'), repr(s)
return s[len('return'):].strip()
</t>
<t tx="ekr.20160318141204.18">def visit(self, node: Node) -> str:
"""Return the formatted version of an Ast node, or list of Ast nodes."""
tag = 'AstFormatter.visit'
name = node.__class__.__name__