Skip to content

Commit eb02cc3

Browse files
committed
pytester: typing
1 parent f0c2b07 commit eb02cc3

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

src/_pytest/pytester.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import traceback
1111
from collections.abc import Sequence
1212
from fnmatch import fnmatch
13+
from typing import Union
1314
from weakref import WeakKeyDictionary
1415

1516
import py
@@ -361,7 +362,7 @@ class RunResult:
361362
:ivar duration: duration in seconds
362363
"""
363364

364-
def __init__(self, ret, outlines, errlines, duration):
365+
def __init__(self, ret: Union[int, ExitCode], outlines, errlines, duration):
365366
self.ret = ret
366367
self.outlines = outlines
367368
self.errlines = errlines
@@ -479,11 +480,7 @@ def __init__(self, request, tmpdir_factory):
479480
self._sys_modules_snapshot = self.__take_sys_modules_snapshot()
480481
self.chdir()
481482
self.request.addfinalizer(self.finalize)
482-
method = self.request.config.getoption("--runpytest")
483-
if method == "inprocess":
484-
self._runpytest_method = self.runpytest_inprocess
485-
elif method == "subprocess":
486-
self._runpytest_method = self.runpytest_subprocess
483+
self._method = self.request.config.getoption("--runpytest")
487484

488485
mp = self.monkeypatch = MonkeyPatch()
489486
mp.setenv("PYTEST_DEBUG_TEMPROOT", str(self.test_tmproot))
@@ -831,7 +828,7 @@ def pytest_configure(x, config):
831828
reprec = rec.pop()
832829
else:
833830

834-
class reprec:
831+
class reprec: # type: ignore
835832
pass
836833

837834
reprec.ret = ret
@@ -847,7 +844,7 @@ class reprec:
847844
for finalizer in finalizers:
848845
finalizer()
849846

850-
def runpytest_inprocess(self, *args, **kwargs):
847+
def runpytest_inprocess(self, *args, **kwargs) -> RunResult:
851848
"""Return result of running pytest in-process, providing a similar
852849
interface to what self.runpytest() provides.
853850
"""
@@ -862,15 +859,20 @@ def runpytest_inprocess(self, *args, **kwargs):
862859
try:
863860
reprec = self.inline_run(*args, **kwargs)
864861
except SystemExit as e:
862+
ret = e.args[0]
863+
try:
864+
ret = ExitCode(e.args[0])
865+
except ValueError:
866+
pass
865867

866-
class reprec:
867-
ret = e.args[0]
868+
class reprec: # type: ignore
869+
ret = ret
868870

869871
except Exception:
870872
traceback.print_exc()
871873

872-
class reprec:
873-
ret = 3
874+
class reprec: # type: ignore
875+
ret = ExitCode(3)
874876

875877
finally:
876878
out, err = capture.readouterr()
@@ -879,16 +881,19 @@ class reprec:
879881
sys.stderr.write(err)
880882

881883
res = RunResult(reprec.ret, out.split("\n"), err.split("\n"), time.time() - now)
882-
res.reprec = reprec
884+
res.reprec = reprec # type: ignore
883885
return res
884886

885-
def runpytest(self, *args, **kwargs):
887+
def runpytest(self, *args, **kwargs) -> RunResult:
886888
"""Run pytest inline or in a subprocess, depending on the command line
887889
option "--runpytest" and return a :py:class:`RunResult`.
888890
889891
"""
890892
args = self._ensure_basetemp(args)
891-
return self._runpytest_method(*args, **kwargs)
893+
if self._method == "inprocess":
894+
return self.runpytest_inprocess(*args, **kwargs)
895+
assert self._method == "subprocess"
896+
return self.runpytest_subprocess(*args, **kwargs)
892897

893898
def _ensure_basetemp(self, args):
894899
args = list(args)
@@ -1045,7 +1050,7 @@ def popen(
10451050

10461051
return popen
10471052

1048-
def run(self, *cmdargs, timeout=None, stdin=CLOSE_STDIN):
1053+
def run(self, *cmdargs, timeout=None, stdin=CLOSE_STDIN) -> RunResult:
10491054
"""Run a command with arguments.
10501055
10511056
Run a process using subprocess.Popen saving the stdout and stderr.
@@ -1063,9 +1068,9 @@ def run(self, *cmdargs, timeout=None, stdin=CLOSE_STDIN):
10631068
"""
10641069
__tracebackhide__ = True
10651070

1066-
cmdargs = [
1071+
cmdargs = tuple(
10671072
str(arg) if isinstance(arg, py.path.local) else arg for arg in cmdargs
1068-
]
1073+
)
10691074
p1 = self.tmpdir.join("stdout")
10701075
p2 = self.tmpdir.join("stderr")
10711076
print("running:", *cmdargs)
@@ -1116,6 +1121,10 @@ def handle_timeout():
11161121
f2.close()
11171122
self._dump_lines(out, sys.stdout)
11181123
self._dump_lines(err, sys.stderr)
1124+
try:
1125+
ret = ExitCode(ret)
1126+
except ValueError:
1127+
pass
11191128
return RunResult(ret, out, err, time.time() - now)
11201129

11211130
def _dump_lines(self, lines, fp):
@@ -1128,7 +1137,7 @@ def _dump_lines(self, lines, fp):
11281137
def _getpytestargs(self):
11291138
return sys.executable, "-mpytest"
11301139

1131-
def runpython(self, script):
1140+
def runpython(self, script) -> RunResult:
11321141
"""Run a python script using sys.executable as interpreter.
11331142
11341143
Returns a :py:class:`RunResult`.
@@ -1140,7 +1149,7 @@ def runpython_c(self, command):
11401149
"""Run python -c "command", return a :py:class:`RunResult`."""
11411150
return self.run(sys.executable, "-c", command)
11421151

1143-
def runpytest_subprocess(self, *args, timeout=None):
1152+
def runpytest_subprocess(self, *args, timeout=None) -> RunResult:
11441153
"""Run pytest as a subprocess with given arguments.
11451154
11461155
Any plugins added to the :py:attr:`plugins` list will be added using the

0 commit comments

Comments
 (0)