Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop stubgen support for Python 2 #13256

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions docs/source/stubgen.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,6 @@ Additional flags

Show help message and exit.

.. option:: --py2

Run stubgen in Python 2 mode (the default is Python 3 mode).

.. option:: --ignore-errors

If an exception was raised during stub generation, continue to process any
Expand All @@ -172,13 +168,6 @@ Additional flags
Specify module search directories, separated by colons (only used if
:option:`--no-import` is given).

.. option:: --python-executable PATH

Use Python interpreter at ``PATH`` for importing modules and runtime
introspection. This has no effect with :option:`--no-import`, and this only works
in Python 2 mode. In Python 3 mode the Python interpreter used to run stubgen
will always be used.

.. option:: -o PATH, --output PATH

Change the output directory. By default the stubs are written in the
Expand Down
45 changes: 7 additions & 38 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
$ stubgen -p urllib
=> Generate stubs for whole urlib package (recursively).

For Python 2 mode, use --py2:

$ stubgen --py2 -m textwrap

For C modules, you can get more precise function signatures by parsing .rst (Sphinx)
documentation for extra information. For this, use the --doc-dir option:

Expand All @@ -36,8 +32,6 @@
Note: The generated stubs should be verified manually.

TODO:
- support stubs for C modules in Python 2 mode
- detect 'if PY2 / is_py2' etc. and either preserve those or only include Python 2 or 3 case
- maybe use .rst docs also for Python modules
- maybe export more imported names if there is no __all__ (this affects ssl.SSLError, for example)
- a quick and dirty heuristic would be to turn this on if a module has something like
Expand Down Expand Up @@ -113,9 +107,7 @@
from mypy.stubutil import (
CantImport,
common_dir_prefix,
default_py2_interpreter,
fail_missing,
find_module_path_and_all_py2,
find_module_path_and_all_py3,
generate_guarded,
remove_misplaced_type_comments,
Expand Down Expand Up @@ -1423,12 +1415,7 @@ def collect_build_targets(
else:
# Using imports is the default, since we can also find C modules.
py_modules, c_modules = find_module_paths_using_imports(
options.modules,
options.packages,
options.interpreter,
options.pyversion,
options.verbose,
options.quiet,
options.modules, options.packages, options.verbose, options.quiet
)
else:
# Use mypy native source collection for files and directories.
Expand All @@ -1445,12 +1432,7 @@ def collect_build_targets(


def find_module_paths_using_imports(
modules: List[str],
packages: List[str],
interpreter: str,
pyversion: Tuple[int, int],
verbose: bool,
quiet: bool,
modules: List[str], packages: List[str], verbose: bool, quiet: bool
) -> Tuple[List[StubSource], List[StubSource]]:
"""Find path and runtime value of __all__ (if possible) for modules and packages.

Expand All @@ -1466,10 +1448,7 @@ def find_module_paths_using_imports(
] # We don't want to run any tests or scripts
for mod in modules:
try:
if pyversion[0] == 2:
result = find_module_path_and_all_py2(mod, interpreter)
else:
result = find_module_path_and_all_py3(inspect, mod, verbose)
result = find_module_path_and_all_py3(inspect, mod, verbose)
except CantImport as e:
tb = traceback.format_exc()
if verbose:
Expand Down Expand Up @@ -1719,7 +1698,7 @@ def generate_stubs(options: Options) -> None:
print(f"Generated files under {common_dir_prefix(files)}" + os.sep)


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

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

parser.add_argument(
"--py2", action="store_true", help="run in Python 2 mode (default: Python 3 mode)"
)
parser.add_argument(
"--ignore-errors",
action="store_true",
Expand Down Expand Up @@ -1784,13 +1760,6 @@ def parse_options(args: List[str]) -> Options:
help="specify module search directories, separated by ':' "
"(currently only used if --no-import is given)",
)
parser.add_argument(
"--python-executable",
metavar="PATH",
dest="interpreter",
default="",
help="use Python interpreter at PATH (only works for " "Python 2 right now)",
)
parser.add_argument(
"-o",
"--output",
Expand Down Expand Up @@ -1826,9 +1795,9 @@ def parse_options(args: List[str]) -> Options:

ns = parser.parse_args(args)

pyversion = defaults.PYTHON2_VERSION if ns.py2 else sys.version_info[:2]
if not ns.interpreter:
ns.interpreter = sys.executable if pyversion[0] == 3 else default_py2_interpreter()
pyversion = sys.version_info[:2]
ns.interpreter = sys.executable

if ns.modules + ns.packages and ns.files:
parser.error("May only specify one of: modules/packages or files.")
if ns.quiet and ns.verbose:
Expand Down