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

View File

@@ -117,6 +117,10 @@ def _thread_mapper(maxsize: int) -> Generator[
def xargs(
cmd: Tuple[str, ...],
varargs: Sequence[str],
*,
color: bool = False,
target_concurrency: int = 1,
_max_length: int = _get_platform_max_length(),
**kwargs: Any,
) -> Tuple[int, bytes]:
"""A simplified implementation of xargs.
@@ -124,9 +128,6 @@ def xargs(
color: Make a pty if on a platform that supports it
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
retcode = 0
stdout = b''
@@ -136,7 +137,7 @@ def xargs(
except parse_shebang.ExecutableNotFoundError as e:
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(
run_cmd: Tuple[str, ...],

View File

@@ -18,13 +18,15 @@ def get_resource_path(path):
return os.path.join(TESTING_DIR, 'resources', path)
def cmd_output_mocked_pre_commit_home(*args, **kwargs):
# keyword-only argument
tempdir_factory = kwargs.pop('tempdir_factory')
pre_commit_home = kwargs.pop('pre_commit_home', tempdir_factory.get())
def cmd_output_mocked_pre_commit_home(
*args, tempdir_factory, pre_commit_home=None, env=None, **kwargs,
):
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)
# 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)
return ret, out.replace('\r\n', '\n'), None
@@ -123,9 +125,7 @@ def cwd(path):
os.chdir(original_cwd)
def git_commit(*args, **kwargs):
fn = kwargs.pop('fn', cmd_output)
msg = kwargs.pop('msg', 'commit!')
def git_commit(*args, fn=cmd_output, msg='commit!', **kwargs):
kwargs.setdefault('stderr', subprocess.STDOUT)
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
def _test(**kwargs):
before = kwargs.pop('before')
patch = kwargs.pop('patch')
expected = kwargs.pop('expected')
assert not kwargs
def _test(*, before, patch, expected):
env = before.copy()
with envcontext(patch, _env=env):
assert env == expected