Skip to content

Commit 154d6f9

Browse files
authored
Drop stubgen support for Python 2 (#13256)
1 parent 002f77c commit 154d6f9

File tree

2 files changed

+7
-45
lines changed

2 files changed

+7
-45
lines changed

docs/source/stubgen.rst

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,6 @@ Additional flags
168168
Specify module search directories, separated by colons (only used if
169169
:option:`--no-import` is given).
170170

171-
.. option:: --python-executable PATH
172-
173-
Use Python interpreter at ``PATH`` for importing modules and runtime
174-
introspection. This has no effect with :option:`--no-import`, and this only works
175-
in Python 2 mode. In Python 3 mode the Python interpreter used to run stubgen
176-
will always be used.
177-
178171
.. option:: -o PATH, --output PATH
179172

180173
Change the output directory. By default the stubs are written in the

mypy/stubgen.py

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424
$ stubgen -p urllib
2525
=> Generate stubs for whole urlib package (recursively).
2626
27-
For Python 2 mode, use --py2:
28-
29-
$ stubgen --py2 -m textwrap
30-
3127
For C modules, you can get more precise function signatures by parsing .rst (Sphinx)
3228
documentation for extra information. For this, use the --doc-dir option:
3329
@@ -36,8 +32,6 @@
3632
Note: The generated stubs should be verified manually.
3733
3834
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
4135
- maybe use .rst docs also for Python modules
4236
- maybe export more imported names if there is no __all__ (this affects ssl.SSLError, for example)
4337
- a quick and dirty heuristic would be to turn this on if a module has something like
@@ -113,9 +107,7 @@
113107
from mypy.stubutil import (
114108
CantImport,
115109
common_dir_prefix,
116-
default_py2_interpreter,
117110
fail_missing,
118-
find_module_path_and_all_py2,
119111
find_module_path_and_all_py3,
120112
generate_guarded,
121113
remove_misplaced_type_comments,
@@ -1423,12 +1415,7 @@ def collect_build_targets(
14231415
else:
14241416
# Using imports is the default, since we can also find C modules.
14251417
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
14321419
)
14331420
else:
14341421
# Use mypy native source collection for files and directories.
@@ -1445,12 +1432,7 @@ def collect_build_targets(
14451432

14461433

14471434
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
14541436
) -> Tuple[List[StubSource], List[StubSource]]:
14551437
"""Find path and runtime value of __all__ (if possible) for modules and packages.
14561438
@@ -1466,10 +1448,7 @@ def find_module_paths_using_imports(
14661448
] # We don't want to run any tests or scripts
14671449
for mod in modules:
14681450
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)
14731452
except CantImport as e:
14741453
tb = traceback.format_exc()
14751454
if verbose:
@@ -1719,7 +1698,7 @@ def generate_stubs(options: Options) -> None:
17191698
print(f"Generated files under {common_dir_prefix(files)}" + os.sep)
17201699

17211700

1722-
HEADER = """%(prog)s [-h] [--py2] [more options, see -h]
1701+
HEADER = """%(prog)s [-h] [more options, see -h]
17231702
[-m MODULE] [-p PACKAGE] [files ...]"""
17241703

17251704
DESCRIPTION = """
@@ -1733,9 +1712,6 @@ def generate_stubs(options: Options) -> None:
17331712
def parse_options(args: List[str]) -> Options:
17341713
parser = argparse.ArgumentParser(prog="stubgen", usage=HEADER, description=DESCRIPTION)
17351714

1736-
parser.add_argument(
1737-
"--py2", action="store_true", help="run in Python 2 mode (default: Python 3 mode)"
1738-
)
17391715
parser.add_argument(
17401716
"--ignore-errors",
17411717
action="store_true",
@@ -1784,13 +1760,6 @@ def parse_options(args: List[str]) -> Options:
17841760
help="specify module search directories, separated by ':' "
17851761
"(currently only used if --no-import is given)",
17861762
)
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-
)
17941763
parser.add_argument(
17951764
"-o",
17961765
"--output",
@@ -1826,9 +1795,9 @@ def parse_options(args: List[str]) -> Options:
18261795

18271796
ns = parser.parse_args(args)
18281797

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+
18321801
if ns.modules + ns.packages and ns.files:
18331802
parser.error("May only specify one of: modules/packages or files.")
18341803
if ns.quiet and ns.verbose:

0 commit comments

Comments
 (0)