Skip to content

Commit 10074a0

Browse files
bluetechblueyed
authored andcommitted
typing: wrap_session
Pulled out of #6556.
1 parent 16ab892 commit 10074a0

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/_pytest/main.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import importlib
66
import os
77
import sys
8+
from typing import Callable
89
from typing import Dict
910
from typing import FrozenSet
1011
from typing import List
@@ -23,7 +24,7 @@
2324
from _pytest.config import UsageError
2425
from _pytest.fixtures import FixtureManager
2526
from _pytest.nodes import Node
26-
from _pytest.outcomes import exit
27+
from _pytest.outcomes import Exit
2728
from _pytest.runner import collect_one_node
2829
from _pytest.runner import SetupState
2930

@@ -194,7 +195,9 @@ def pytest_addoption(parser):
194195
)
195196

196197

197-
def wrap_session(config, doit):
198+
def wrap_session(
199+
config: Config, doit: Callable[[Config, "Session"], Optional[Union[int, ExitCode]]]
200+
) -> Union[int, ExitCode]:
198201
"""Skeleton command line program"""
199202
session = Session(config)
200203
session.exitstatus = ExitCode.OK
@@ -211,10 +214,10 @@ def wrap_session(config, doit):
211214
raise
212215
except Failed:
213216
session.exitstatus = ExitCode.TESTS_FAILED
214-
except (KeyboardInterrupt, exit.Exception):
217+
except (KeyboardInterrupt, Exit):
215218
excinfo = _pytest._code.ExceptionInfo.from_current()
216-
exitstatus = ExitCode.INTERRUPTED
217-
if isinstance(excinfo.value, exit.Exception):
219+
exitstatus = ExitCode.INTERRUPTED # type: Union[int, ExitCode]
220+
if isinstance(excinfo.value, Exit):
218221
if excinfo.value.returncode is not None:
219222
exitstatus = excinfo.value.returncode
220223
if initstate < 2:
@@ -228,7 +231,7 @@ def wrap_session(config, doit):
228231
excinfo = _pytest._code.ExceptionInfo.from_current()
229232
try:
230233
config.notify_exception(excinfo, config.option)
231-
except exit.Exception as exc:
234+
except Exit as exc:
232235
if exc.returncode is not None:
233236
session.exitstatus = exc.returncode
234237
sys.stderr.write("{}: {}\n".format(type(exc).__name__, exc))
@@ -237,14 +240,15 @@ def wrap_session(config, doit):
237240
sys.stderr.write("mainloop: caught unexpected SystemExit!\n")
238241

239242
finally:
240-
excinfo = None # Explicitly break reference cycle.
243+
# Explicitly break reference cycle.
244+
excinfo = None # type: ignore
241245
session.startdir.chdir()
242246
if initstate >= 2:
243247
try:
244248
config.hook.pytest_sessionfinish(
245249
session=session, exitstatus=session.exitstatus
246250
)
247-
except exit.Exception as exc:
251+
except Exit as exc:
248252
if exc.returncode is not None:
249253
session.exitstatus = exc.returncode
250254
sys.stderr.write("{}: {}\n".format(type(exc).__name__, exc))
@@ -387,6 +391,7 @@ class Session(nodes.FSCollector):
387391
_setupstate = None # type: SetupState
388392
# Set on the session by fixtures.pytest_sessionstart.
389393
_fixturemanager = None # type: FixtureManager
394+
exitstatus = None # type: Union[int, ExitCode]
390395

391396
def __init__(self, config: Config) -> None:
392397
nodes.FSCollector.__init__(

0 commit comments

Comments
 (0)