diff --git a/src/pip/_internal/cli/base_command.py b/src/pip/_internal/cli/base_command.py index 1fa5ba0bd..71d3e4c82 100644 --- a/src/pip/_internal/cli/base_command.py +++ b/src/pip/_internal/cli/base_command.py @@ -87,6 +87,13 @@ class Command(CommandContextMixIn): self.parser, ) self.parser.add_option_group(gen_opts) + try: + # mypy raises error due to + # https://github.com/python/mypy/issues/5868 + self.add_options(self.cmd_opts) # type: ignore + except AttributeError: + # it means that the base class has not defined the method + logger.debug("No add_options method defined in the class") def handle_pip_version_check(self, options): # type: (Values) -> None diff --git a/src/pip/_internal/commands/completion.py b/src/pip/_internal/commands/completion.py index 70d33243f..1dddf869d 100644 --- a/src/pip/_internal/commands/completion.py +++ b/src/pip/_internal/commands/completion.py @@ -9,8 +9,8 @@ from pip._internal.utils.misc import get_prog from pip._internal.utils.typing import MYPY_CHECK_RUNNING if MYPY_CHECK_RUNNING: - from typing import Any, List - from optparse import Values + from typing import List + from optparse import Values, OptionGroup BASE_COMPLETION = """ # pip {shell} completion start{script}# pip {shell} completion end @@ -56,12 +56,8 @@ class CompletionCommand(Command): ignore_require_venv = True - def __init__(self, *args, **kw): - # type: (*Any, **Any) -> None - super(CompletionCommand, self).__init__(*args, **kw) - - cmd_opts = self.cmd_opts - + def add_options(self, cmd_opts): + # type: (OptionGroup) -> None cmd_opts.add_option( '--bash', '-b', action='store_const', diff --git a/src/pip/_internal/commands/configuration.py b/src/pip/_internal/commands/configuration.py index b801be6a0..26cfd7a4a 100644 --- a/src/pip/_internal/commands/configuration.py +++ b/src/pip/_internal/commands/configuration.py @@ -45,12 +45,15 @@ class ConfigurationCommand(Command): %prog [] unset name """ - def __init__(self, *args, **kwargs): - super(ConfigurationCommand, self).__init__(*args, **kwargs) + def __init__(self, name, summary, isolated=False): + super(ConfigurationCommand, self).__init__( + name, summary, isolated=isolated + ) self.configuration = None - self.cmd_opts.add_option( + def add_options(self, cmd_opts): + cmd_opts.add_option( '--editor', dest='editor', action='store', @@ -61,7 +64,7 @@ class ConfigurationCommand(Command): ) ) - self.cmd_opts.add_option( + cmd_opts.add_option( '--global', dest='global_file', action='store_true', @@ -69,7 +72,7 @@ class ConfigurationCommand(Command): help='Use the system-wide configuration file only' ) - self.cmd_opts.add_option( + cmd_opts.add_option( '--user', dest='user_file', action='store_true', @@ -77,7 +80,7 @@ class ConfigurationCommand(Command): help='Use the user configuration file only' ) - self.cmd_opts.add_option( + cmd_opts.add_option( '--site', dest='site_file', action='store_true', @@ -85,7 +88,7 @@ class ConfigurationCommand(Command): help='Use the current environment configuration file only' ) - self.parser.insert_option_group(0, self.cmd_opts) + self.parser.insert_option_group(0, cmd_opts) def run(self, options, args): handlers = { diff --git a/src/pip/_internal/commands/debug.py b/src/pip/_internal/commands/debug.py index 8e243011f..3a32f9299 100644 --- a/src/pip/_internal/commands/debug.py +++ b/src/pip/_internal/commands/debug.py @@ -20,8 +20,8 @@ from pip._internal.utils.typing import MYPY_CHECK_RUNNING if MYPY_CHECK_RUNNING: from types import ModuleType - from typing import Any, List, Optional, Dict - from optparse import Values + from typing import List, Optional, Dict + from optparse import Values, OptionGroup logger = logging.getLogger(__name__) @@ -193,11 +193,8 @@ class DebugCommand(Command): %prog """ ignore_require_venv = True - def __init__(self, *args, **kw): - # type: (*Any, **Any) -> None - super(DebugCommand, self).__init__(*args, **kw) - - cmd_opts = self.cmd_opts + def add_options(self, cmd_opts): + # type: (OptionGroup) -> None cmdoptions.add_target_python_options(cmd_opts) self.parser.insert_option_group(0, cmd_opts) self.parser.config.load() diff --git a/src/pip/_internal/commands/download.py b/src/pip/_internal/commands/download.py index e48a7838c..c8da0e5ee 100644 --- a/src/pip/_internal/commands/download.py +++ b/src/pip/_internal/commands/download.py @@ -39,12 +39,7 @@ class DownloadCommand(RequirementCommand): %prog [options] ... %prog [options] ...""" - def __init__(self, *args, **kw): - # type: (*Any, **Any) -> None - super(DownloadCommand, self).__init__(*args, **kw) - - cmd_opts = self.cmd_opts - + def add_options(self, cmd_opts): cmd_opts.add_option(cmdoptions.constraints()) cmd_opts.add_option(cmdoptions.requirements()) cmd_opts.add_option(cmdoptions.build_dir()) diff --git a/src/pip/_internal/commands/freeze.py b/src/pip/_internal/commands/freeze.py index 5b9a78ff5..a140f94bd 100644 --- a/src/pip/_internal/commands/freeze.py +++ b/src/pip/_internal/commands/freeze.py @@ -29,11 +29,8 @@ class FreezeCommand(Command): %prog [options]""" log_streams = ("ext://sys.stderr", "ext://sys.stderr") - def __init__(self, *args, **kw): - # type: (*Any, **Any) -> None - super(FreezeCommand, self).__init__(*args, **kw) - - self.cmd_opts.add_option( + def add_options(self, cmd_opts): + cmd_opts.add_option( '-r', '--requirement', dest='requirements', action='append', @@ -42,7 +39,7 @@ class FreezeCommand(Command): help="Use the order in the given requirements file and its " "comments when generating output. This option can be " "used multiple times.") - self.cmd_opts.add_option( + cmd_opts.add_option( '-f', '--find-links', dest='find_links', action='append', @@ -50,33 +47,33 @@ class FreezeCommand(Command): metavar='URL', help='URL for finding packages, which will be added to the ' 'output.') - self.cmd_opts.add_option( + cmd_opts.add_option( '-l', '--local', dest='local', action='store_true', default=False, help='If in a virtualenv that has global access, do not output ' 'globally-installed packages.') - self.cmd_opts.add_option( + cmd_opts.add_option( '--user', dest='user', action='store_true', default=False, help='Only output packages installed in user-site.') - self.cmd_opts.add_option(cmdoptions.list_path()) - self.cmd_opts.add_option( + cmd_opts.add_option(cmdoptions.list_path()) + cmd_opts.add_option( '--all', dest='freeze_all', action='store_true', help='Do not skip these packages in the output:' ' {}'.format(', '.join(DEV_PKGS))) - self.cmd_opts.add_option( + cmd_opts.add_option( '--exclude-editable', dest='exclude_editable', action='store_true', help='Exclude editable package from output.') - self.parser.insert_option_group(0, self.cmd_opts) + self.parser.insert_option_group(0, cmd_opts) def run(self, options, args): # type: (Values, List[str]) -> int diff --git a/src/pip/_internal/commands/hash.py b/src/pip/_internal/commands/hash.py index aab4a3dc2..1a09c4f7c 100644 --- a/src/pip/_internal/commands/hash.py +++ b/src/pip/_internal/commands/hash.py @@ -11,8 +11,8 @@ from pip._internal.utils.misc import read_chunks, write_output from pip._internal.utils.typing import MYPY_CHECK_RUNNING if MYPY_CHECK_RUNNING: - from optparse import Values - from typing import Any, List + from optparse import Values, OptionGroup + from typing import List logger = logging.getLogger(__name__) @@ -28,10 +28,9 @@ class HashCommand(Command): usage = '%prog [options] ...' ignore_require_venv = True - def __init__(self, *args, **kw): - # type: (*Any, **Any) -> None - super(HashCommand, self).__init__(*args, **kw) - self.cmd_opts.add_option( + def add_options(self, cmd_opts): + # type: (OptionGroup) -> None + cmd_opts.add_option( '-a', '--algorithm', dest='algorithm', choices=STRONG_HASHES, @@ -39,7 +38,7 @@ class HashCommand(Command): default=FAVORITE_HASH, help='The hash algorithm to use: one of {}'.format( ', '.join(STRONG_HASHES))) - self.parser.insert_option_group(0, self.cmd_opts) + self.parser.insert_option_group(0, cmd_opts) def run(self, options, args): # type: (Values, List[str]) -> int diff --git a/src/pip/_internal/commands/install.py b/src/pip/_internal/commands/install.py index c9b9ea4a8..ee933b67e 100644 --- a/src/pip/_internal/commands/install.py +++ b/src/pip/_internal/commands/install.py @@ -43,7 +43,7 @@ from pip._internal.utils.virtualenv import virtualenv_no_global from pip._internal.wheel_builder import build, should_build_for_install_command if MYPY_CHECK_RUNNING: - from optparse import Values + from optparse import Values, OptionGroup from typing import Any, Iterable, List, Optional from pip._internal.models.format_control import FormatControl @@ -87,11 +87,8 @@ class InstallCommand(RequirementCommand): %prog [options] [-e] ... %prog [options] ...""" - def __init__(self, *args, **kw): - super(InstallCommand, self).__init__(*args, **kw) - - cmd_opts = self.cmd_opts - + def add_options(self, cmd_opts): + # type: (OptionGroup) -> None cmd_opts.add_option(cmdoptions.requirements()) cmd_opts.add_option(cmdoptions.constraints()) cmd_opts.add_option(cmdoptions.no_deps()) diff --git a/src/pip/_internal/commands/list.py b/src/pip/_internal/commands/list.py index 052f63890..7d6acb818 100644 --- a/src/pip/_internal/commands/list.py +++ b/src/pip/_internal/commands/list.py @@ -22,8 +22,8 @@ from pip._internal.utils.packaging import get_installer from pip._internal.utils.typing import MYPY_CHECK_RUNNING if MYPY_CHECK_RUNNING: - from optparse import Values - from typing import Any, List, Set, Tuple, Iterator + from optparse import Values, OptionGroup + from typing import List, Set, Tuple, Iterator from pip._internal.network.session import PipSession from pip._vendor.pkg_resources import Distribution @@ -41,12 +41,8 @@ class ListCommand(IndexGroupCommand): usage = """ %prog [options]""" - def __init__(self, *args, **kw): - # type: (*Any, **Any) -> None - super(ListCommand, self).__init__(*args, **kw) - - cmd_opts = self.cmd_opts - + def add_options(self, cmd_opts): + # type: (OptionGroup) -> None cmd_opts.add_option( '-o', '--outdated', action='store_true', diff --git a/src/pip/_internal/commands/search.py b/src/pip/_internal/commands/search.py index d8c5fba8f..dc97dae1e 100644 --- a/src/pip/_internal/commands/search.py +++ b/src/pip/_internal/commands/search.py @@ -41,17 +41,15 @@ class SearchCommand(Command, SessionCommandMixin): %prog [options] """ ignore_require_venv = True - def __init__(self, *args, **kw): - # type: (*Any, **Any) -> None - super(SearchCommand, self).__init__(*args, **kw) - self.cmd_opts.add_option( + def add_options(self, cmd_opts): + cmd_opts.add_option( '-i', '--index', dest='index', metavar='URL', default=PyPI.pypi_url, help='Base URL of Python Package Index (default %default)') - self.parser.insert_option_group(0, self.cmd_opts) + self.parser.insert_option_group(0, cmd_opts) def run(self, options, args): # type: (Values, List[str]) -> int diff --git a/src/pip/_internal/commands/show.py b/src/pip/_internal/commands/show.py index 97735f2d2..0b9723498 100644 --- a/src/pip/_internal/commands/show.py +++ b/src/pip/_internal/commands/show.py @@ -13,8 +13,8 @@ from pip._internal.utils.misc import write_output from pip._internal.utils.typing import MYPY_CHECK_RUNNING if MYPY_CHECK_RUNNING: - from optparse import Values - from typing import Any, List, Dict, Iterator + from optparse import Values, OptionGroup + from typing import List, Dict, Iterator logger = logging.getLogger(__name__) @@ -30,17 +30,16 @@ class ShowCommand(Command): %prog [options] ...""" ignore_require_venv = True - def __init__(self, *args, **kw): - # type: (*Any, **Any) -> None - super(ShowCommand, self).__init__(*args, **kw) - self.cmd_opts.add_option( + def add_options(self, cmd_opts): + # type: (OptionGroup) -> None + cmd_opts.add_option( '-f', '--files', dest='files', action='store_true', default=False, help='Show the full list of installed files for each package.') - self.parser.insert_option_group(0, self.cmd_opts) + self.parser.insert_option_group(0, cmd_opts) def run(self, options, args): # type: (Values, List[str]) -> int diff --git a/src/pip/_internal/commands/uninstall.py b/src/pip/_internal/commands/uninstall.py index 0542e78c7..e5b6b9460 100644 --- a/src/pip/_internal/commands/uninstall.py +++ b/src/pip/_internal/commands/uninstall.py @@ -15,8 +15,8 @@ from pip._internal.utils.misc import protect_pip_from_modification_on_windows from pip._internal.utils.typing import MYPY_CHECK_RUNNING if MYPY_CHECK_RUNNING: - from optparse import Values - from typing import Any, List + from optparse import Values, OptionGroup + from typing import List class UninstallCommand(Command, SessionCommandMixin): @@ -34,10 +34,9 @@ class UninstallCommand(Command, SessionCommandMixin): %prog [options] ... %prog [options] -r ...""" - def __init__(self, *args, **kw): - # type: (*Any, **Any) -> None - super(UninstallCommand, self).__init__(*args, **kw) - self.cmd_opts.add_option( + def add_options(self, cmd_opts): + # type: (OptionGroup) -> None + cmd_opts.add_option( '-r', '--requirement', dest='requirements', action='append', @@ -46,13 +45,13 @@ class UninstallCommand(Command, SessionCommandMixin): help='Uninstall all the packages listed in the given requirements ' 'file. This option can be used multiple times.', ) - self.cmd_opts.add_option( + cmd_opts.add_option( '-y', '--yes', dest='yes', action='store_true', help="Don't ask for confirmation of uninstall deletions.") - self.parser.insert_option_group(0, self.cmd_opts) + self.parser.insert_option_group(0, cmd_opts) def run(self, options, args): # type: (Values, List[str]) -> int diff --git a/src/pip/_internal/commands/wheel.py b/src/pip/_internal/commands/wheel.py index f028d681f..e3d249f25 100644 --- a/src/pip/_internal/commands/wheel.py +++ b/src/pip/_internal/commands/wheel.py @@ -18,8 +18,8 @@ from pip._internal.utils.typing import MYPY_CHECK_RUNNING from pip._internal.wheel_builder import build, should_build_for_wheel_command if MYPY_CHECK_RUNNING: - from optparse import Values - from typing import Any, List + from optparse import Values, OptionGroup + from typing import List logger = logging.getLogger(__name__) @@ -47,10 +47,8 @@ class WheelCommand(RequirementCommand): %prog [options] [-e] ... %prog [options] ...""" - def __init__(self, *args, **kw): - # type: (*Any, **Any) -> None - super(WheelCommand, self).__init__(*args, **kw) - + def add_options(self, cmd_opts): + # type: (OptionGroup) -> None cmd_opts = self.cmd_opts cmd_opts.add_option( diff --git a/tests/unit/test_format_control.py b/tests/unit/test_format_control.py index 0b0e2bde2..9901c0303 100644 --- a/tests/unit/test_format_control.py +++ b/tests/unit/test_format_control.py @@ -9,8 +9,10 @@ class SimpleCommand(Command): def __init__(self): super(SimpleCommand, self).__init__('fake', 'fake summary') - self.cmd_opts.add_option(cmdoptions.no_binary()) - self.cmd_opts.add_option(cmdoptions.only_binary()) + + def add_options(self, cmd_opts): + cmd_opts.add_option(cmdoptions.no_binary()) + cmd_opts.add_option(cmdoptions.only_binary()) def run(self, options, args): self.options = options