11from io import StringIO
22from pprint import pprint
33from typing import Any
4+ from typing import cast
45from typing import Dict
56from typing import Iterable
67from typing import Iterator
1516
1617from _pytest ._code .code import ExceptionChainRepr
1718from _pytest ._code .code import ExceptionInfo
19+ from _pytest ._code .code import ExceptionRepr
1820from _pytest ._code .code import ReprEntry
1921from _pytest ._code .code import ReprEntryNative
2022from _pytest ._code .code import ReprExceptionInfo
@@ -57,8 +59,9 @@ def getworkerinfoline(node):
5759class BaseReport :
5860 when = None # type: Optional[str]
5961 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]
6265 sections = [] # type: List[Tuple[str, str]]
6366 nodeid = None # type: str
6467
@@ -79,7 +82,8 @@ def toterminal(self, out: TerminalWriter) -> None:
7982 return
8083
8184 if hasattr (longrepr , "toterminal" ):
82- longrepr .toterminal (out )
85+ longrepr_terminal = cast (TerminalRepr , longrepr )
86+ longrepr_terminal .toterminal (out )
8387 else :
8488 try :
8589 s = str (longrepr )
@@ -233,7 +237,9 @@ def __init__(
233237 location : Tuple [str , Optional [int ], str ],
234238 keywords ,
235239 outcome : "Literal['passed', 'failed', 'skipped']" ,
236- longrepr ,
240+ longrepr : Union [
241+ None , ExceptionInfo [BaseException ], Tuple [str , int , str ], str , TerminalRepr
242+ ],
237243 when : "Literal['setup', 'call', 'teardown']" ,
238244 sections : Iterable [Tuple [str , str ]] = (),
239245 duration : float = 0 ,
@@ -293,8 +299,9 @@ def from_item_and_call(cls, item: Item, call: "CallInfo[None]") -> "TestReport":
293299 sections = []
294300 if not call .excinfo :
295301 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]
298305 else :
299306 if not isinstance (excinfo , ExceptionInfo ):
300307 outcome = "failed"
@@ -372,7 +379,7 @@ def __repr__(self) -> str:
372379
373380
374381class CollectErrorRepr (TerminalRepr ):
375- def __init__ (self , msg ) -> None :
382+ def __init__ (self , msg : str ) -> None :
376383 self .longrepr = msg
377384
378385 def toterminal (self , out : TerminalWriter ) -> None :
@@ -436,16 +443,18 @@ def serialize_repr_crash(
436443 else :
437444 return None
438445
439- def serialize_longrepr (rep : BaseReport ) -> Dict [str , Any ]:
446+ def serialize_exception_longrepr (rep : BaseReport ) -> Dict [str , Any ]:
440447 assert rep .longrepr is not None
448+ # TODO: Investigate whether the duck typing is really necessary here.
449+ longrepr = cast (ExceptionRepr , rep .longrepr )
441450 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 ,
445454 } # type: Dict[str, Any]
446- if isinstance (rep . longrepr , ExceptionChainRepr ):
455+ if isinstance (longrepr , ExceptionChainRepr ):
447456 result ["chain" ] = []
448- for repr_traceback , repr_crash , description in rep . longrepr .chain :
457+ for repr_traceback , repr_crash , description in longrepr .chain :
449458 result ["chain" ].append (
450459 (
451460 serialize_repr_traceback (repr_traceback ),
@@ -462,7 +471,7 @@ def serialize_longrepr(rep: BaseReport) -> Dict[str, Any]:
462471 if hasattr (report .longrepr , "reprtraceback" ) and hasattr (
463472 report .longrepr , "reprcrash"
464473 ):
465- d ["longrepr" ] = serialize_longrepr (report )
474+ d ["longrepr" ] = serialize_exception_longrepr (report )
466475 else :
467476 d ["longrepr" ] = str (report .longrepr )
468477 else :
0 commit comments