Merge pull request #8281 from gutsytechster/create_add_option

This commit is contained in:
Pradyun Gedam 2020-05-23 17:05:12 +05:30 committed by GitHub
commit f3a0bf8172
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 135 additions and 149 deletions

View File

@ -0,0 +1,2 @@
Refactor the commands by removing the ``__init__`` method and defining and explicit
``add_options`` method for adding command options.

View File

@ -88,6 +88,12 @@ class Command(CommandContextMixIn):
)
self.parser.add_option_group(gen_opts)
self.add_options()
def add_options(self):
# type: () -> None
pass
def handle_pip_version_check(self, options):
# type: (Values) -> None
"""

View File

@ -9,7 +9,7 @@ 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 typing import List
from optparse import Values
BASE_COMPLETION = """
@ -56,32 +56,28 @@ 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
cmd_opts.add_option(
def add_options(self):
# type: () -> None
self.cmd_opts.add_option(
'--bash', '-b',
action='store_const',
const='bash',
dest='shell',
help='Emit completion code for bash')
cmd_opts.add_option(
self.cmd_opts.add_option(
'--zsh', '-z',
action='store_const',
const='zsh',
dest='shell',
help='Emit completion code for zsh')
cmd_opts.add_option(
self.cmd_opts.add_option(
'--fish', '-f',
action='store_const',
const='fish',
dest='shell',
help='Emit completion code for fish')
self.parser.insert_option_group(0, cmd_opts)
self.parser.insert_option_group(0, self.cmd_opts)
def run(self, options, args):
# type: (Values, List[str]) -> int

View File

@ -45,11 +45,14 @@ class ConfigurationCommand(Command):
%prog [<file-option>] 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
def add_options(self):
self.cmd_opts.add_option(
'--editor',
dest='editor',

View File

@ -20,7 +20,7 @@ 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 typing import List, Optional, Dict
from optparse import Values
logger = logging.getLogger(__name__)
@ -193,13 +193,10 @@ class DebugCommand(Command):
%prog <options>"""
ignore_require_venv = True
def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(DebugCommand, self).__init__(*args, **kw)
cmd_opts = self.cmd_opts
cmdoptions.add_target_python_options(cmd_opts)
self.parser.insert_option_group(0, cmd_opts)
def add_options(self):
# type: () -> None
cmdoptions.add_target_python_options(self.cmd_opts)
self.parser.insert_option_group(0, self.cmd_opts)
self.parser.config.load()
def run(self, options, args):

View File

@ -14,7 +14,7 @@ from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
from optparse import Values
from typing import Any, List
from typing import List
logger = logging.getLogger(__name__)
@ -39,29 +39,25 @@ class DownloadCommand(RequirementCommand):
%prog [options] <local project path> ...
%prog [options] <archive url/path> ..."""
def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(DownloadCommand, self).__init__(*args, **kw)
def add_options(self):
# type: () -> None
self.cmd_opts.add_option(cmdoptions.constraints())
self.cmd_opts.add_option(cmdoptions.requirements())
self.cmd_opts.add_option(cmdoptions.build_dir())
self.cmd_opts.add_option(cmdoptions.no_deps())
self.cmd_opts.add_option(cmdoptions.global_options())
self.cmd_opts.add_option(cmdoptions.no_binary())
self.cmd_opts.add_option(cmdoptions.only_binary())
self.cmd_opts.add_option(cmdoptions.prefer_binary())
self.cmd_opts.add_option(cmdoptions.src())
self.cmd_opts.add_option(cmdoptions.pre())
self.cmd_opts.add_option(cmdoptions.require_hashes())
self.cmd_opts.add_option(cmdoptions.progress_bar())
self.cmd_opts.add_option(cmdoptions.no_build_isolation())
self.cmd_opts.add_option(cmdoptions.use_pep517())
self.cmd_opts.add_option(cmdoptions.no_use_pep517())
cmd_opts = self.cmd_opts
cmd_opts.add_option(cmdoptions.constraints())
cmd_opts.add_option(cmdoptions.requirements())
cmd_opts.add_option(cmdoptions.build_dir())
cmd_opts.add_option(cmdoptions.no_deps())
cmd_opts.add_option(cmdoptions.global_options())
cmd_opts.add_option(cmdoptions.no_binary())
cmd_opts.add_option(cmdoptions.only_binary())
cmd_opts.add_option(cmdoptions.prefer_binary())
cmd_opts.add_option(cmdoptions.src())
cmd_opts.add_option(cmdoptions.pre())
cmd_opts.add_option(cmdoptions.require_hashes())
cmd_opts.add_option(cmdoptions.progress_bar())
cmd_opts.add_option(cmdoptions.no_build_isolation())
cmd_opts.add_option(cmdoptions.use_pep517())
cmd_opts.add_option(cmdoptions.no_use_pep517())
cmd_opts.add_option(
self.cmd_opts.add_option(
'-d', '--dest', '--destination-dir', '--destination-directory',
dest='download_dir',
metavar='dir',
@ -69,7 +65,7 @@ class DownloadCommand(RequirementCommand):
help=("Download packages into <dir>."),
)
cmdoptions.add_target_python_options(cmd_opts)
cmdoptions.add_target_python_options(self.cmd_opts)
index_opts = cmdoptions.make_option_group(
cmdoptions.index_group,
@ -77,7 +73,7 @@ class DownloadCommand(RequirementCommand):
)
self.parser.insert_option_group(0, index_opts)
self.parser.insert_option_group(0, cmd_opts)
self.parser.insert_option_group(0, self.cmd_opts)
@with_cleanup
def run(self, options, args):

View File

@ -15,7 +15,7 @@ DEV_PKGS = {'pip', 'setuptools', 'distribute', 'wheel'}
if MYPY_CHECK_RUNNING:
from optparse import Values
from typing import Any, List
from typing import List
class FreezeCommand(Command):
@ -29,10 +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)
def add_options(self):
# type: () -> None
self.cmd_opts.add_option(
'-r', '--requirement',
dest='requirements',

View File

@ -12,7 +12,7 @@ from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
from optparse import Values
from typing import Any, List
from typing import List
logger = logging.getLogger(__name__)
@ -28,9 +28,8 @@ class HashCommand(Command):
usage = '%prog [options] <file> ...'
ignore_require_venv = True
def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(HashCommand, self).__init__(*args, **kw)
def add_options(self):
# type: () -> None
self.cmd_opts.add_option(
'-a', '--algorithm',
dest='algorithm',

View File

@ -87,18 +87,15 @@ class InstallCommand(RequirementCommand):
%prog [options] [-e] <local project path> ...
%prog [options] <archive url/path> ..."""
def __init__(self, *args, **kw):
super(InstallCommand, self).__init__(*args, **kw)
def add_options(self):
# type: () -> None
self.cmd_opts.add_option(cmdoptions.requirements())
self.cmd_opts.add_option(cmdoptions.constraints())
self.cmd_opts.add_option(cmdoptions.no_deps())
self.cmd_opts.add_option(cmdoptions.pre())
cmd_opts = self.cmd_opts
cmd_opts.add_option(cmdoptions.requirements())
cmd_opts.add_option(cmdoptions.constraints())
cmd_opts.add_option(cmdoptions.no_deps())
cmd_opts.add_option(cmdoptions.pre())
cmd_opts.add_option(cmdoptions.editable())
cmd_opts.add_option(
self.cmd_opts.add_option(cmdoptions.editable())
self.cmd_opts.add_option(
'-t', '--target',
dest='target_dir',
metavar='dir',
@ -108,9 +105,9 @@ class InstallCommand(RequirementCommand):
'<dir>. Use --upgrade to replace existing packages in <dir> '
'with new versions.'
)
cmdoptions.add_target_python_options(cmd_opts)
cmdoptions.add_target_python_options(self.cmd_opts)
cmd_opts.add_option(
self.cmd_opts.add_option(
'--user',
dest='use_user_site',
action='store_true',
@ -118,19 +115,19 @@ class InstallCommand(RequirementCommand):
"platform. Typically ~/.local/, or %APPDATA%\\Python on "
"Windows. (See the Python documentation for site.USER_BASE "
"for full details.)")
cmd_opts.add_option(
self.cmd_opts.add_option(
'--no-user',
dest='use_user_site',
action='store_false',
help=SUPPRESS_HELP)
cmd_opts.add_option(
self.cmd_opts.add_option(
'--root',
dest='root_path',
metavar='dir',
default=None,
help="Install everything relative to this alternate root "
"directory.")
cmd_opts.add_option(
self.cmd_opts.add_option(
'--prefix',
dest='prefix_path',
metavar='dir',
@ -138,11 +135,11 @@ class InstallCommand(RequirementCommand):
help="Installation prefix where lib, bin and other top-level "
"folders are placed")
cmd_opts.add_option(cmdoptions.build_dir())
self.cmd_opts.add_option(cmdoptions.build_dir())
cmd_opts.add_option(cmdoptions.src())
self.cmd_opts.add_option(cmdoptions.src())
cmd_opts.add_option(
self.cmd_opts.add_option(
'-U', '--upgrade',
dest='upgrade',
action='store_true',
@ -151,7 +148,7 @@ class InstallCommand(RequirementCommand):
'upgrade-strategy used.'
)
cmd_opts.add_option(
self.cmd_opts.add_option(
'--upgrade-strategy',
dest='upgrade_strategy',
default='only-if-needed',
@ -165,14 +162,14 @@ class InstallCommand(RequirementCommand):
'satisfy the requirements of the upgraded package(s).'
)
cmd_opts.add_option(
self.cmd_opts.add_option(
'--force-reinstall',
dest='force_reinstall',
action='store_true',
help='Reinstall all packages even if they are already '
'up-to-date.')
cmd_opts.add_option(
self.cmd_opts.add_option(
'-I', '--ignore-installed',
dest='ignore_installed',
action='store_true',
@ -182,15 +179,15 @@ class InstallCommand(RequirementCommand):
'with a different package manager!'
)
cmd_opts.add_option(cmdoptions.ignore_requires_python())
cmd_opts.add_option(cmdoptions.no_build_isolation())
cmd_opts.add_option(cmdoptions.use_pep517())
cmd_opts.add_option(cmdoptions.no_use_pep517())
self.cmd_opts.add_option(cmdoptions.ignore_requires_python())
self.cmd_opts.add_option(cmdoptions.no_build_isolation())
self.cmd_opts.add_option(cmdoptions.use_pep517())
self.cmd_opts.add_option(cmdoptions.no_use_pep517())
cmd_opts.add_option(cmdoptions.install_options())
cmd_opts.add_option(cmdoptions.global_options())
self.cmd_opts.add_option(cmdoptions.install_options())
self.cmd_opts.add_option(cmdoptions.global_options())
cmd_opts.add_option(
self.cmd_opts.add_option(
"--compile",
action="store_true",
dest="compile",
@ -198,21 +195,21 @@ class InstallCommand(RequirementCommand):
help="Compile Python source files to bytecode",
)
cmd_opts.add_option(
self.cmd_opts.add_option(
"--no-compile",
action="store_false",
dest="compile",
help="Do not compile Python source files to bytecode",
)
cmd_opts.add_option(
self.cmd_opts.add_option(
"--no-warn-script-location",
action="store_false",
dest="warn_script_location",
default=True,
help="Do not warn when installing scripts outside PATH",
)
cmd_opts.add_option(
self.cmd_opts.add_option(
"--no-warn-conflicts",
action="store_false",
dest="warn_about_conflicts",
@ -220,11 +217,11 @@ class InstallCommand(RequirementCommand):
help="Do not warn about broken dependencies",
)
cmd_opts.add_option(cmdoptions.no_binary())
cmd_opts.add_option(cmdoptions.only_binary())
cmd_opts.add_option(cmdoptions.prefer_binary())
cmd_opts.add_option(cmdoptions.require_hashes())
cmd_opts.add_option(cmdoptions.progress_bar())
self.cmd_opts.add_option(cmdoptions.no_binary())
self.cmd_opts.add_option(cmdoptions.only_binary())
self.cmd_opts.add_option(cmdoptions.prefer_binary())
self.cmd_opts.add_option(cmdoptions.require_hashes())
self.cmd_opts.add_option(cmdoptions.progress_bar())
index_opts = cmdoptions.make_option_group(
cmdoptions.index_group,
@ -232,7 +229,7 @@ class InstallCommand(RequirementCommand):
)
self.parser.insert_option_group(0, index_opts)
self.parser.insert_option_group(0, cmd_opts)
self.parser.insert_option_group(0, self.cmd_opts)
@with_cleanup
def run(self, options, args):

View File

@ -23,7 +23,7 @@ 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 typing import List, Set, Tuple, Iterator
from pip._internal.network.session import PipSession
from pip._vendor.pkg_resources import Distribution
@ -41,28 +41,24 @@ 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
cmd_opts.add_option(
def add_options(self):
# type: () -> None
self.cmd_opts.add_option(
'-o', '--outdated',
action='store_true',
default=False,
help='List outdated packages')
cmd_opts.add_option(
self.cmd_opts.add_option(
'-u', '--uptodate',
action='store_true',
default=False,
help='List uptodate packages')
cmd_opts.add_option(
self.cmd_opts.add_option(
'-e', '--editable',
action='store_true',
default=False,
help='List editable projects.')
cmd_opts.add_option(
self.cmd_opts.add_option(
'-l', '--local',
action='store_true',
default=False,
@ -75,8 +71,8 @@ class ListCommand(IndexGroupCommand):
action='store_true',
default=False,
help='Only output packages installed in user-site.')
cmd_opts.add_option(cmdoptions.list_path())
cmd_opts.add_option(
self.cmd_opts.add_option(cmdoptions.list_path())
self.cmd_opts.add_option(
'--pre',
action='store_true',
default=False,
@ -84,7 +80,7 @@ class ListCommand(IndexGroupCommand):
"pip only finds stable versions."),
)
cmd_opts.add_option(
self.cmd_opts.add_option(
'--format',
action='store',
dest='list_format',
@ -94,7 +90,7 @@ class ListCommand(IndexGroupCommand):
"or json",
)
cmd_opts.add_option(
self.cmd_opts.add_option(
'--not-required',
action='store_true',
dest='not_required',
@ -102,13 +98,13 @@ class ListCommand(IndexGroupCommand):
"installed packages.",
)
cmd_opts.add_option(
self.cmd_opts.add_option(
'--exclude-editable',
action='store_false',
dest='include_editable',
help='Exclude editable package from output.',
)
cmd_opts.add_option(
self.cmd_opts.add_option(
'--include-editable',
action='store_true',
dest='include_editable',
@ -120,7 +116,7 @@ class ListCommand(IndexGroupCommand):
)
self.parser.insert_option_group(0, index_opts)
self.parser.insert_option_group(0, cmd_opts)
self.parser.insert_option_group(0, self.cmd_opts)
def _build_package_finder(self, options, session):
# type: (Values, PipSession) -> PackageFinder

View File

@ -24,7 +24,7 @@ from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
from optparse import Values
from typing import Any, List, Dict, Optional
from typing import List, Dict, Optional
from typing_extensions import TypedDict
TransformedHit = TypedDict(
'TransformedHit',
@ -41,9 +41,8 @@ class SearchCommand(Command, SessionCommandMixin):
%prog [options] <query>"""
ignore_require_venv = True
def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(SearchCommand, self).__init__(*args, **kw)
def add_options(self):
# type: () -> None
self.cmd_opts.add_option(
'-i', '--index',
dest='index',

View File

@ -14,7 +14,7 @@ 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 typing import List, Dict, Iterator
logger = logging.getLogger(__name__)
@ -30,9 +30,8 @@ class ShowCommand(Command):
%prog [options] <package> ..."""
ignore_require_venv = True
def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(ShowCommand, self).__init__(*args, **kw)
def add_options(self):
# type: () -> None
self.cmd_opts.add_option(
'-f', '--files',
dest='files',

View File

@ -16,7 +16,7 @@ from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
from optparse import Values
from typing import Any, List
from typing import List
class UninstallCommand(Command, SessionCommandMixin):
@ -34,9 +34,8 @@ class UninstallCommand(Command, SessionCommandMixin):
%prog [options] <package> ...
%prog [options] -r <requirements file> ..."""
def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(UninstallCommand, self).__init__(*args, **kw)
def add_options(self):
# type: () -> None
self.cmd_opts.add_option(
'-r', '--requirement',
dest='requirements',

View File

@ -19,7 +19,7 @@ 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 typing import List
logger = logging.getLogger(__name__)
@ -47,13 +47,10 @@ class WheelCommand(RequirementCommand):
%prog [options] [-e] <local project path> ...
%prog [options] <archive url/path> ..."""
def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(WheelCommand, self).__init__(*args, **kw)
def add_options(self):
# type: () -> None
cmd_opts = self.cmd_opts
cmd_opts.add_option(
self.cmd_opts.add_option(
'-w', '--wheel-dir',
dest='wheel_dir',
metavar='dir',
@ -61,29 +58,29 @@ class WheelCommand(RequirementCommand):
help=("Build wheels into <dir>, where the default is the "
"current working directory."),
)
cmd_opts.add_option(cmdoptions.no_binary())
cmd_opts.add_option(cmdoptions.only_binary())
cmd_opts.add_option(cmdoptions.prefer_binary())
cmd_opts.add_option(
self.cmd_opts.add_option(cmdoptions.no_binary())
self.cmd_opts.add_option(cmdoptions.only_binary())
self.cmd_opts.add_option(cmdoptions.prefer_binary())
self.cmd_opts.add_option(
'--build-option',
dest='build_options',
metavar='options',
action='append',
help="Extra arguments to be supplied to 'setup.py bdist_wheel'.",
)
cmd_opts.add_option(cmdoptions.no_build_isolation())
cmd_opts.add_option(cmdoptions.use_pep517())
cmd_opts.add_option(cmdoptions.no_use_pep517())
cmd_opts.add_option(cmdoptions.constraints())
cmd_opts.add_option(cmdoptions.editable())
cmd_opts.add_option(cmdoptions.requirements())
cmd_opts.add_option(cmdoptions.src())
cmd_opts.add_option(cmdoptions.ignore_requires_python())
cmd_opts.add_option(cmdoptions.no_deps())
cmd_opts.add_option(cmdoptions.build_dir())
cmd_opts.add_option(cmdoptions.progress_bar())
self.cmd_opts.add_option(cmdoptions.no_build_isolation())
self.cmd_opts.add_option(cmdoptions.use_pep517())
self.cmd_opts.add_option(cmdoptions.no_use_pep517())
self.cmd_opts.add_option(cmdoptions.constraints())
self.cmd_opts.add_option(cmdoptions.editable())
self.cmd_opts.add_option(cmdoptions.requirements())
self.cmd_opts.add_option(cmdoptions.src())
self.cmd_opts.add_option(cmdoptions.ignore_requires_python())
self.cmd_opts.add_option(cmdoptions.no_deps())
self.cmd_opts.add_option(cmdoptions.build_dir())
self.cmd_opts.add_option(cmdoptions.progress_bar())
cmd_opts.add_option(
self.cmd_opts.add_option(
'--global-option',
dest='global_options',
action='append',
@ -91,7 +88,7 @@ class WheelCommand(RequirementCommand):
help="Extra global options to be supplied to the setup.py "
"call before the 'bdist_wheel' command.")
cmd_opts.add_option(
self.cmd_opts.add_option(
'--pre',
action='store_true',
default=False,
@ -99,7 +96,7 @@ class WheelCommand(RequirementCommand):
"pip only finds stable versions."),
)
cmd_opts.add_option(cmdoptions.require_hashes())
self.cmd_opts.add_option(cmdoptions.require_hashes())
index_opts = cmdoptions.make_option_group(
cmdoptions.index_group,
@ -107,7 +104,7 @@ class WheelCommand(RequirementCommand):
)
self.parser.insert_option_group(0, index_opts)
self.parser.insert_option_group(0, cmd_opts)
self.parser.insert_option_group(0, self.cmd_opts)
@with_cleanup
def run(self, options, args):

View File

@ -9,6 +9,8 @@ class SimpleCommand(Command):
def __init__(self):
super(SimpleCommand, self).__init__('fake', 'fake summary')
def add_options(self):
self.cmd_opts.add_option(cmdoptions.no_binary())
self.cmd_opts.add_option(cmdoptions.only_binary())