1- from __future__ import unicode_literals
2-
31import argparse
42import ast
5- import collections
6- import sys
73from typing import List
4+ from typing import NamedTuple
85from typing import Optional
96from typing import Sequence
107from typing import Set
2118}
2219
2320
24- Call = collections .namedtuple ('Call' , ['name' , 'line' , 'column' ])
21+ class Call (NamedTuple ):
22+ name : str
23+ line : int
24+ column : int
2525
2626
2727class Visitor (ast .NodeVisitor ):
28- def __init__ (self , ignore = None , allow_dict_kwargs = True ):
29- # type: (Optional[Sequence[str]], bool) -> None
30- self .builtin_type_calls = [] # type: List[Call]
28+ def __init__ (
29+ self ,
30+ ignore : Optional [Sequence [str ]] = None ,
31+ allow_dict_kwargs : bool = True ,
32+ ) -> None :
33+ self .builtin_type_calls : List [Call ] = []
3134 self .ignore = set (ignore ) if ignore else set ()
3235 self .allow_dict_kwargs = allow_dict_kwargs
3336
34- def _check_dict_call (self , node ): # type: (ast.Call) -> bool
35- return (
36- self .allow_dict_kwargs and
37- (getattr (node , 'kwargs' , None ) or getattr (node , 'keywords' , None ))
38- )
37+ def _check_dict_call (self , node : ast .Call ) -> bool :
38+ return self .allow_dict_kwargs and bool (node .keywords )
3939
40- def visit_Call (self , node ): # type: ( ast.Call) -> None
40+ def visit_Call (self , node : ast .Call ) -> None :
4141 if not isinstance (node .func , ast .Name ):
4242 # Ignore functions that are object attributes (`foo.bar()`).
4343 # Assume that if the user calls `builtins.list()`, they know what
@@ -54,20 +54,23 @@ def visit_Call(self, node): # type: (ast.Call) -> None
5454 )
5555
5656
57- def check_file (filename , ignore = None , allow_dict_kwargs = True ):
58- # type: (str, Optional[Sequence[str]], bool) -> List[Call]
57+ def check_file (
58+ filename : str ,
59+ ignore : Optional [Sequence [str ]] = None ,
60+ allow_dict_kwargs : bool = True ,
61+ ) -> List [Call ]:
5962 with open (filename , 'rb' ) as f :
6063 tree = ast .parse (f .read (), filename = filename )
6164 visitor = Visitor (ignore = ignore , allow_dict_kwargs = allow_dict_kwargs )
6265 visitor .visit (tree )
6366 return visitor .builtin_type_calls
6467
6568
66- def parse_ignore (value ): # type: ( str) -> Set[str]
69+ def parse_ignore (value : str ) -> Set [str ]:
6770 return set (value .split (',' ))
6871
6972
70- def main (argv = None ): # type: ( Optional[Sequence[str]]) -> int
73+ def main (argv : Optional [Sequence [str ]] = None ) -> int :
7174 parser = argparse .ArgumentParser ()
7275 parser .add_argument ('filenames' , nargs = '*' )
7376 parser .add_argument ('--ignore' , type = parse_ignore , default = set ())
@@ -93,15 +96,11 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
9396 rc = rc or 1
9497 for call in calls :
9598 print (
96- '{filename}:{call.line}:{call.column}: '
97- 'replace {call.name}() with {replacement}' .format (
98- filename = filename ,
99- call = call ,
100- replacement = BUILTIN_TYPES [call .name ],
101- ),
99+ f'{ filename } :{ call .line } :{ call .column } : '
100+ f'replace { call .name } () with { BUILTIN_TYPES [call .name ]} ' ,
102101 )
103102 return rc
104103
105104
106105if __name__ == '__main__' :
107- sys . exit (main ())
106+ exit (main ())
0 commit comments