Skip to content

Commit 79dd615

Browse files
committed
Move ExceptionGroup repr tests into new function
1 parent 56eefee commit 79dd615

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

Lib/test/test_exception_group.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,13 @@ class MyBEG(BaseExceptionGroup):
131131

132132
class StrAndReprTests(unittest.TestCase):
133133
def test_ExceptionGroup(self):
134-
eg_excs = [ValueError(1), TypeError(2)]
135134
eg = BaseExceptionGroup(
136-
'flat', eg_excs)
135+
'flat', [ValueError(1), TypeError(2)])
137136

138137
self.assertEqual(str(eg), "flat (2 sub-exceptions)")
139138
self.assertEqual(repr(eg),
140139
"ExceptionGroup('flat', [ValueError(1), TypeError(2)])")
141140

142-
# Mutate the list of exceptions passed to BaseExceptionGroup.
143-
# This shouldn't change the EG's functionality, nor its repr.
144-
eg_excs.clear()
145-
146141
eg = BaseExceptionGroup(
147142
'nested', [eg, ValueError(1), eg, TypeError(2)])
148143

@@ -156,20 +151,15 @@ def test_ExceptionGroup(self):
156151
"[ValueError(1), TypeError(2)]), TypeError(2)])")
157152

158153
def test_BaseExceptionGroup(self):
159-
eg_excs = [ValueError(1), KeyboardInterrupt(2)]
160154
eg = BaseExceptionGroup(
161-
'flat', eg_excs)
155+
'flat', [ValueError(1), KeyboardInterrupt(2)])
162156

163157
self.assertEqual(str(eg), "flat (2 sub-exceptions)")
164158
self.assertEqual(repr(eg),
165159
"BaseExceptionGroup("
166160
"'flat', "
167161
"[ValueError(1), KeyboardInterrupt(2)])")
168162

169-
# Mutate the list of exceptions passed to BaseExceptionGroup.
170-
# This shouldn't change the EG's functionality, nor its repr.
171-
eg_excs.clear()
172-
173163
eg = BaseExceptionGroup(
174164
'nested', [eg, ValueError(1), eg])
175165

@@ -186,17 +176,12 @@ def test_custom_exception(self):
186176
class MyEG(ExceptionGroup):
187177
pass
188178

189-
eg_excs = [ValueError(1), TypeError(2)]
190179
eg = MyEG(
191-
'flat', eg_excs)
180+
'flat', [ValueError(1), TypeError(2)])
192181

193182
self.assertEqual(str(eg), "flat (2 sub-exceptions)")
194183
self.assertEqual(repr(eg), "MyEG('flat', [ValueError(1), TypeError(2)])")
195184

196-
# Mutate the list of exceptions passed to MyEG.
197-
# This shouldn't change the EG's functionality, nor its repr.
198-
eg_excs.clear()
199-
200185
eg = MyEG(
201186
'nested', [eg, ValueError(1), eg, TypeError(2)])
202187

@@ -208,6 +193,28 @@ class MyEG(ExceptionGroup):
208193
"MyEG('flat', [ValueError(1), TypeError(2)]), "
209194
"TypeError(2)])"))
210195

196+
def test_exceptions_mutation(self):
197+
class MyEG(ExceptionGroup):
198+
pass
199+
200+
excs = [ValueError(1), TypeError(2)]
201+
eg = MyEG('test', excs)
202+
203+
self.assertEqual(repr(eg), "MyEG('test', [ValueError(1), TypeError(2)])")
204+
excs.clear()
205+
206+
# Ensure that clearing the exceptions sequence doesn't change the repr.
207+
self.assertEqual(repr(eg), "MyEG('test', [ValueError(1), TypeError(2)])")
208+
209+
# Ensure that the args are still as passed.
210+
self.assertEqual(eg.args, ('test', []))
211+
212+
excs = (ValueError(1), KeyboardInterrupt(2))
213+
eg = BaseExceptionGroup('test', excs)
214+
215+
# Ensure that other, immutable, sequences still work fine.
216+
self.assertEqual(repr(eg), "BaseExceptionGroup('test', (ValueError(1), KeyboardInterrupt(2)))")
217+
211218

212219
def create_simple_eg():
213220
excs = []

0 commit comments

Comments
 (0)