11from math import nan , inf
2+ from contextlib import contextmanager
3+ from importlib import import_module
24
35from pytest import mark
46
1315 GraphQLString ,
1416)
1517
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+
1647
1748def describe_inspect ():
1849 def inspect_invalid ():
@@ -33,6 +64,8 @@ def overly_large_string():
3364 s = "foo" * 100
3465 r = repr (s )
3566 assert inspect (s ) == r [:118 ] + "..." + r [- 119 :]
67+ with increased_str_size ():
68+ assert inspect (s ) == r
3669
3770 def inspect_bytes ():
3871 for b in b"" , b"abc" , b"foo\t bar \x7f \xff \0 " , b"'" , b"'" :
@@ -62,6 +95,8 @@ def overly_large_int():
6295 n = int ("123" * 100 )
6396 r = repr (n )
6497 assert inspect (n ) == r [:118 ] + "..." + r [- 119 :]
98+ with increased_str_size ():
99+ assert inspect (n ) == r
65100
66101 def inspect_function ():
67102 assert inspect (lambda : 0 ) == "<function>"
@@ -132,18 +167,21 @@ def inspect_lists():
132167 def inspect_overly_large_list ():
133168 s = list (range (20 ))
134169 assert inspect (s ) == "[0, 1, 2, 3, 4, ..., 16, 17, 18, 19]"
170+ with increased_list_size ():
171+ assert inspect (s ) == repr (s )
135172
136173 def inspect_overly_nested_list ():
137174 s = [[[]]]
138175 assert inspect (s ) == "[[[]]]"
139176 s = [[[1 , 2 , 3 ]]]
140177 assert inspect (s ) == "[[[...]]]"
141- assert inspect (s , max_depth = 3 ) == repr (s )
178+ with increased_recursive_depth ():
179+ assert inspect (s ) == repr (s )
142180
143181 def inspect_recursive_list ():
144182 s = [1 , 2 , 3 ]
145183 s [1 ] = s
146- assert inspect (s ) == "[1, [1, [ ...], 3 ], 3]"
184+ assert inspect (s ) == "[1, [...], 3]"
147185
148186 def inspect_tuples ():
149187 assert inspect (()) == "()"
@@ -155,13 +193,16 @@ def inspect_tuples():
155193 def inspect_overly_large_tuple ():
156194 s = tuple (range (20 ))
157195 assert inspect (s ) == "(0, 1, 2, 3, 4, ..., 16, 17, 18, 19)"
196+ with increased_list_size ():
197+ assert inspect (s ) == repr (s )
158198
159199 def inspect_overly_nested_tuple ():
160200 s = (((),),)
161201 assert inspect (s ) == "(((),),)"
162202 s = (((1 , 2 , 3 ),),)
163203 assert inspect (s ) == "(((...),),)"
164- assert inspect (s , max_depth = 3 ) == repr (s )
204+ with increased_recursive_depth ():
205+ assert inspect (s ) == repr (s )
165206
166207 def inspect_recursive_tuple ():
167208 s = [1 , 2 , 3 ]
@@ -193,18 +234,21 @@ def inspect_overly_large_dict():
193234 inspect (s ) == "{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4,"
194235 " ..., 'q': 16, 'r': 17, 's': 18, 't': 19}"
195236 )
237+ with increased_list_size ():
238+ assert inspect (s ) == repr (s )
196239
197240 def inspect_overly_nested_dict ():
198241 s = {"a" : {"b" : {}}}
199242 assert inspect (s ) == "{'a': {'b': {}}}"
200243 s = {"a" : {"b" : {"c" : 3 }}}
201244 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 )
203247
204248 def inspect_recursive_dict ():
205249 s = {}
206250 s [1 ] = s
207- assert inspect (s ) == "{1: {1: { ...} }}"
251+ assert inspect (s ) == "{1: {...}}"
208252
209253 def inspect_sets ():
210254 assert inspect (set ()) == "set()"
@@ -217,13 +261,16 @@ def inspect_overly_large_set():
217261 assert r .startswith ("{" ) and r .endswith ("}" )
218262 assert "..., " in r and "5" not in s # sets are unordered
219263 assert len (r ) == 36
264+ with increased_list_size ():
265+ assert inspect (s ) == repr (s )
220266
221267 def inspect_overly_nested_set ():
222268 s = [[set ()]]
223269 assert inspect (s ) == "[[set()]]"
224270 s = [[set ([1 , 2 , 3 ])]]
225271 assert inspect (s ) == "[[set(...)]]"
226- assert inspect (s , max_depth = 3 ) == repr (s )
272+ with increased_recursive_depth ():
273+ assert inspect (s ) == repr (s )
227274
228275 def inspect_frozensets ():
229276 assert inspect (frozenset ()) == "frozenset()"
@@ -239,13 +286,16 @@ def inspect_overly_large_frozenset():
239286 assert r .startswith ("frozenset({" ) and r .endswith ("})" )
240287 assert "..., " in r and "5" not in s # frozensets are unordered
241288 assert len (r ) == 47
289+ with increased_list_size ():
290+ assert inspect (s ) == repr (s )
242291
243292 def inspect_overly_nested_frozenset ():
244293 s = frozenset ([frozenset ([frozenset ()])])
245294 assert inspect (s ) == "frozenset({frozenset({frozenset()})})"
246295 s = frozenset ([frozenset ([frozenset ([1 , 2 , 3 ])])])
247296 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 )
249299
250300 def mixed_recursive_dict_and_list ():
251301 s = {1 : []}
@@ -312,7 +362,11 @@ class TestClass:
312362 def __inspect__ ():
313363 return s
314364
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
316370
317371 def custom_inspect_that_is_recursive ():
318372 class TestClass :
0 commit comments