keyword only arguments in some places

This commit is contained in:
Anthony Sottile 2020-01-12 12:19:07 -08:00
parent 34c3a1580a
commit 5779f93ec6
4 changed files with 17 additions and 20 deletions

View file

@ -108,9 +108,9 @@ def _setdefault_kwargs(kwargs: Dict[str, Any]) -> None:
def cmd_output_b( def cmd_output_b(
*cmd: str, *cmd: str,
retcode: Optional[int] = 0,
**kwargs: Any, **kwargs: Any,
) -> Tuple[int, bytes, Optional[bytes]]: ) -> Tuple[int, bytes, Optional[bytes]]:
retcode = kwargs.pop('retcode', 0)
_setdefault_kwargs(kwargs) _setdefault_kwargs(kwargs)
try: try:
@ -176,9 +176,10 @@ if os.name != 'nt': # pragma: windows no cover
def cmd_output_p( def cmd_output_p(
*cmd: str, *cmd: str,
retcode: Optional[int] = 0,
**kwargs: Any, **kwargs: Any,
) -> Tuple[int, bytes, Optional[bytes]]: ) -> Tuple[int, bytes, Optional[bytes]]:
assert kwargs.pop('retcode') is None assert retcode is None
assert kwargs['stderr'] == subprocess.STDOUT, kwargs['stderr'] assert kwargs['stderr'] == subprocess.STDOUT, kwargs['stderr']
_setdefault_kwargs(kwargs) _setdefault_kwargs(kwargs)

View file

@ -117,6 +117,10 @@ def _thread_mapper(maxsize: int) -> Generator[
def xargs( def xargs(
cmd: Tuple[str, ...], cmd: Tuple[str, ...],
varargs: Sequence[str], varargs: Sequence[str],
*,
color: bool = False,
target_concurrency: int = 1,
_max_length: int = _get_platform_max_length(),
**kwargs: Any, **kwargs: Any,
) -> Tuple[int, bytes]: ) -> Tuple[int, bytes]:
"""A simplified implementation of xargs. """A simplified implementation of xargs.
@ -124,9 +128,6 @@ def xargs(
color: Make a pty if on a platform that supports it color: Make a pty if on a platform that supports it
target_concurrency: Target number of partitions to run concurrently target_concurrency: Target number of partitions to run concurrently
""" """
color = kwargs.pop('color', False)
target_concurrency = kwargs.pop('target_concurrency', 1)
max_length = kwargs.pop('_max_length', _get_platform_max_length())
cmd_fn = cmd_output_p if color else cmd_output_b cmd_fn = cmd_output_p if color else cmd_output_b
retcode = 0 retcode = 0
stdout = b'' stdout = b''
@ -136,7 +137,7 @@ def xargs(
except parse_shebang.ExecutableNotFoundError as e: except parse_shebang.ExecutableNotFoundError as e:
return e.to_output()[:2] return e.to_output()[:2]
partitions = partition(cmd, varargs, target_concurrency, max_length) partitions = partition(cmd, varargs, target_concurrency, _max_length)
def run_cmd_partition( def run_cmd_partition(
run_cmd: Tuple[str, ...], run_cmd: Tuple[str, ...],

View file

@ -18,13 +18,15 @@ def get_resource_path(path):
return os.path.join(TESTING_DIR, 'resources', path) return os.path.join(TESTING_DIR, 'resources', path)
def cmd_output_mocked_pre_commit_home(*args, **kwargs): def cmd_output_mocked_pre_commit_home(
# keyword-only argument *args, tempdir_factory, pre_commit_home=None, env=None, **kwargs,
tempdir_factory = kwargs.pop('tempdir_factory') ):
pre_commit_home = kwargs.pop('pre_commit_home', tempdir_factory.get()) if pre_commit_home is None:
pre_commit_home = tempdir_factory.get()
env = env if env is not None else os.environ
kwargs.setdefault('stderr', subprocess.STDOUT) kwargs.setdefault('stderr', subprocess.STDOUT)
# Don't want to write to the home directory # Don't want to write to the home directory
env = dict(kwargs.pop('env', os.environ), PRE_COMMIT_HOME=pre_commit_home) env = dict(env, PRE_COMMIT_HOME=pre_commit_home)
ret, out, _ = cmd_output(*args, env=env, **kwargs) ret, out, _ = cmd_output(*args, env=env, **kwargs)
return ret, out.replace('\r\n', '\n'), None return ret, out.replace('\r\n', '\n'), None
@ -123,9 +125,7 @@ def cwd(path):
os.chdir(original_cwd) os.chdir(original_cwd)
def git_commit(*args, **kwargs): def git_commit(*args, fn=cmd_output, msg='commit!', **kwargs):
fn = kwargs.pop('fn', cmd_output)
msg = kwargs.pop('msg', 'commit!')
kwargs.setdefault('stderr', subprocess.STDOUT) kwargs.setdefault('stderr', subprocess.STDOUT)
cmd = ('git', 'commit', '--allow-empty', '--no-gpg-sign', '-a') + args cmd = ('git', 'commit', '--allow-empty', '--no-gpg-sign', '-a') + args

View file

@ -8,12 +8,7 @@ from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var from pre_commit.envcontext import Var
def _test(**kwargs): def _test(*, before, patch, expected):
before = kwargs.pop('before')
patch = kwargs.pop('patch')
expected = kwargs.pop('expected')
assert not kwargs
env = before.copy() env = before.copy()
with envcontext(patch, _env=env): with envcontext(patch, _env=env):
assert env == expected assert env == expected