Skip to content

Commit abb67e6

Browse files
authored
Deprecate unnamed arguments to the run() function (#62)
1 parent 29d5c06 commit abb67e6

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

HISTORY.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
History
33
=======
44

5+
2.2.0
6+
-----
7+
* Calling the run() function with unnamed arguments (other than the command
8+
list as the first argument) is now deprecated. As a number of arguments
9+
will be removed in a future version the use of unnamed arguments will
10+
cause future confusion. `Use explicit keyword arguments instead (#62). <https://github.com/DiamondLightSource/python-procrunner/pull/62>`_
11+
512
2.1.0 (2020-09-05)
613
------------------
714
* `Deprecated array access on the return object (#60). <https://github.com/DiamondLightSource/python-procrunner/pull/60>`_

procrunner/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import codecs
2+
import functools
23
import io
34
import logging
45
import os
@@ -413,6 +414,22 @@ def update(self, dictionary):
413414
self._extras.update(dictionary)
414415

415416

417+
def _deprecate_argument_calling(f):
418+
@functools.wraps(f)
419+
def wrapper(*args, **kwargs):
420+
if len(args) > 1:
421+
warnings.warn(
422+
"Calling procrunner.run() with unnamed arguments (apart from "
423+
"the command) is deprecated. Use keyword arguments instead.",
424+
DeprecationWarning,
425+
stacklevel=2,
426+
)
427+
return f(*args, **kwargs)
428+
429+
return wrapper
430+
431+
432+
@_deprecate_argument_calling
416433
def run(
417434
command,
418435
timeout=None,

tests/test_procrunner.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_run_command_aborts_after_timeout_legacy(
2121

2222
with pytest.raises(RuntimeError):
2323
with pytest.warns(DeprecationWarning, match="timeout"):
24-
procrunner.run(task, -1, False)
24+
procrunner.run(task, timeout=-1, debug=False)
2525

2626
assert mock_subprocess.Popen.called
2727
assert mock_process.terminate.called
@@ -42,7 +42,7 @@ def test_run_command_aborts_after_timeout(
4242
task = ["___"]
4343

4444
with pytest.raises(RuntimeError):
45-
procrunner.run(task, -1, False, raise_timeout_exception=True)
45+
procrunner.run(task, timeout=-1, debug=False, raise_timeout_exception=True)
4646

4747
assert mock_subprocess.Popen.called
4848
assert mock_process.terminate.called
@@ -83,8 +83,8 @@ def streamreader_processing(*args, **kwargs):
8383

8484
actual = procrunner.run(
8585
command,
86-
0.5,
87-
False,
86+
timeout=0.5,
87+
debug=False,
8888
callback_stdout=mock.sentinel.callback_stdout,
8989
callback_stderr=mock.sentinel.callback_stderr,
9090
working_directory=mock.sentinel.cwd,
@@ -128,7 +128,9 @@ def streamreader_processing(*args, **kwargs):
128128
def test_default_process_environment_is_parent_environment(mock_subprocess):
129129
mock_subprocess.Popen.side_effect = NotImplementedError() # cut calls short
130130
with pytest.raises(NotImplementedError):
131-
procrunner.run([mock.Mock()], -1, False, raise_timeout_exception=True)
131+
procrunner.run(
132+
[mock.Mock()], timeout=-1, debug=False, raise_timeout_exception=True
133+
)
132134
assert mock_subprocess.Popen.call_args[1]["env"] == os.environ
133135

134136

@@ -140,8 +142,8 @@ def test_pass_custom_environment_to_process(mock_subprocess):
140142
with pytest.raises(NotImplementedError):
141143
procrunner.run(
142144
[mock.Mock()],
143-
-1,
144-
False,
145+
timeout=-1,
146+
debug=False,
145147
environment=copy.copy(mock_env),
146148
raise_timeout_exception=True,
147149
)
@@ -157,8 +159,8 @@ def test_pass_custom_environment_to_process_and_add_another_value(mock_subproces
157159
with pytest.raises(NotImplementedError):
158160
procrunner.run(
159161
[mock.Mock()],
160-
-1,
161-
False,
162+
timeout=-1,
163+
debug=False,
162164
environment=copy.copy(mock_env1),
163165
environment_override=copy.copy(mock_env2),
164166
raise_timeout_exception=True,
@@ -175,8 +177,8 @@ def test_use_default_process_environment_and_add_another_value(mock_subprocess):
175177
with pytest.raises(NotImplementedError):
176178
procrunner.run(
177179
[mock.Mock()],
178-
-1,
179-
False,
180+
timeout=-1,
181+
debug=False,
180182
environment_override=copy.copy(mock_env2),
181183
raise_timeout_exception=True,
182184
)
@@ -204,8 +206,8 @@ def test_use_default_process_environment_and_override_a_value(mock_subprocess):
204206
with pytest.raises(NotImplementedError):
205207
procrunner.run(
206208
[mock.Mock()],
207-
-1,
208-
False,
209+
timeout=-1,
210+
debug=False,
209211
environment_override={
210212
random_environment_variable: "X" + random_environment_value
211213
},

tests/test_procrunner_system.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,14 @@ def test_timeout_behaviour(tmp_path):
118118
assert te.value.stderr == b""
119119
assert te.value.timeout == 0.1
120120
assert te.value.cmd == command
121+
122+
123+
def test_argument_deprecation(tmp_path):
124+
with pytest.warns(DeprecationWarning, match="keyword arguments"):
125+
result = procrunner.run(
126+
[sys.executable, "-V"],
127+
None,
128+
working_directory=tmp_path,
129+
)
130+
assert not result.returncode
131+
assert result.stderr or result.stdout

0 commit comments

Comments
 (0)