Skip to content

Commit d7ee3da

Browse files
committed
Type-annotate pytest.{exit,skip,fail,xfail,importorskip}
1 parent 866904a commit d7ee3da

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/_pytest/outcomes.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@
33
as well as functions creating them
44
"""
55
import sys
6+
from typing import Any
7+
from typing import Optional
68

79
from packaging.version import Version
810

11+
if False: # TYPE_CHECKING
12+
from typing import NoReturn
13+
914

1015
class OutcomeException(BaseException):
1116
""" OutcomeException and its subclass instances indicate and
1217
contain info about test and collection outcomes.
1318
"""
1419

15-
def __init__(self, msg=None, pytrace=True):
20+
def __init__(self, msg: Optional[str] = None, pytrace: bool = True) -> None:
1621
BaseException.__init__(self, msg)
1722
self.msg = msg
1823
self.pytrace = pytrace
1924

20-
def __repr__(self):
25+
def __repr__(self) -> str:
2126
if self.msg:
2227
val = self.msg
2328
if isinstance(val, bytes):
@@ -36,7 +41,12 @@ class Skipped(OutcomeException):
3641
# in order to have Skipped exception printing shorter/nicer
3742
__module__ = "builtins"
3843

39-
def __init__(self, msg=None, pytrace=True, allow_module_level=False):
44+
def __init__(
45+
self,
46+
msg: Optional[str] = None,
47+
pytrace: bool = True,
48+
allow_module_level: bool = False,
49+
) -> None:
4050
OutcomeException.__init__(self, msg=msg, pytrace=pytrace)
4151
self.allow_module_level = allow_module_level
4252

@@ -50,7 +60,9 @@ class Failed(OutcomeException):
5060
class Exit(Exception):
5161
""" raised for immediate program exits (no tracebacks/summaries)"""
5262

53-
def __init__(self, msg="unknown reason", returncode=None):
63+
def __init__(
64+
self, msg: str = "unknown reason", returncode: Optional[int] = None
65+
) -> None:
5466
self.msg = msg
5567
self.returncode = returncode
5668
super().__init__(msg)
@@ -59,7 +71,7 @@ def __init__(self, msg="unknown reason", returncode=None):
5971
# exposed helper methods
6072

6173

62-
def exit(msg, returncode=None):
74+
def exit(msg: str, returncode: Optional[int] = None) -> "NoReturn":
6375
"""
6476
Exit testing process.
6577
@@ -74,7 +86,7 @@ def exit(msg, returncode=None):
7486
exit.Exception = Exit # type: ignore
7587

7688

77-
def skip(msg="", *, allow_module_level=False):
89+
def skip(msg: str = "", *, allow_module_level: bool = False) -> "NoReturn":
7890
"""
7991
Skip an executing test with the given message.
8092
@@ -101,7 +113,7 @@ def skip(msg="", *, allow_module_level=False):
101113
skip.Exception = Skipped # type: ignore
102114

103115

104-
def fail(msg="", pytrace=True):
116+
def fail(msg: str = "", pytrace: bool = True) -> "NoReturn":
105117
"""
106118
Explicitly fail an executing test with the given message.
107119
@@ -121,7 +133,7 @@ class XFailed(Failed):
121133
""" raised from an explicit call to pytest.xfail() """
122134

123135

124-
def xfail(reason=""):
136+
def xfail(reason: str = "") -> "NoReturn":
125137
"""
126138
Imperatively xfail an executing test or setup functions with the given reason.
127139
@@ -139,7 +151,9 @@ def xfail(reason=""):
139151
xfail.Exception = XFailed # type: ignore
140152

141153

142-
def importorskip(modname, minversion=None, reason=None):
154+
def importorskip(
155+
modname: str, minversion: Optional[str] = None, reason: Optional[str] = None
156+
) -> Any:
143157
"""Imports and returns the requested module ``modname``, or skip the current test
144158
if the module cannot be imported.
145159

0 commit comments

Comments
 (0)