29
29
import shlex
30
30
import subprocess
31
31
import sys
32
- import types
33
32
from ast import AST
34
33
from contextlib import redirect_stdout
35
- from typing import List , Dict , Any , Union , Tuple , Optional , TypedDict
34
+ from typing import List , Dict , Any , Union , Tuple , Optional , TypedDict , Callable
35
+ from types import TracebackType , MethodType , FunctionType
36
36
37
- def sh (cmd ) :
37
+ def sh (cmd : str ) -> str :
38
38
r = subprocess .run (
39
39
shlex .split (cmd ),
40
40
stdout = subprocess .PIPE ,
@@ -63,7 +63,7 @@ def sh(cmd):
63
63
class Stack :
64
64
line_numbers : Dict [Tuple [str , str ], int ] = {}
65
65
66
- def __init__ (self , tb ):
66
+ def __init__ (self , tb : Optional [ TracebackType ] ):
67
67
self .stack = []
68
68
self .stack_idx = 0
69
69
while tb :
@@ -79,13 +79,13 @@ def __init__(self, tb):
79
79
if self .stack_top >= 0 :
80
80
self .set_frame (self .stack_top )
81
81
82
- def frame_string (self , i ) :
82
+ def frame_string (self , i : int ) -> str :
83
83
(fname , line , f ) = self .stack [i ]
84
84
res = " File \" %s\" , line %d, Frame [%d/%d] (%s):" % (
85
85
f .f_code .co_filename , line , i , self .stack_top , f .f_code .co_name )
86
86
return res
87
87
88
- def __repr__ (self ):
88
+ def __repr__ (self ) -> str :
89
89
frames = []
90
90
for i in range (self .stack_top + 1 ):
91
91
s = self .frame_string (i )
@@ -94,7 +94,7 @@ def __repr__(self):
94
94
frames .append (s )
95
95
return "\n " .join (frames )
96
96
97
- def set_frame (self , i ) :
97
+ def set_frame (self , i : int ) -> None :
98
98
if i >= 0 :
99
99
f = self .stack [i ][2 ]
100
100
self .stack_idx = i
@@ -107,15 +107,15 @@ def set_frame(self, i):
107
107
108
108
print (self .frame_string (self .stack_idx ))
109
109
110
- def up (self , delta = 1 ) :
110
+ def up (self , delta : int = 1 ) -> None :
111
111
if self .stack_idx <= 0 :
112
112
if self .stack :
113
113
print (self .frame_string (self .stack_idx ))
114
114
else :
115
115
self .stack_idx = max (self .stack_idx - delta , 0 )
116
116
self .set_frame (self .stack_idx )
117
117
118
- def down (self , delta = 1 ) :
118
+ def down (self , delta : int = 1 ) -> None :
119
119
if self .stack_idx >= self .stack_top :
120
120
if self .stack :
121
121
print (self .frame_string (self .stack_idx ))
@@ -124,21 +124,21 @@ def down(self, delta=1):
124
124
self .set_frame (self .stack_idx )
125
125
126
126
class Autocall :
127
- def __init__ (self , f ):
127
+ def __init__ (self , f : Callable ):
128
128
self .f = f
129
129
130
- def __call__ (self , n ) :
130
+ def __call__ (self , n : Any ) -> None :
131
131
self .f (n )
132
132
133
- def __repr__ (self ):
133
+ def __repr__ (self ) -> str :
134
134
try :
135
135
self .f ()
136
136
except :
137
137
pass
138
138
return ""
139
139
140
140
#* Functions
141
- def chfile (f ) :
141
+ def chfile (f : str ) -> None :
142
142
tf = top_level ()
143
143
tf .f_globals ["__file__" ] = f
144
144
d = os .path .dirname (f )
@@ -147,36 +147,31 @@ def chfile(f):
147
147
except :
148
148
pass
149
149
150
- def format_arg (arg_pair ):
151
- name , default_value = arg_pair
152
- if default_value :
153
- return name + " = " + default_value
154
- else :
155
- return name
156
-
157
- def arglist (sym ):
150
+ def arglist (sym : Callable ) -> List [str ]:
151
+ def format_arg (arg_pair : Tuple [str , Optional [str ]]) -> str :
152
+ name , default_value = arg_pair
153
+ if default_value :
154
+ return name + " = " + default_value
155
+ else :
156
+ return name
158
157
arg_info = inspect .getfullargspec (sym )
159
158
if "self" in arg_info .args :
160
159
arg_info .args .remove ("self" )
161
160
if arg_info .defaults :
162
- defaults = (
163
- [None ] * (len (arg_info .args ) - len (arg_info .defaults )) +
164
- [repr (x ) for x in arg_info .defaults ])
161
+ defaults : List [Optional [str ]] = [None ] * (len (arg_info .args ) - len (arg_info .defaults ))
162
+ defaults += [repr (x ) for x in arg_info .defaults ]
165
163
args = [format_arg (x ) for x in zip (arg_info .args , defaults )]
166
164
else :
167
165
args = arg_info .args
168
166
if arg_info .varargs :
169
167
args += arg_info .varargs
170
168
keywords = arg_info .kwonlydefaults
171
169
if keywords :
172
- if type (keywords ) is dict :
173
- for k , v in keywords .items ():
174
- args .append (f"{ k } = { v } " )
175
- else :
176
- args .append ("**" + keywords )
170
+ for k , v in keywords .items ():
171
+ args .append (f"{ k } = { v } " )
177
172
return args
178
173
179
- def print_elisp (obj , end = "\n " ):
174
+ def print_elisp (obj : Any , end : str = "\n " ) -> None :
180
175
if hasattr (obj , "_asdict" ) and obj ._asdict is not None :
181
176
# namedtuple
182
177
try :
@@ -302,7 +297,7 @@ def list_step(varname, lst):
302
297
f_globals [varname ] = val
303
298
return val
304
299
305
- def argv (cmd ) :
300
+ def argv (cmd : str ) -> None :
306
301
sys .argv = shlex .split (cmd )
307
302
308
303
def find_global_vars (class_name ):
@@ -320,11 +315,11 @@ def rebind(method, fname=None, line=None):
320
315
(cls_name , fun_name ) = qname .split ("." )
321
316
for (n , v ) in find_global_vars (cls_name ):
322
317
print ("rebind:" , n )
323
- top_level ().f_globals [n ].__dict__ [fun_name ] = types . MethodType (top_level ().f_globals [fun_name ], v )
318
+ top_level ().f_globals [n ].__dict__ [fun_name ] = MethodType (top_level ().f_globals [fun_name ], v )
324
319
if fname and line :
325
320
Stack .line_numbers [(fname , qname )] = line
326
321
327
- def pm ():
322
+ def pm () -> None :
328
323
"""Post mortem: recover the locals and globals from the last traceback."""
329
324
if hasattr (sys , 'last_traceback' ):
330
325
stack = Stack (sys .last_traceback )
@@ -335,7 +330,7 @@ def pm():
335
330
tl .f_globals ["dn" ] = Autocall (stack .down )
336
331
globals ()["stack" ] = stack
337
332
338
- def pprint (x ) :
333
+ def pprint (x : Any ) -> None :
339
334
r1 = repr (x )
340
335
if len (r1 ) > 1000 and repr1 :
341
336
print (repr1 .repr (x ))
@@ -345,7 +340,7 @@ def pprint(x):
345
340
else :
346
341
pp .PrettyPrinter (width = 200 ).pprint (x )
347
342
348
- def to_str (x ) :
343
+ def to_str (x : Any ) -> str :
349
344
with io .StringIO () as buf , redirect_stdout (buf ):
350
345
pprint (x )
351
346
return buf .getvalue ().strip ()
@@ -357,7 +352,7 @@ def step_in(fn, *args):
357
352
f_globals [arg_name ] = arg_val
358
353
359
354
def step_into_module_maybe (module ):
360
- if isinstance (module , types . FunctionType ):
355
+ if isinstance (module , FunctionType ):
361
356
try :
362
357
module = sys .modules [module .__module__ ]
363
358
except :
0 commit comments