1
1
from math import nan , inf
2
+ from contextlib import contextmanager
3
+ from importlib import import_module
2
4
3
5
from pytest import mark
4
6
13
15
GraphQLString ,
14
16
)
15
17
18
+ inspect_module = import_module (inspect .__module__ )
19
+
20
+
21
+ @contextmanager
22
+ def increased_recursive_depth ():
23
+ inspect_module .max_recursive_depth += 1
24
+ try :
25
+ yield inspect
26
+ finally :
27
+ inspect_module .max_recursive_depth -= 1
28
+
29
+
30
+ @contextmanager
31
+ def increased_str_size ():
32
+ inspect_module .max_str_size *= 2
33
+ try :
34
+ yield inspect
35
+ finally :
36
+ inspect_module .max_str_size //= 2
37
+
38
+
39
+ @contextmanager
40
+ def increased_list_size ():
41
+ inspect_module .max_list_size *= 2
42
+ try :
43
+ yield inspect
44
+ finally :
45
+ inspect_module .max_list_size //= 2
46
+
16
47
17
48
def describe_inspect ():
18
49
def inspect_invalid ():
@@ -33,6 +64,8 @@ def overly_large_string():
33
64
s = "foo" * 100
34
65
r = repr (s )
35
66
assert inspect (s ) == r [:118 ] + "..." + r [- 119 :]
67
+ with increased_str_size ():
68
+ assert inspect (s ) == r
36
69
37
70
def inspect_bytes ():
38
71
for b in b"" , b"abc" , b"foo\t bar \x7f \xff \0 " , b"'" , b"'" :
@@ -62,6 +95,8 @@ def overly_large_int():
62
95
n = int ("123" * 100 )
63
96
r = repr (n )
64
97
assert inspect (n ) == r [:118 ] + "..." + r [- 119 :]
98
+ with increased_str_size ():
99
+ assert inspect (n ) == r
65
100
66
101
def inspect_function ():
67
102
assert inspect (lambda : 0 ) == "<function>"
@@ -132,18 +167,21 @@ def inspect_lists():
132
167
def inspect_overly_large_list ():
133
168
s = list (range (20 ))
134
169
assert inspect (s ) == "[0, 1, 2, 3, 4, ..., 16, 17, 18, 19]"
170
+ with increased_list_size ():
171
+ assert inspect (s ) == repr (s )
135
172
136
173
def inspect_overly_nested_list ():
137
174
s = [[[]]]
138
175
assert inspect (s ) == "[[[]]]"
139
176
s = [[[1 , 2 , 3 ]]]
140
177
assert inspect (s ) == "[[[...]]]"
141
- assert inspect (s , max_depth = 3 ) == repr (s )
178
+ with increased_recursive_depth ():
179
+ assert inspect (s ) == repr (s )
142
180
143
181
def inspect_recursive_list ():
144
182
s = [1 , 2 , 3 ]
145
183
s [1 ] = s
146
- assert inspect (s ) == "[1, [1, [ ...], 3 ], 3]"
184
+ assert inspect (s ) == "[1, [...], 3]"
147
185
148
186
def inspect_tuples ():
149
187
assert inspect (()) == "()"
@@ -155,13 +193,16 @@ def inspect_tuples():
155
193
def inspect_overly_large_tuple ():
156
194
s = tuple (range (20 ))
157
195
assert inspect (s ) == "(0, 1, 2, 3, 4, ..., 16, 17, 18, 19)"
196
+ with increased_list_size ():
197
+ assert inspect (s ) == repr (s )
158
198
159
199
def inspect_overly_nested_tuple ():
160
200
s = (((),),)
161
201
assert inspect (s ) == "(((),),)"
162
202
s = (((1 , 2 , 3 ),),)
163
203
assert inspect (s ) == "(((...),),)"
164
- assert inspect (s , max_depth = 3 ) == repr (s )
204
+ with increased_recursive_depth ():
205
+ assert inspect (s ) == repr (s )
165
206
166
207
def inspect_recursive_tuple ():
167
208
s = [1 , 2 , 3 ]
@@ -193,18 +234,21 @@ def inspect_overly_large_dict():
193
234
inspect (s ) == "{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4,"
194
235
" ..., 'q': 16, 'r': 17, 's': 18, 't': 19}"
195
236
)
237
+ with increased_list_size ():
238
+ assert inspect (s ) == repr (s )
196
239
197
240
def inspect_overly_nested_dict ():
198
241
s = {"a" : {"b" : {}}}
199
242
assert inspect (s ) == "{'a': {'b': {}}}"
200
243
s = {"a" : {"b" : {"c" : 3 }}}
201
244
assert inspect (s ) == "{'a': {'b': {...}}}"
202
- assert inspect (s , max_depth = 3 ) == repr (s )
245
+ with increased_recursive_depth ():
246
+ assert inspect (s ) == repr (s )
203
247
204
248
def inspect_recursive_dict ():
205
249
s = {}
206
250
s [1 ] = s
207
- assert inspect (s ) == "{1: {1: { ...} }}"
251
+ assert inspect (s ) == "{1: {...}}"
208
252
209
253
def inspect_sets ():
210
254
assert inspect (set ()) == "set()"
@@ -217,13 +261,16 @@ def inspect_overly_large_set():
217
261
assert r .startswith ("{" ) and r .endswith ("}" )
218
262
assert "..., " in r and "5" not in s # sets are unordered
219
263
assert len (r ) == 36
264
+ with increased_list_size ():
265
+ assert inspect (s ) == repr (s )
220
266
221
267
def inspect_overly_nested_set ():
222
268
s = [[set ()]]
223
269
assert inspect (s ) == "[[set()]]"
224
270
s = [[set ([1 , 2 , 3 ])]]
225
271
assert inspect (s ) == "[[set(...)]]"
226
- assert inspect (s , max_depth = 3 ) == repr (s )
272
+ with increased_recursive_depth ():
273
+ assert inspect (s ) == repr (s )
227
274
228
275
def inspect_frozensets ():
229
276
assert inspect (frozenset ()) == "frozenset()"
@@ -239,13 +286,16 @@ def inspect_overly_large_frozenset():
239
286
assert r .startswith ("frozenset({" ) and r .endswith ("})" )
240
287
assert "..., " in r and "5" not in s # frozensets are unordered
241
288
assert len (r ) == 47
289
+ with increased_list_size ():
290
+ assert inspect (s ) == repr (s )
242
291
243
292
def inspect_overly_nested_frozenset ():
244
293
s = frozenset ([frozenset ([frozenset ()])])
245
294
assert inspect (s ) == "frozenset({frozenset({frozenset()})})"
246
295
s = frozenset ([frozenset ([frozenset ([1 , 2 , 3 ])])])
247
296
assert inspect (s ) == "frozenset({frozenset({frozenset(...)})})"
248
- assert inspect (s , max_depth = 3 ) == repr (s )
297
+ with increased_recursive_depth ():
298
+ assert inspect (s ) == repr (s )
249
299
250
300
def mixed_recursive_dict_and_list ():
251
301
s = {1 : []}
@@ -312,7 +362,11 @@ class TestClass:
312
362
def __inspect__ ():
313
363
return s
314
364
315
- assert inspect (TestClass ()) == s [:118 ] + "..." + s [- 119 :]
365
+ value = TestClass ()
366
+
367
+ assert inspect (value ) == s [:118 ] + "..." + s [- 119 :]
368
+ with increased_str_size ():
369
+ assert inspect (value ) == s
316
370
317
371
def custom_inspect_that_is_recursive ():
318
372
class TestClass :
0 commit comments