66import importlib
77import os .path
88import re
9-
9+ from typing import List , Dict , Tuple
10+ from types import ModuleType
1011
1112from mypy .stubutil import (
1213 parse_all_signatures , find_unique_signatures , is_c_module , write_header ,
1314 infer_sig_from_docstring
1415)
1516
1617
17- def generate_stub_for_c_module (module_name , target , add_header = True , sigs = {}, class_sigs = {}):
18+ def generate_stub_for_c_module (module_name : str ,
19+ target : str ,
20+ add_header : bool = True ,
21+ sigs : Dict [str , str ] = {},
22+ class_sigs : Dict [str , str ] = {},
23+ ) -> None :
1824 module = importlib .import_module (module_name )
1925 assert is_c_module (module ), '%s is not a C module' % module_name
2026 subdir = os .path .dirname (target )
2127 if subdir and not os .path .isdir (subdir ):
2228 os .makedirs (subdir )
23- functions = []
29+ functions = [] # type: List[str]
2430 done = set ()
2531 items = sorted (module .__dict__ .items (), key = lambda x : x [0 ])
2632 for name , obj in items :
2733 if is_c_function (obj ):
2834 generate_c_function_stub (module , name , obj , functions , sigs = sigs )
2935 done .add (name )
30- types = []
36+ types = [] # type: List[str]
3137 for name , obj in items :
3238 if name .startswith ('__' ) and name .endswith ('__' ):
3339 continue
@@ -62,7 +68,7 @@ def generate_stub_for_c_module(module_name, target, add_header=True, sigs={}, cl
6268 file .write ('%s\n ' % line )
6369
6470
65- def add_typing_import (output ) :
71+ def add_typing_import (output : List [ str ]) -> List [ str ] :
6672 names = []
6773 for name in ['Any' ]:
6874 if any (re .search (r'\b%s\b' % name , line ) for line in output ):
@@ -73,27 +79,34 @@ def add_typing_import(output):
7379 return output [:]
7480
7581
76- def is_c_function (obj ) :
82+ def is_c_function (obj : object ) -> bool :
7783 return type (obj ) is type (ord )
7884
7985
80- def is_c_method (obj ) :
86+ def is_c_method (obj : object ) -> bool :
8187 return type (obj ) in (type (str .index ),
8288 type (str .__add__ ),
8389 type (str .__new__ ))
8490
8591
86- def is_c_classmethod (obj ) :
92+ def is_c_classmethod (obj : object ) -> bool :
8793 type_str = type (obj ).__name__
8894 return type_str == 'classmethod_descriptor'
8995
9096
91- def is_c_type (obj ) :
97+ def is_c_type (obj : object ) -> bool :
9298 return type (obj ) is type (int )
9399
94100
95- def generate_c_function_stub (module , name , obj , output , self_var = None , sigs = {}, class_name = None ,
96- class_sigs = {}):
101+ def generate_c_function_stub (module : ModuleType ,
102+ name : str ,
103+ obj : object ,
104+ output : List [str ],
105+ self_var : str = None ,
106+ sigs : Dict [str , str ] = {},
107+ class_name : str = None ,
108+ class_sigs : Dict [str , str ] = {},
109+ ) -> None :
97110 if self_var :
98111 self_arg = '%s, ' % self_var
99112 else :
@@ -117,7 +130,13 @@ def generate_c_function_stub(module, name, obj, output, self_var=None, sigs={},
117130 output .append ('def %s(%s%s): ...' % (name , self_arg , sig ))
118131
119132
120- def generate_c_type_stub (module , class_name , obj , output , sigs = {}, class_sigs = {}):
133+ def generate_c_type_stub (module : ModuleType ,
134+ class_name : str ,
135+ obj : type ,
136+ output : List [str ],
137+ sigs : Dict [str , str ] = {},
138+ class_sigs : Dict [str , str ] = {},
139+ ) -> None :
121140 items = sorted (obj .__dict__ .items (), key = lambda x : method_name_sort_key (x [0 ]))
122141 methods = []
123142 done = set ()
@@ -151,7 +170,7 @@ def generate_c_type_stub(module, class_name, obj, output, sigs={}, class_sigs={}
151170 # TODO: Is this always object?
152171 del all_bases [- 1 ]
153172 # Remove base classes of other bases as redundant.
154- bases = []
173+ bases = [] # type: List[type]
155174 for base in all_bases :
156175 if not any (issubclass (b , base ) for b in bases ):
157176 bases .append (base )
@@ -169,15 +188,15 @@ def generate_c_type_stub(module, class_name, obj, output, sigs={}, class_sigs={}
169188 output .append (' %s' % method )
170189
171190
172- def method_name_sort_key (name ) :
191+ def method_name_sort_key (name : str ) -> Tuple [ int , str ] :
173192 if name in ('__new__' , '__init__' ):
174193 return (0 , name )
175194 if name .startswith ('__' ) and name .endswith ('__' ):
176195 return (2 , name )
177196 return (1 , name )
178197
179198
180- def is_skipped_attribute (attr ) :
199+ def is_skipped_attribute (attr : str ) -> bool :
181200 return attr in ('__getattribute__' ,
182201 '__str__' ,
183202 '__repr__' ,
@@ -187,7 +206,7 @@ def is_skipped_attribute(attr):
187206 '__weakref__' ) # For pickling
188207
189208
190- def infer_method_sig (name ) :
209+ def infer_method_sig (name : str ) -> str :
191210 if name .startswith ('__' ) and name .endswith ('__' ):
192211 name = name [2 :- 2 ]
193212 if name in ('hash' , 'iter' , 'next' , 'sizeof' , 'copy' , 'deepcopy' , 'reduce' , 'getinitargs' ,
0 commit comments