11import ast
2+ import sys
23from typing import List , Tuple , Generator , Type , Any , Optional
34
45from flake8_debug .errors import ERRORS , Error
56from flake8_debug .meta import Meta
67
78TDebug = Generator [Tuple [int , int , str , Type [Any ]], None , None ]
89
10+ # Func names meaningful only as attribute calls (e.g. pdb.set_trace())
11+ _ATTR_DETECTABLE : frozenset = frozenset ({'set_trace' })
12+ # Only flag attribute calls when the object looks like a known debugger module
13+ _DEBUGGER_MODULES : frozenset = frozenset ({'pdb' , 'ipdb' })
14+
915
1016class DebugVisitor (ast .NodeVisitor ):
1117 def __init__ (self , errors : Tuple [Type [Error ], ...]) -> None :
@@ -14,14 +20,20 @@ def __init__(self, errors: Tuple[Type[Error], ...]) -> None:
1420
1521 def visit_Call (self , node : ast .Call ) -> None :
1622 for error in self ._errors :
17- if (
23+ is_bare_call = (
1824 isinstance (node .func , ast .Name )
1925 and node .func .id == error .func_name
20- ) or (
26+ )
27+ is_attr_call = (
2128 isinstance (node .func , ast .Attribute )
2229 and node .func .attr == error .func_name
23- ):
30+ and error .func_name in _ATTR_DETECTABLE
31+ and isinstance (node .func .value , ast .Name )
32+ and node .func .value .id in _DEBUGGER_MODULES
33+ )
34+ if is_bare_call or is_attr_call :
2435 self .issues .append ((node .lineno , node .col_offset , error ().msg ))
36+ self .generic_visit (node )
2537
2638
2739class NoDebug :
@@ -32,10 +44,16 @@ def __init__(
3244 self , tree : ast .Module , filename : Optional [str ] = None
3345 ) -> None :
3446 self ._tree = tree
35- self ._filename = filename
3647
3748 def run (self ) -> TDebug :
3849 debug = DebugVisitor (ERRORS )
39- debug .visit (self ._tree )
50+ old_limit = sys .getrecursionlimit ()
51+ try :
52+ sys .setrecursionlimit (max (old_limit , 2000 ))
53+ debug .visit (self ._tree )
54+ except RecursionError :
55+ pass
56+ finally :
57+ sys .setrecursionlimit (old_limit )
4058 for lineno , column , msg in debug .issues : # type: int, int, str
4159 yield lineno , column , msg , type (self )
0 commit comments