Skip to content

Commit 581ef0d

Browse files
committed
results.py: Don't import xml.etree.ElementTree in the global scope
1 parent 98d3000 commit 581ef0d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Lib/test/libregrtest/results.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
2-
import xml.etree.ElementTree as ET
32
from test.support import TestStats
3+
from typing import TYPE_CHECKING
44

55
from .runtests import RunTests
66
from .result import State, TestResult
@@ -9,6 +9,13 @@
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+
1219
EXITCODE_BAD_TEST = 2
1320
EXITCODE_ENV_CHANGED = 3
1421
EXITCODE_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

Comments
 (0)