1
1
from io import StringIO
2
2
from pprint import pprint
3
3
from typing import Any
4
+ from typing import cast
4
5
from typing import Dict
5
6
from typing import Iterable
6
7
from typing import Iterator
15
16
16
17
from _pytest ._code .code import ExceptionChainRepr
17
18
from _pytest ._code .code import ExceptionInfo
19
+ from _pytest ._code .code import ExceptionRepr
18
20
from _pytest ._code .code import ReprEntry
19
21
from _pytest ._code .code import ReprEntryNative
20
22
from _pytest ._code .code import ReprExceptionInfo
@@ -57,8 +59,9 @@ def getworkerinfoline(node):
57
59
class BaseReport :
58
60
when = None # type: Optional[str]
59
61
location = None # type: Optional[Tuple[str, Optional[int], str]]
60
- # TODO: Improve this Any.
61
- longrepr = None # type: Optional[Any]
62
+ longrepr = (
63
+ None
64
+ ) # type: Union[None, ExceptionInfo[BaseException], Tuple[str, int, str], str, TerminalRepr]
62
65
sections = [] # type: List[Tuple[str, str]]
63
66
nodeid = None # type: str
64
67
@@ -79,7 +82,8 @@ def toterminal(self, out: TerminalWriter) -> None:
79
82
return
80
83
81
84
if hasattr (longrepr , "toterminal" ):
82
- longrepr .toterminal (out )
85
+ longrepr_terminal = cast (TerminalRepr , longrepr )
86
+ longrepr_terminal .toterminal (out )
83
87
else :
84
88
try :
85
89
s = str (longrepr )
@@ -233,7 +237,9 @@ def __init__(
233
237
location : Tuple [str , Optional [int ], str ],
234
238
keywords ,
235
239
outcome : "Literal['passed', 'failed', 'skipped']" ,
236
- longrepr ,
240
+ longrepr : Union [
241
+ None , ExceptionInfo [BaseException ], Tuple [str , int , str ], str , TerminalRepr
242
+ ],
237
243
when : "Literal['setup', 'call', 'teardown']" ,
238
244
sections : Iterable [Tuple [str , str ]] = (),
239
245
duration : float = 0 ,
@@ -293,8 +299,9 @@ def from_item_and_call(cls, item: Item, call: "CallInfo[None]") -> "TestReport":
293
299
sections = []
294
300
if not call .excinfo :
295
301
outcome = "passed" # type: Literal["passed", "failed", "skipped"]
296
- # TODO: Improve this Any.
297
- longrepr = None # type: Optional[Any]
302
+ longrepr = (
303
+ None
304
+ ) # type: Union[None, ExceptionInfo[BaseException], Tuple[str, int, str], str, TerminalRepr]
298
305
else :
299
306
if not isinstance (excinfo , ExceptionInfo ):
300
307
outcome = "failed"
@@ -372,7 +379,7 @@ def __repr__(self) -> str:
372
379
373
380
374
381
class CollectErrorRepr (TerminalRepr ):
375
- def __init__ (self , msg ) -> None :
382
+ def __init__ (self , msg : str ) -> None :
376
383
self .longrepr = msg
377
384
378
385
def toterminal (self , out : TerminalWriter ) -> None :
@@ -436,16 +443,18 @@ def serialize_repr_crash(
436
443
else :
437
444
return None
438
445
439
- def serialize_longrepr (rep : BaseReport ) -> Dict [str , Any ]:
446
+ def serialize_exception_longrepr (rep : BaseReport ) -> Dict [str , Any ]:
440
447
assert rep .longrepr is not None
448
+ # TODO: Investigate whether the duck typing is really necessary here.
449
+ longrepr = cast (ExceptionRepr , rep .longrepr )
441
450
result = {
442
- "reprcrash" : serialize_repr_crash (rep . longrepr .reprcrash ),
443
- "reprtraceback" : serialize_repr_traceback (rep . longrepr .reprtraceback ),
444
- "sections" : rep . longrepr .sections ,
451
+ "reprcrash" : serialize_repr_crash (longrepr .reprcrash ),
452
+ "reprtraceback" : serialize_repr_traceback (longrepr .reprtraceback ),
453
+ "sections" : longrepr .sections ,
445
454
} # type: Dict[str, Any]
446
- if isinstance (rep . longrepr , ExceptionChainRepr ):
455
+ if isinstance (longrepr , ExceptionChainRepr ):
447
456
result ["chain" ] = []
448
- for repr_traceback , repr_crash , description in rep . longrepr .chain :
457
+ for repr_traceback , repr_crash , description in longrepr .chain :
449
458
result ["chain" ].append (
450
459
(
451
460
serialize_repr_traceback (repr_traceback ),
@@ -462,7 +471,7 @@ def serialize_longrepr(rep: BaseReport) -> Dict[str, Any]:
462
471
if hasattr (report .longrepr , "reprtraceback" ) and hasattr (
463
472
report .longrepr , "reprcrash"
464
473
):
465
- d ["longrepr" ] = serialize_longrepr (report )
474
+ d ["longrepr" ] = serialize_exception_longrepr (report )
466
475
else :
467
476
d ["longrepr" ] = str (report .longrepr )
468
477
else :
0 commit comments