Merge pull request #5960 from cytolentino/cmdoptions-typing

Add mypy annotations to pip._internal.cli.cmdoptions
This commit is contained in:
Pradyun Gedam 2018-10-31 01:38:35 +05:30 committed by GitHub
commit d95bc267fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 41 deletions

View File

@ -22,10 +22,13 @@ from pip._internal.utils.typing import MYPY_CHECK_RUNNING
from pip._internal.utils.ui import BAR_TYPES
if MYPY_CHECK_RUNNING:
from typing import Any # noqa: F401
from typing import Any, Callable, Dict, List, Optional, Union # noqa: F401
from optparse import OptionParser, Values # noqa: F401
from pip._internal.cli.parser import ConfigOptionParser # noqa: F401
def make_option_group(group, parser):
# type: (Dict[str, Any], ConfigOptionParser) -> OptionGroup
"""
Return an OptionGroup object
group -- assumed to be dict with 'name' and 'options' keys
@ -38,6 +41,7 @@ def make_option_group(group, parser):
def check_install_build_global(options, check_options=None):
# type: (Values, Optional[Values]) -> None
"""Disable wheels if per-setup.py call options are set.
:param options: The OptionParser options to update.
@ -60,6 +64,7 @@ def check_install_build_global(options, check_options=None):
def check_dist_restriction(options, check_target=False):
# type: (Values, bool) -> None
"""Function for determining if custom platform options are allowed.
:param options: The OptionParser options.
@ -108,7 +113,7 @@ help_ = partial(
dest='help',
action='help',
help='Show help.',
) # type: Any
) # type: partial[Option]
isolated_mode = partial(
Option,
@ -120,7 +125,7 @@ isolated_mode = partial(
"Run pip in an isolated mode, ignoring environment variables and user "
"configuration."
),
)
) # type: partial[Option]
require_virtualenv = partial(
Option,
@ -130,7 +135,7 @@ require_virtualenv = partial(
action='store_true',
default=False,
help=SUPPRESS_HELP
) # type: Any
) # type: partial[Option]
verbose = partial(
Option,
@ -139,7 +144,7 @@ verbose = partial(
action='count',
default=0,
help='Give more output. Option is additive, and can be used up to 3 times.'
)
) # type: partial[Option]
no_color = partial(
Option,
@ -148,7 +153,7 @@ no_color = partial(
action='store_true',
default=False,
help="Suppress colored output",
)
) # type: partial[Option]
version = partial(
Option,
@ -156,7 +161,7 @@ version = partial(
dest='version',
action='store_true',
help='Show version and exit.',
) # type: Any
) # type: partial[Option]
quiet = partial(
Option,
@ -169,7 +174,7 @@ quiet = partial(
' times (corresponding to WARNING, ERROR, and CRITICAL logging'
' levels).'
),
) # type: Any
) # type: partial[Option]
progress_bar = partial(
Option,
@ -182,7 +187,7 @@ progress_bar = partial(
'Specify type of progress to be displayed [' +
'|'.join(BAR_TYPES.keys()) + '] (default: %default)'
),
) # type: Any
) # type: partial[Option]
log = partial(
Option,
@ -190,7 +195,7 @@ log = partial(
dest="log",
metavar="path",
help="Path to a verbose appending log."
) # type: Any
) # type: partial[Option]
no_input = partial(
Option,
@ -200,7 +205,7 @@ no_input = partial(
action='store_true',
default=False,
help=SUPPRESS_HELP
) # type: Any
) # type: partial[Option]
proxy = partial(
Option,
@ -209,7 +214,7 @@ proxy = partial(
type='str',
default='',
help="Specify a proxy in the form [user:passwd@]proxy.server:port."
) # type: Any
) # type: partial[Option]
retries = partial(
Option,
@ -219,7 +224,7 @@ retries = partial(
default=5,
help="Maximum number of retries each connection should attempt "
"(default %default times).",
) # type: Any
) # type: partial[Option]
timeout = partial(
Option,
@ -229,7 +234,7 @@ timeout = partial(
type='float',
default=15,
help='Set the socket timeout (default %default seconds).',
) # type: Any
) # type: partial[Option]
skip_requirements_regex = partial(
Option,
@ -239,10 +244,11 @@ skip_requirements_regex = partial(
type='str',
default='',
help=SUPPRESS_HELP,
) # type: Any
) # type: partial[Option]
def exists_action():
# type: () -> Option
return Option(
# Option when path already exist
'--exists-action',
@ -264,7 +270,7 @@ cert = partial(
type='str',
metavar='path',
help="Path to alternate CA bundle.",
) # type: Any
) # type: partial[Option]
client_cert = partial(
Option,
@ -275,7 +281,7 @@ client_cert = partial(
metavar='path',
help="Path to SSL client certificate, a single file containing the "
"private key and the certificate in PEM format.",
) # type: Any
) # type: partial[Option]
index_url = partial(
Option,
@ -287,7 +293,7 @@ index_url = partial(
"This should point to a repository compliant with PEP 503 "
"(the simple repository API) or a local directory laid out "
"in the same format.",
) # type: Any
) # type: partial[Option]
def extra_index_url():
@ -310,10 +316,11 @@ no_index = partial(
action='store_true',
default=False,
help='Ignore package index (only looking at --find-links URLs instead).',
) # type: Any
) # type: partial[Option]
def find_links():
# type: () -> Option
return Option(
'-f', '--find-links',
dest='find_links',
@ -327,6 +334,7 @@ def find_links():
def trusted_host():
# type: () -> Option
return Option(
"--trusted-host",
dest="trusted_hosts",
@ -346,10 +354,11 @@ process_dependency_links = partial(
action="store_true",
default=False,
help="Enable the processing of dependency links.",
) # type: Any
) # type: partial[Option]
def constraints():
# type: () -> Option
return Option(
'-c', '--constraint',
dest='constraints',
@ -362,6 +371,7 @@ def constraints():
def requirements():
# type: () -> Option
return Option(
'-r', '--requirement',
dest='requirements',
@ -374,6 +384,7 @@ def requirements():
def editable():
# type: () -> Option
return Option(
'-e', '--editable',
dest='editables',
@ -394,15 +405,17 @@ src = partial(
help='Directory to check out editable projects into. '
'The default in a virtualenv is "<venv path>/src". '
'The default for global installs is "<current dir>/src".'
) # type: Any
) # type: partial[Option]
def _get_format_control(values, option):
# type: (Values, Option) -> Any
"""Get a format_control object."""
return getattr(values, option.dest)
def _handle_no_binary(option, opt_str, value, parser):
# type: (Option, str, str, OptionParser) -> None
existing = _get_format_control(parser.values, option)
FormatControl.handle_mutual_excludes(
value, existing.no_binary, existing.only_binary,
@ -410,6 +423,7 @@ def _handle_no_binary(option, opt_str, value, parser):
def _handle_only_binary(option, opt_str, value, parser):
# type: (Option, str, str, OptionParser) -> None
existing = _get_format_control(parser.values, option)
FormatControl.handle_mutual_excludes(
value, existing.only_binary, existing.no_binary,
@ -417,6 +431,7 @@ def _handle_only_binary(option, opt_str, value, parser):
def no_binary():
# type: () -> Option
format_control = FormatControl(set(), set())
return Option(
"--no-binary", dest="format_control", action="callback",
@ -432,6 +447,7 @@ def no_binary():
def only_binary():
# type: () -> Option
format_control = FormatControl(set(), set())
return Option(
"--only-binary", dest="format_control", action="callback",
@ -454,7 +470,7 @@ platform = partial(
default=None,
help=("Only use wheels compatible with <platform>. "
"Defaults to the platform of the running system."),
)
) # type: partial[Option]
python_version = partial(
@ -469,7 +485,7 @@ python_version = partial(
"version (e.g. '2') can be specified to match all "
"minor revs of that major version. A minor version "
"(e.g. '34') can also be specified."),
)
) # type: partial[Option]
implementation = partial(
@ -483,7 +499,7 @@ implementation = partial(
" or 'ip'. If not specified, then the current "
"interpreter implementation is used. Use 'py' to force "
"implementation-agnostic wheels."),
)
) # type: partial[Option]
abi = partial(
@ -498,10 +514,11 @@ abi = partial(
"you will need to specify --implementation, "
"--platform, and --python-version when using "
"this option."),
)
) # type: partial[Option]
def prefer_binary():
# type: () -> Option
return Option(
"--prefer-binary",
dest="prefer_binary",
@ -518,7 +535,7 @@ cache_dir = partial(
default=USER_CACHE_DIR,
metavar="dir",
help="Store the cache data in <dir>."
)
) # type: partial[Option]
no_cache = partial(
Option,
@ -526,7 +543,7 @@ no_cache = partial(
dest="cache_dir",
action="store_false",
help="Disable the cache.",
)
) # type: partial[Option]
no_deps = partial(
Option,
@ -535,7 +552,7 @@ no_deps = partial(
action='store_true',
default=False,
help="Don't install package dependencies.",
) # type: Any
) # type: partial[Option]
build_dir = partial(
Option,
@ -547,7 +564,7 @@ build_dir = partial(
'The location of temporary directories can be controlled by setting '
'the TMPDIR environment variable (TEMP on Windows) appropriately. '
'When passed, build directories are not cleaned in case of failures.'
) # type: Any
) # type: partial[Option]
ignore_requires_python = partial(
Option,
@ -555,7 +572,7 @@ ignore_requires_python = partial(
dest='ignore_requires_python',
action='store_true',
help='Ignore the Requires-Python information.'
) # type: Any
) # type: partial[Option]
no_build_isolation = partial(
Option,
@ -566,7 +583,7 @@ no_build_isolation = partial(
help='Disable isolation when building a modern source distribution. '
'Build dependencies specified by PEP 518 must be already installed '
'if this option is used.'
) # type: Any
) # type: partial[Option]
install_options = partial(
Option,
@ -579,7 +596,7 @@ install_options = partial(
"bin\"). Use multiple --install-option options to pass multiple "
"options to setup.py install. If you are using an option with a "
"directory path, be sure to use absolute path.",
) # type: Any
) # type: partial[Option]
global_options = partial(
Option,
@ -589,7 +606,7 @@ global_options = partial(
metavar='options',
help="Extra global options to be supplied to the setup.py "
"call before the install command.",
) # type: Any
) # type: partial[Option]
no_clean = partial(
Option,
@ -597,7 +614,7 @@ no_clean = partial(
action='store_true',
default=False,
help="Don't clean up build directories."
) # type: Any
) # type: partial[Option]
pre = partial(
Option,
@ -606,7 +623,7 @@ pre = partial(
default=False,
help="Include pre-release and development versions. By default, "
"pip only finds stable versions.",
) # type: Any
) # type: partial[Option]
disable_pip_version_check = partial(
Option,
@ -616,7 +633,7 @@ disable_pip_version_check = partial(
default=False,
help="Don't periodically check PyPI to determine whether a new version "
"of pip is available for download. Implied with --no-index.",
) # type: Any
) # type: partial[Option]
# Deprecated, Remove later
@ -626,10 +643,11 @@ always_unzip = partial(
dest='always_unzip',
action='store_true',
help=SUPPRESS_HELP,
) # type: Any
) # type: partial[Option]
def _merge_hash(option, opt_str, value, parser):
# type: (Option, str, str, OptionParser) -> None
"""Given a value spelled "algo:digest", append the digest to a list
pointed to in a dict by the algo name."""
if not parser.values.hashes:
@ -657,7 +675,7 @@ hash = partial(
type='string',
help="Verify that the package's archive matches this "
'hash before installing. Example: --hash=sha256:abcdef...',
) # type: Any
) # type: partial[Option]
require_hashes = partial(
@ -669,7 +687,7 @@ require_hashes = partial(
help='Require a hash to check each requirement against, for '
'repeatable installs. This option is implied when any package in a '
'requirements file has a --hash option.',
) # type: Any
) # type: partial[Option]
##########
@ -700,7 +718,7 @@ general_group = {
disable_pip_version_check,
no_color,
]
}
} # type: Dict[str, Any]
index_group = {
'name': 'Package Index Options',
@ -711,4 +729,4 @@ index_group = {
find_links,
process_dependency_links,
]
}
} # type: Dict[str, Any]

View File

@ -1,5 +1,10 @@
from pip._vendor.packaging.utils import canonicalize_name
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
if MYPY_CHECK_RUNNING:
from typing import Optional, Set # noqa: F401
class FormatControl(object):
"""A helper class for controlling formats from which packages are installed.
@ -9,6 +14,7 @@ class FormatControl(object):
are listed, with any given package only showing up in one field at a time.
"""
def __init__(self, no_binary=None, only_binary=None):
# type: (Optional[Set], Optional[Set]) -> None
self.no_binary = set() if no_binary is None else no_binary
self.only_binary = set() if only_binary is None else only_binary
@ -27,6 +33,7 @@ class FormatControl(object):
@staticmethod
def handle_mutual_excludes(value, target, other):
# type: (str, Optional[Set], Optional[Set]) -> None
new = value.split(',')
while ':all:' in new:
other.clear()