24
24
$ stubgen -p urllib
25
25
=> Generate stubs for whole urlib package (recursively).
26
26
27
- For Python 2 mode, use --py2:
28
-
29
- $ stubgen --py2 -m textwrap
30
-
31
27
For C modules, you can get more precise function signatures by parsing .rst (Sphinx)
32
28
documentation for extra information. For this, use the --doc-dir option:
33
29
36
32
Note: The generated stubs should be verified manually.
37
33
38
34
TODO:
39
- - support stubs for C modules in Python 2 mode
40
- - detect 'if PY2 / is_py2' etc. and either preserve those or only include Python 2 or 3 case
41
35
- maybe use .rst docs also for Python modules
42
36
- maybe export more imported names if there is no __all__ (this affects ssl.SSLError, for example)
43
37
- a quick and dirty heuristic would be to turn this on if a module has something like
113
107
from mypy .stubutil import (
114
108
CantImport ,
115
109
common_dir_prefix ,
116
- default_py2_interpreter ,
117
110
fail_missing ,
118
- find_module_path_and_all_py2 ,
119
111
find_module_path_and_all_py3 ,
120
112
generate_guarded ,
121
113
remove_misplaced_type_comments ,
@@ -1423,12 +1415,7 @@ def collect_build_targets(
1423
1415
else :
1424
1416
# Using imports is the default, since we can also find C modules.
1425
1417
py_modules , c_modules = find_module_paths_using_imports (
1426
- options .modules ,
1427
- options .packages ,
1428
- options .interpreter ,
1429
- options .pyversion ,
1430
- options .verbose ,
1431
- options .quiet ,
1418
+ options .modules , options .packages , options .verbose , options .quiet
1432
1419
)
1433
1420
else :
1434
1421
# Use mypy native source collection for files and directories.
@@ -1445,12 +1432,7 @@ def collect_build_targets(
1445
1432
1446
1433
1447
1434
def find_module_paths_using_imports (
1448
- modules : List [str ],
1449
- packages : List [str ],
1450
- interpreter : str ,
1451
- pyversion : Tuple [int , int ],
1452
- verbose : bool ,
1453
- quiet : bool ,
1435
+ modules : List [str ], packages : List [str ], verbose : bool , quiet : bool
1454
1436
) -> Tuple [List [StubSource ], List [StubSource ]]:
1455
1437
"""Find path and runtime value of __all__ (if possible) for modules and packages.
1456
1438
@@ -1466,10 +1448,7 @@ def find_module_paths_using_imports(
1466
1448
] # We don't want to run any tests or scripts
1467
1449
for mod in modules :
1468
1450
try :
1469
- if pyversion [0 ] == 2 :
1470
- result = find_module_path_and_all_py2 (mod , interpreter )
1471
- else :
1472
- result = find_module_path_and_all_py3 (inspect , mod , verbose )
1451
+ result = find_module_path_and_all_py3 (inspect , mod , verbose )
1473
1452
except CantImport as e :
1474
1453
tb = traceback .format_exc ()
1475
1454
if verbose :
@@ -1719,7 +1698,7 @@ def generate_stubs(options: Options) -> None:
1719
1698
print (f"Generated files under { common_dir_prefix (files )} " + os .sep )
1720
1699
1721
1700
1722
- HEADER = """%(prog)s [-h] [--py2] [ more options, see -h]
1701
+ HEADER = """%(prog)s [-h] [more options, see -h]
1723
1702
[-m MODULE] [-p PACKAGE] [files ...]"""
1724
1703
1725
1704
DESCRIPTION = """
@@ -1733,9 +1712,6 @@ def generate_stubs(options: Options) -> None:
1733
1712
def parse_options (args : List [str ]) -> Options :
1734
1713
parser = argparse .ArgumentParser (prog = "stubgen" , usage = HEADER , description = DESCRIPTION )
1735
1714
1736
- parser .add_argument (
1737
- "--py2" , action = "store_true" , help = "run in Python 2 mode (default: Python 3 mode)"
1738
- )
1739
1715
parser .add_argument (
1740
1716
"--ignore-errors" ,
1741
1717
action = "store_true" ,
@@ -1784,13 +1760,6 @@ def parse_options(args: List[str]) -> Options:
1784
1760
help = "specify module search directories, separated by ':' "
1785
1761
"(currently only used if --no-import is given)" ,
1786
1762
)
1787
- parser .add_argument (
1788
- "--python-executable" ,
1789
- metavar = "PATH" ,
1790
- dest = "interpreter" ,
1791
- default = "" ,
1792
- help = "use Python interpreter at PATH (only works for " "Python 2 right now)" ,
1793
- )
1794
1763
parser .add_argument (
1795
1764
"-o" ,
1796
1765
"--output" ,
@@ -1826,9 +1795,9 @@ def parse_options(args: List[str]) -> Options:
1826
1795
1827
1796
ns = parser .parse_args (args )
1828
1797
1829
- pyversion = defaults . PYTHON2_VERSION if ns . py2 else sys .version_info [:2 ]
1830
- if not ns .interpreter :
1831
- ns . interpreter = sys . executable if pyversion [ 0 ] == 3 else default_py2_interpreter ()
1798
+ pyversion = sys .version_info [:2 ]
1799
+ ns .interpreter = sys . executable
1800
+
1832
1801
if ns .modules + ns .packages and ns .files :
1833
1802
parser .error ("May only specify one of: modules/packages or files." )
1834
1803
if ns .quiet and ns .verbose :
0 commit comments