1010MYPY = False
1111if MYPY :
1212 from types import FrameType
13- from typing import Any , Generator , List , Optional
13+ from typing import Any , Generator , List , Optional , Union
1414
1515pformat = PrettyFormat (
1616 indent_step = int (os .getenv ('PY_DEVTOOLS_INDENT' , 4 )),
1717 simple_cutoff = int (os .getenv ('PY_DEVTOOLS_SIMPLE_CUTOFF' , 10 )),
1818 width = int (os .getenv ('PY_DEVTOOLS_WIDTH' , 120 )),
1919 yield_from_generators = env_true ('PY_DEVTOOLS_YIELD_FROM_GEN' , True ),
2020)
21+ # required for type hinting because I (stupidly) added methods called `str`
22+ StrType = str
2123
2224
2325class DebugArgument :
2426 __slots__ = 'value' , 'name' , 'extra'
2527
26- def __init__ (self , value , * , name = None , ** extra ) :
28+ def __init__ (self , value : 'Any' , * , name : 'Optional[str]' = None , ** extra : 'Any' ) -> None :
2729 self .value = value
2830 self .name = name
2931 self .extra = []
@@ -35,7 +37,7 @@ def __init__(self, value, *, name=None, **extra):
3537 self .extra .append (('len' , length ))
3638 self .extra += [(k , v ) for k , v in extra .items () if v is not None ]
3739
38- def str (self , highlight = False ) -> str :
40+ def str (self , highlight : bool = False ) -> StrType :
3941 s = ''
4042 if self .name and not is_literal (self .name ):
4143 s = f'{ sformat (self .name , sformat .blue , apply = highlight )} : '
@@ -54,7 +56,7 @@ def str(self, highlight=False) -> str:
5456 s += suffix
5557 return s
5658
57- def __str__ (self ) -> str :
59+ def __str__ (self ) -> StrType :
5860 return self .str ()
5961
6062
@@ -66,14 +68,22 @@ class DebugOutput:
6668 arg_class = DebugArgument
6769 __slots__ = 'filename' , 'lineno' , 'frame' , 'arguments' , 'warning'
6870
69- def __init__ (self , * , filename : str , lineno : int , frame : str , arguments : 'List[DebugArgument]' , warning = None ):
71+ def __init__ (
72+ self ,
73+ * ,
74+ filename : str ,
75+ lineno : int ,
76+ frame : str ,
77+ arguments : 'List[DebugArgument]' ,
78+ warning : 'Union[None, str, bool]' = None ,
79+ ) -> None :
7080 self .filename = filename
7181 self .lineno = lineno
7282 self .frame = frame
7383 self .arguments = arguments
7484 self .warning = warning
7585
76- def str (self , highlight = False ) -> str :
86+ def str (self , highlight : bool = False ) -> StrType :
7787 if highlight :
7888 prefix = (
7989 f'{ sformat (self .filename , sformat .magenta )} :{ sformat (self .lineno , sformat .green )} '
@@ -87,10 +97,10 @@ def str(self, highlight=False) -> str:
8797 prefix += f' ({ self .warning } )'
8898 return f'{ prefix } \n ' + '\n ' .join (a .str (highlight ) for a in self .arguments )
8999
90- def __str__ (self ) -> str :
100+ def __str__ (self ) -> StrType :
91101 return self .str ()
92102
93- def __repr__ (self ) -> str :
103+ def __repr__ (self ) -> StrType :
94104 arguments = ' ' .join (str (a ) for a in self .arguments )
95105 return f'<DebugOutput { self .filename } :{ self .lineno } { self .frame } arguments: { arguments } >'
96106
@@ -102,7 +112,7 @@ def __init__(self, *, warnings: 'Optional[bool]' = None, highlight: 'Optional[bo
102112 self ._show_warnings = env_bool (warnings , 'PY_DEVTOOLS_WARNINGS' , True )
103113 self ._highlight = highlight
104114
105- def __call__ (self , * args , file_ = None , flush_ = True , ** kwargs ) -> 'Any' :
115+ def __call__ (self , * args : 'Any' , file_ : 'Any' = None , flush_ : bool = True , ** kwargs : 'Any' ) -> 'Any' :
106116 d_out = self ._process (args , kwargs )
107117 s = d_out .str (use_highlight (self ._highlight , file_ ))
108118 print (s , file = file_ , flush = flush_ )
@@ -113,18 +123,18 @@ def __call__(self, *args, file_=None, flush_=True, **kwargs) -> 'Any':
113123 else :
114124 return args
115125
116- def format (self , * args , ** kwargs ) -> DebugOutput :
126+ def format (self , * args : 'Any' , ** kwargs : 'Any' ) -> DebugOutput :
117127 return self ._process (args , kwargs )
118128
119- def breakpoint (self ):
129+ def breakpoint (self ) -> None :
120130 import pdb
121131
122132 pdb .Pdb (skip = ['devtools.*' ]).set_trace ()
123133
124- def timer (self , name = None , * , verbose = True , file = None , dp = 3 ) -> Timer :
134+ def timer (self , name : 'Optional[str]' = None , * , verbose : bool = True , file : 'Any' = None , dp : int = 3 ) -> Timer :
125135 return Timer (name = name , verbose = verbose , file = file , dp = dp )
126136
127- def _process (self , args , kwargs ) -> DebugOutput :
137+ def _process (self , args : 'Any' , kwargs : 'Any' ) -> DebugOutput :
128138 """
129139 BEWARE: this must be called from a function exactly 2 levels below the top of the stack.
130140 """
@@ -181,13 +191,13 @@ def _process(self, args, kwargs) -> DebugOutput:
181191 warning = self ._show_warnings and warning ,
182192 )
183193
184- def _args_inspection_failed (self , args , kwargs ) :
194+ def _args_inspection_failed (self , args : 'Any' , kwargs : 'Any' ) -> 'Generator[DebugArgument, None, None]' :
185195 for arg in args :
186196 yield self .output_class .arg_class (arg )
187197 for name , value in kwargs .items ():
188198 yield self .output_class .arg_class (value , name = name )
189199
190- def _process_args (self , ex , args , kwargs ) -> 'Generator[DebugArgument, None, None]' :
200+ def _process_args (self , ex : 'Any' , args : 'Any' , kwargs : 'Any' ) -> 'Generator[DebugArgument, None, None]' :
191201 import ast
192202
193203 func_ast = ex .node
0 commit comments