From 0ec17bc2d435b4700c64cd206f3be4ac0dc19d56 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 27 Jul 2022 20:11:59 -0700 Subject: [PATCH 1/2] Drop stubgen support for Python 2 --- docs/source/stubgen.rst | 11 ----------- mypy/stubgen.py | 35 +++++------------------------------ 2 files changed, 5 insertions(+), 41 deletions(-) diff --git a/docs/source/stubgen.rst b/docs/source/stubgen.rst index 33fdac2089f7..f06c9c066bb7 100644 --- a/docs/source/stubgen.rst +++ b/docs/source/stubgen.rst @@ -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 @@ -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 diff --git a/mypy/stubgen.py b/mypy/stubgen.py index f6e1fd6c23ce..09220def336f 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -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: @@ -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 @@ -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, @@ -1425,8 +1417,6 @@ def collect_build_targets( py_modules, c_modules = find_module_paths_using_imports( options.modules, options.packages, - options.interpreter, - options.pyversion, options.verbose, options.quiet, ) @@ -1447,8 +1437,6 @@ 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, ) -> Tuple[List[StubSource], List[StubSource]]: @@ -1466,10 +1454,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: @@ -1719,7 +1704,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 = """ @@ -1733,9 +1718,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", @@ -1784,13 +1766,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", @@ -1826,9 +1801,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: From 6b09d56a29d2d78c5b8d6585d32b41e97c1ba396 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 27 Jul 2022 20:17:38 -0700 Subject: [PATCH 2/2] black --- mypy/stubgen.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/mypy/stubgen.py b/mypy/stubgen.py index 09220def336f..08f86d96be11 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -1415,10 +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.verbose, - options.quiet, + options.modules, options.packages, options.verbose, options.quiet ) else: # Use mypy native source collection for files and directories. @@ -1435,10 +1432,7 @@ def collect_build_targets( def find_module_paths_using_imports( - modules: List[str], - packages: List[str], - 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.