Skip to content

Commit 158bde7

Browse files
committed
_pytest._py.error: mypy typing
1 parent cfcaac5 commit 158bde7

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

src/_pytest/_py/error.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
"""
2-
create errno-specific classes for IO or os calls.
1+
"""create errno-specific classes for IO or os calls."""
2+
from __future__ import annotations
33

4-
"""
54
import errno
65
import os
76
import sys
7+
from typing import Callable
8+
from typing import TYPE_CHECKING
9+
from typing import TypeVar
10+
11+
if TYPE_CHECKING:
12+
from typing_extensions import ParamSpec
13+
14+
P = ParamSpec("P")
15+
16+
R = TypeVar("R")
817

918

1019
class Error(EnvironmentError):
11-
def __repr__(self):
20+
def __repr__(self) -> str:
1221
return "{}.{} {!r}: {} ".format(
1322
self.__class__.__module__,
1423
self.__class__.__name__,
@@ -17,7 +26,7 @@ def __repr__(self):
1726
# repr(self.args)
1827
)
1928

20-
def __str__(self):
29+
def __str__(self) -> str:
2130
s = "[{}]: {}".format(
2231
self.__class__.__doc__,
2332
" ".join(map(str, self.args)),
@@ -44,60 +53,57 @@ class ErrorMaker:
4453
subclass EnvironmentError.
4554
"""
4655

47-
Error = Error
48-
_errno2class = {}
56+
_errno2class: dict[int, type[Error]] = {}
4957

50-
def __getattr__(self, name):
58+
def __getattr__(self, name: str) -> type[Error]:
5159
if name[0] == "_":
5260
raise AttributeError(name)
5361
eno = getattr(errno, name)
5462
cls = self._geterrnoclass(eno)
5563
setattr(self, name, cls)
5664
return cls
5765

58-
def _geterrnoclass(self, eno):
66+
def _geterrnoclass(self, eno: int) -> type[Error]:
5967
try:
6068
return self._errno2class[eno]
6169
except KeyError:
6270
clsname = errno.errorcode.get(eno, "UnknownErrno%d" % (eno,))
63-
errorcls = type(Error)(
71+
errorcls = type(
6472
clsname,
6573
(Error,),
6674
{"__module__": "py.error", "__doc__": os.strerror(eno)},
6775
)
6876
self._errno2class[eno] = errorcls
6977
return errorcls
7078

71-
def checked_call(self, func, *args, **kwargs):
72-
"""call a function and raise an errno-exception if applicable."""
79+
def checked_call(
80+
self, func: Callable[P, R], *args: P.args, **kwargs: P.kwargs
81+
) -> R:
82+
"""Call a function and raise an errno-exception if applicable."""
7383
__tracebackhide__ = True
7484
try:
7585
return func(*args, **kwargs)
7686
except self.Error:
7787
raise
78-
except OSError:
79-
cls, value, tb = sys.exc_info()
88+
except OSError as value:
8089
if not hasattr(value, "errno"):
8190
raise
82-
__tracebackhide__ = False
8391
errno = value.errno
84-
try:
85-
if not isinstance(value, WindowsError):
86-
raise NameError
87-
except NameError:
88-
# we are not on Windows, or we got a proper OSError
89-
cls = self._geterrnoclass(errno)
90-
else:
92+
if sys.platform == "win32":
9193
try:
9294
cls = self._geterrnoclass(_winerrnomap[errno])
9395
except KeyError:
9496
raise value
97+
else:
98+
# we are not on Windows, or we got a proper OSError
99+
cls = self._geterrnoclass(errno)
100+
95101
raise cls(f"{func.__name__}{args!r}")
96-
__tracebackhide__ = True
97102

98103

99104
_error_maker = ErrorMaker()
105+
checked_call = _error_maker.checked_call
100106

101107

102-
def __getattr__(attr):
103-
return getattr(_error_maker, attr)
108+
def __getattr__(attr: str) -> type[Error]:
109+
return getattr(_error_maker, attr) # type: ignore[no-any-return]

0 commit comments

Comments
 (0)