2121}
2222
2323
24- BuiltinTypeCall = collections .namedtuple ('BuiltinTypeCall ' , ['name' , 'line' , 'column' ])
24+ Call = collections .namedtuple ('Call ' , ['name' , 'line' , 'column' ])
2525
2626
27- class BuiltinTypeVisitor (ast .NodeVisitor ):
27+ class Visitor (ast .NodeVisitor ):
2828 def __init__ (self , ignore = None , allow_dict_kwargs = True ):
2929 # type: (Optional[Sequence[str]], bool) -> None
30- self .builtin_type_calls = [] # type: List[BuiltinTypeCall ]
30+ self .builtin_type_calls = [] # type: List[Call ]
3131 self .ignore = set (ignore ) if ignore else set ()
3232 self .allow_dict_kwargs = allow_dict_kwargs
3333
3434 def _check_dict_call (self , node ): # type: (ast.Call) -> bool
35-
36- return self .allow_dict_kwargs and (getattr (node , 'kwargs' , None ) or getattr (node , 'keywords' , None ))
35+ return (
36+ self .allow_dict_kwargs and
37+ (getattr (node , 'kwargs' , None ) or getattr (node , 'keywords' , None ))
38+ )
3739
3840 def visit_Call (self , node ): # type: (ast.Call) -> None
39-
4041 if not isinstance (node .func , ast .Name ):
4142 # Ignore functions that are object attributes (`foo.bar()`).
4243 # Assume that if the user calls `builtins.list()`, they know what
@@ -49,15 +50,15 @@ def visit_Call(self, node): # type: (ast.Call) -> None
4950 elif node .args :
5051 return
5152 self .builtin_type_calls .append (
52- BuiltinTypeCall (node .func .id , node .lineno , node .col_offset ),
53+ Call (node .func .id , node .lineno , node .col_offset ),
5354 )
5455
5556
56- def check_file_for_builtin_type_constructors (filename , ignore = None , allow_dict_kwargs = True ):
57- # type: (str, Optional[Sequence[str]], bool) -> List[BuiltinTypeCall ]
57+ def check_file (filename , ignore = None , allow_dict_kwargs = True ):
58+ # type: (str, Optional[Sequence[str]], bool) -> List[Call ]
5859 with open (filename , 'rb' ) as f :
5960 tree = ast .parse (f .read (), filename = filename )
60- visitor = BuiltinTypeVisitor (ignore = ignore , allow_dict_kwargs = allow_dict_kwargs )
61+ visitor = Visitor (ignore = ignore , allow_dict_kwargs = allow_dict_kwargs )
6162 visitor .visit (tree )
6263 return visitor .builtin_type_calls
6364
@@ -73,14 +74,17 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
7374
7475 mutex = parser .add_mutually_exclusive_group (required = False )
7576 mutex .add_argument ('--allow-dict-kwargs' , action = 'store_true' )
76- mutex .add_argument ('--no-allow-dict-kwargs' , dest = 'allow_dict_kwargs' , action = 'store_false' )
77+ mutex .add_argument (
78+ '--no-allow-dict-kwargs' ,
79+ dest = 'allow_dict_kwargs' , action = 'store_false' ,
80+ )
7781 mutex .set_defaults (allow_dict_kwargs = True )
7882
7983 args = parser .parse_args (argv )
8084
8185 rc = 0
8286 for filename in args .filenames :
83- calls = check_file_for_builtin_type_constructors (
87+ calls = check_file (
8488 filename ,
8589 ignore = args .ignore ,
8690 allow_dict_kwargs = args .allow_dict_kwargs ,
@@ -89,7 +93,8 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
8993 rc = rc or 1
9094 for call in calls :
9195 print (
92- '{filename}:{call.line}:{call.column} - Replace {call.name}() with {replacement}' .format (
96+ '{filename}:{call.line}:{call.column}: '
97+ 'replace {call.name}() with {replacement}' .format (
9398 filename = filename ,
9499 call = call ,
95100 replacement = BUILTIN_TYPES [call .name ],
0 commit comments