73
73
('modules' , List [str ]),
74
74
('ignore_errors' , bool ),
75
75
('recursive' , bool ),
76
+ ('include_private' , bool ),
76
77
])
77
78
78
79
@@ -86,7 +87,8 @@ def generate_stub_for_module(module: str, output_dir: str, quiet: bool = False,
86
87
pyversion : Tuple [int , int ] = defaults .PYTHON3_VERSION ,
87
88
no_import : bool = False ,
88
89
search_path : List [str ] = [],
89
- interpreter : str = sys .executable ) -> None :
90
+ interpreter : str = sys .executable ,
91
+ include_private : bool = False ) -> None :
90
92
target = module .replace ('.' , '/' )
91
93
try :
92
94
result = find_module_path_and_all (module = module ,
@@ -118,7 +120,7 @@ def generate_stub_for_module(module: str, output_dir: str, quiet: bool = False,
118
120
target = os .path .join (output_dir , target )
119
121
generate_stub (module_path , output_dir , module_all ,
120
122
target = target , add_header = add_header , module = module ,
121
- pyversion = pyversion )
123
+ pyversion = pyversion , include_private = include_private )
122
124
if not quiet :
123
125
print ('Created %s' % target )
124
126
@@ -185,7 +187,8 @@ def load_python_module_info(module: str, interpreter: str) -> Tuple[str, Optiona
185
187
186
188
def generate_stub (path : str , output_dir : str , _all_ : Optional [List [str ]] = None ,
187
189
target : str = None , add_header : bool = False , module : str = None ,
188
- pyversion : Tuple [int , int ] = defaults .PYTHON3_VERSION
190
+ pyversion : Tuple [int , int ] = defaults .PYTHON3_VERSION ,
191
+ include_private : bool = False
189
192
) -> None :
190
193
with open (path , 'rb' ) as f :
191
194
source = f .read ()
@@ -199,7 +202,7 @@ def generate_stub(path: str, output_dir: str, _all_: Optional[List[str]] = None,
199
202
sys .stderr .write ('%s\n ' % m )
200
203
sys .exit (1 )
201
204
202
- gen = StubGenerator (_all_ , pyversion = pyversion )
205
+ gen = StubGenerator (_all_ , pyversion = pyversion , include_private = include_private )
203
206
ast .accept (gen )
204
207
if not target :
205
208
target = os .path .join (output_dir , os .path .basename (path ))
@@ -223,7 +226,8 @@ def generate_stub(path: str, output_dir: str, _all_: Optional[List[str]] = None,
223
226
224
227
225
228
class StubGenerator (mypy .traverser .TraverserVisitor ):
226
- def __init__ (self , _all_ : Optional [List [str ]], pyversion : Tuple [int , int ]) -> None :
229
+ def __init__ (self , _all_ : Optional [List [str ]], pyversion : Tuple [int , int ],
230
+ include_private : bool = False ) -> None :
227
231
self ._all_ = _all_
228
232
self ._output = [] # type: List[str]
229
233
self ._import_lines = [] # type: List[str]
@@ -235,6 +239,7 @@ def __init__(self, _all_: Optional[List[str]], pyversion: Tuple[int, int]) -> No
235
239
self ._classes = set () # type: Set[str]
236
240
self ._base_classes = [] # type: List[str]
237
241
self ._pyversion = pyversion
242
+ self ._include_private = include_private
238
243
239
244
def visit_mypy_file (self , o : MypyFile ) -> None :
240
245
self ._classes = find_classes (o )
@@ -515,6 +520,8 @@ def is_not_in_all(self, name: str) -> bool:
515
520
return False
516
521
517
522
def is_private_name (self , name : str ) -> bool :
523
+ if self ._include_private :
524
+ return False
518
525
return name .startswith ('_' ) and (not name .endswith ('__' )
519
526
or name in ('__all__' ,
520
527
'__author__' ,
@@ -610,7 +617,7 @@ def walk_packages(packages: List[str]) -> Iterator[str]:
610
617
611
618
612
619
def main () -> None :
613
- options = parse_options ()
620
+ options = parse_options (sys . argv [ 1 :] )
614
621
if not os .path .isdir ('out' ):
615
622
raise SystemExit ('Directory "out" does not exist' )
616
623
if options .recursive and options .no_import :
@@ -636,23 +643,24 @@ def main() -> None:
636
643
pyversion = options .pyversion ,
637
644
no_import = options .no_import ,
638
645
search_path = options .search_path ,
639
- interpreter = options .interpreter )
646
+ interpreter = options .interpreter ,
647
+ include_private = options .include_private )
640
648
except Exception as e :
641
649
if not options .ignore_errors :
642
650
raise e
643
651
else :
644
652
print ("Stub generation failed for" , module , file = sys .stderr )
645
653
646
654
647
- def parse_options () -> Options :
648
- args = sys .argv [1 :]
655
+ def parse_options (args : List [str ]) -> Options :
649
656
pyversion = defaults .PYTHON3_VERSION
650
657
no_import = False
651
658
recursive = False
652
659
ignore_errors = False
653
660
doc_dir = ''
654
661
search_path = [] # type: List[str]
655
662
interpreter = ''
663
+ include_private = False
656
664
while args and args [0 ].startswith ('-' ):
657
665
if args [0 ] == '--doc-dir' :
658
666
doc_dir = args [1 ]
@@ -673,6 +681,8 @@ def parse_options() -> Options:
673
681
pyversion = defaults .PYTHON2_VERSION
674
682
elif args [0 ] == '--no-import' :
675
683
no_import = True
684
+ elif args [0 ] == '--include-private' :
685
+ include_private = True
676
686
elif args [0 ] in ('-h' , '--help' ):
677
687
usage ()
678
688
else :
@@ -689,7 +699,8 @@ def parse_options() -> Options:
689
699
interpreter = interpreter ,
690
700
modules = args ,
691
701
ignore_errors = ignore_errors ,
692
- recursive = recursive )
702
+ recursive = recursive ,
703
+ include_private = include_private )
693
704
694
705
695
706
def default_python2_interpreter () -> str :
@@ -721,6 +732,9 @@ def usage() -> None:
721
732
--no-import don't import the modules, just parse and analyze them
722
733
(doesn't work with C extension modules and doesn't
723
734
respect __all__)
735
+ --include-private
736
+ generate stubs for objects and members considered private
737
+ (single leading undescore and no trailing underscores)
724
738
--doc-dir PATH use .rst documentation in PATH (this may result in
725
739
better stubs in some cases; consider setting this to
726
740
DIR/Python-X.Y.Z/Doc/library)
0 commit comments