11import sys
2- import xml .etree .ElementTree as ET
32from test .support import TestStats
3+ from typing import TYPE_CHECKING
44
55from .runtests import RunTests
66from .result import State , TestResult
99 printlist , count , format_duration )
1010
1111
12+ if TYPE_CHECKING :
13+ # Needed to annotate `TestResults.testsuite_xml` accurately
14+ # Delay the runtime import until later,
15+ # so that we only import it if we actually have to
16+ import xml .etree .ElementTree as ET
17+
18+
1219EXITCODE_BAD_TEST = 2
1320EXITCODE_ENV_CHANGED = 3
1421EXITCODE_NO_TESTS_RAN = 4
@@ -32,7 +39,7 @@ def __init__(self):
3239 self .test_times : list [tuple [float , TestName ]] = []
3340 self .stats = TestStats ()
3441 # used by --junit-xml
35- self .testsuite_xml : list [ET .Element ] = []
42+ self .testsuite_xml : list [" ET.Element" ] = []
3643
3744 def get_executed (self ):
3845 return (set (self .good ) | set (self .bad ) | set (self .skipped )
@@ -132,6 +139,9 @@ def prepare_rerun(self) -> tuple[TestTuple, FilterDict]:
132139 return (tuple (tests ), match_tests_dict )
133140
134141 def add_junit (self , xml_data : list [str ]):
142+ # Local import, so that we only import this if we actually need the xml module.
143+ # It's best to import as few things as possible when running a test.
144+ import xml .etree .ElementTree as ET
135145 for e in xml_data :
136146 try :
137147 self .testsuite_xml .append (ET .fromstring (e ))
0 commit comments