Skip to content

Commit 4b3ecda

Browse files
authored
Merge pull request #9412 from jdufresne/kwargs
Use keyword-only arguments
2 parents 503e6d6 + 8683413 commit 4b3ecda

File tree

7 files changed

+70
-50
lines changed

7 files changed

+70
-50
lines changed

news/287c6463-d2a1-41f5-a2df-d11f8460551a.trivial.rst

Whitespace-only changes.

src/pip/_internal/cli/base_command.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,18 @@ class Command(CommandContextMixIn):
5252
def __init__(self, name, summary, isolated=False):
5353
# type: (str, str, bool) -> None
5454
super().__init__()
55-
parser_kw = {
56-
'usage': self.usage,
57-
'prog': f'{get_prog()} {name}',
58-
'formatter': UpdatingDefaultsHelpFormatter(),
59-
'add_help_option': False,
60-
'name': name,
61-
'description': self.__doc__,
62-
'isolated': isolated,
63-
}
6455

6556
self.name = name
6657
self.summary = summary
67-
self.parser = ConfigOptionParser(**parser_kw)
58+
self.parser = ConfigOptionParser(
59+
usage=self.usage,
60+
prog=f'{get_prog()} {name}',
61+
formatter=UpdatingDefaultsHelpFormatter(),
62+
add_help_option=False,
63+
name=name,
64+
description=self.__doc__,
65+
isolated=isolated,
66+
)
6867

6968
self.tempdir_registry = None # type: Optional[TempDirRegistry]
7069

src/pip/_internal/cli/main_parser.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ def create_main_parser():
2323
"""Creates and returns the main parser for pip's CLI
2424
"""
2525

26-
parser_kw = {
27-
'usage': '\n%prog <command> [options]',
28-
'add_help_option': False,
29-
'formatter': UpdatingDefaultsHelpFormatter(),
30-
'name': 'global',
31-
'prog': get_prog(),
32-
}
33-
34-
parser = ConfigOptionParser(**parser_kw)
26+
parser = ConfigOptionParser(
27+
usage='\n%prog <command> [options]',
28+
add_help_option=False,
29+
formatter=UpdatingDefaultsHelpFormatter(),
30+
name='global',
31+
prog=get_prog(),
32+
)
3533
parser.disable_interspersed_args()
3634

3735
parser.version = get_pip_version()

src/pip/_internal/cli/parser.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
from pip._internal.cli.status_codes import UNKNOWN_ERROR
1515
from pip._internal.configuration import Configuration, ConfigurationError
1616
from pip._internal.utils.misc import redact_auth_from_url, strtobool
17+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
18+
19+
if MYPY_CHECK_RUNNING:
20+
from typing import Any
1721

1822
logger = logging.getLogger(__name__)
1923

@@ -154,10 +158,15 @@ class ConfigOptionParser(CustomOptionParser):
154158
"""Custom option parser which updates its defaults by checking the
155159
configuration files and environmental variables"""
156160

157-
def __init__(self, *args, **kwargs):
158-
self.name = kwargs.pop('name')
159-
160-
isolated = kwargs.pop("isolated", False)
161+
def __init__(
162+
self,
163+
*args, # type: Any
164+
name, # type: str
165+
isolated=False, # type: bool
166+
**kwargs, # type: Any
167+
):
168+
# type: (...) -> None
169+
self.name = name
161170
self.config = Configuration(isolated)
162171

163172
assert self.name

src/pip/_internal/network/session.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from pip._internal.utils.urls import url_to_path
4040

4141
if MYPY_CHECK_RUNNING:
42-
from typing import Iterator, List, Optional, Tuple, Union
42+
from typing import Any, Iterator, List, Optional, Sequence, Tuple, Union
4343

4444
from pip._internal.models.link import Link
4545

@@ -225,16 +225,20 @@ class PipSession(requests.Session):
225225

226226
timeout = None # type: Optional[int]
227227

228-
def __init__(self, *args, **kwargs):
228+
def __init__(
229+
self,
230+
*args, # type: Any
231+
retries=0, # type: int
232+
cache=None, # type: Optional[str]
233+
trusted_hosts=(), # type: Sequence[str]
234+
index_urls=None, # type: Optional[List[str]]
235+
**kwargs, # type: Any
236+
):
237+
# type: (...) -> None
229238
"""
230239
:param trusted_hosts: Domains not to emit warnings for when not using
231240
HTTPS.
232241
"""
233-
retries = kwargs.pop("retries", 0)
234-
cache = kwargs.pop("cache", None)
235-
trusted_hosts = kwargs.pop("trusted_hosts", []) # type: List[str]
236-
index_urls = kwargs.pop("index_urls", None)
237-
238242
super().__init__(*args, **kwargs)
239243

240244
# Namespace the attribute with "pip_" just in case to prevent

src/pip/_internal/utils/logging.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
from pip._internal.utils.compat import WINDOWS
1313
from pip._internal.utils.deprecation import DEPRECATION_MSG_PREFIX
1414
from pip._internal.utils.misc import ensure_dir
15+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
16+
17+
if MYPY_CHECK_RUNNING:
18+
from typing import Any
1519

1620
try:
1721
import threading
@@ -83,14 +87,20 @@ def get_indentation():
8387
class IndentingFormatter(logging.Formatter):
8488
default_time_format = "%Y-%m-%dT%H:%M:%S"
8589

86-
def __init__(self, *args, **kwargs):
90+
def __init__(
91+
self,
92+
*args, # type: Any
93+
add_timestamp=False, # type: bool
94+
**kwargs, # type: Any
95+
):
96+
# type: (...) -> None
8797
"""
8898
A logging.Formatter that obeys the indent_log() context manager.
8999
90100
:param add_timestamp: A bool indicating output lines should be prefixed
91101
with their record's timestamp.
92102
"""
93-
self.add_timestamp = kwargs.pop("add_timestamp", False)
103+
self.add_timestamp = add_timestamp
94104
super().__init__(*args, **kwargs)
95105

96106
def get_message_start(self, formatted, levelno):

tests/lib/__init__.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,15 @@ class PipTestEnvironment(TestFileEnvironment):
454454
exe = sys.platform == 'win32' and '.exe' or ''
455455
verbose = False
456456

457-
def __init__(self, base_path, *args, **kwargs):
457+
def __init__(self, base_path, *args, virtualenv, pip_expect_warning=None, **kwargs):
458458
# Make our base_path a test.lib.path.Path object
459459
base_path = Path(base_path)
460460

461461
# Store paths related to the virtual environment
462-
venv = kwargs.pop("virtualenv")
463-
self.venv_path = venv.location
464-
self.lib_path = venv.lib
465-
self.site_packages_path = venv.site
466-
self.bin_path = venv.bin
462+
self.venv_path = virtualenv.location
463+
self.lib_path = virtualenv.lib
464+
self.site_packages_path = virtualenv.site
465+
self.bin_path = virtualenv.bin
467466

468467
self.user_base_path = self.venv_path.joinpath("user")
469468
self.user_site_path = self.venv_path.joinpath(
@@ -503,7 +502,7 @@ def __init__(self, base_path, *args, **kwargs):
503502

504503
# Whether all pip invocations should expect stderr
505504
# (useful for Python version deprecation)
506-
self.pip_expect_warning = kwargs.pop('pip_expect_warning', None)
505+
self.pip_expect_warning = pip_expect_warning
507506

508507
# Call the TestFileEnvironment __init__
509508
super().__init__(base_path, *args, **kwargs)
@@ -544,7 +543,16 @@ def _find_traverse(self, path, result):
544543
else:
545544
super()._find_traverse(path, result)
546545

547-
def run(self, *args, **kw):
546+
def run(
547+
self,
548+
*args,
549+
cwd=None,
550+
run_from=None,
551+
allow_stderr_error=None,
552+
allow_stderr_warning=None,
553+
allow_error=None,
554+
**kw,
555+
):
548556
"""
549557
:param allow_stderr_error: whether a logged error is allowed in
550558
stderr. Passing True for this argument implies
@@ -567,20 +575,12 @@ def run(self, *args, **kw):
567575
if self.verbose:
568576
print('>> running {args} {kw}'.format(**locals()))
569577

570-
cwd = kw.pop('cwd', None)
571-
run_from = kw.pop('run_from', None)
572578
assert not cwd or not run_from, "Don't use run_from; it's going away"
573579
cwd = cwd or run_from or self.cwd
574580
if sys.platform == 'win32':
575581
# Partial fix for ScriptTest.run using `shell=True` on Windows.
576582
args = [str(a).replace('^', '^^').replace('&', '^&') for a in args]
577583

578-
# Remove `allow_stderr_error`, `allow_stderr_warning` and
579-
# `allow_error` before calling run() because PipTestEnvironment
580-
# doesn't support them.
581-
allow_stderr_error = kw.pop('allow_stderr_error', None)
582-
allow_stderr_warning = kw.pop('allow_stderr_warning', None)
583-
allow_error = kw.pop('allow_error', None)
584584
if allow_error:
585585
kw['expect_error'] = True
586586

@@ -634,11 +634,11 @@ def run(self, *args, **kw):
634634

635635
return TestPipResult(result, verbose=self.verbose)
636636

637-
def pip(self, *args, **kwargs):
637+
def pip(self, *args, use_module=True, **kwargs):
638638
__tracebackhide__ = True
639639
if self.pip_expect_warning:
640640
kwargs['allow_stderr_warning'] = True
641-
if kwargs.pop('use_module', True):
641+
if use_module:
642642
exe = 'python'
643643
args = ('-m', 'pip') + args
644644
else:

0 commit comments

Comments
 (0)