| 1 |
from math import nan, inf |
| 2 |
from contextlib import contextmanager |
| 3 |
from importlib import import_module |
| 4 |
from typing import Any, Dict, FrozenSet, List, Set, Tuple |
| 5 |
|
| 6 |
from pytest import mark |
| 7 |
|
| 8 |
from graphql.pyutils import inspect, Undefined |
| 9 |
from graphql.type import ( |
| 10 |
GraphQLDirective, |
| 11 |
GraphQLField, |
| 12 |
GraphQLInt, |
| 13 |
GraphQLList, |
| 14 |
GraphQLObjectType, |
| 15 |
GraphQLNonNull, |
| 16 |
GraphQLString, |
| 17 |
) |
| 18 |
|
| 19 |
inspect_module = import_module(inspect.__module__) |
| 20 |
|
| 21 |
|
| 22 |
@contextmanager |
| 23 |
def increased_recursive_depth(): |
| 24 |
inspect_module.max_recursive_depth += 1 # type: ignore |
| 25 |
try: |
| 26 |
yield inspect |
| 27 |
finally: |
| 28 |
inspect_module.max_recursive_depth -= 1 # type: ignore |
| 29 |
|
| 30 |
|
| 31 |
@contextmanager |
| 32 |
def increased_str_size(): |
| 33 |
inspect_module.max_str_size *= 2 # type: ignore |
| 34 |
try: |
| 35 |
yield inspect |
| 36 |
finally: |
| 37 |
inspect_module.max_str_size //= 2 # type: ignore |
| 38 |
|
| 39 |
|
| 40 |
@contextmanager |
| 41 |
def increased_list_size(): |
| 42 |
inspect_module.max_list_size *= 2 # type: ignore |
| 43 |
try: |
| 44 |
yield inspect |
| 45 |
finally: |
| 46 |
inspect_module.max_list_size //= 2 # type: ignore |
| 47 |
|
| 48 |
|
| 49 |
def describe_inspect(): |
| 50 |
def inspect_invalid(): |
| 51 |
assert inspect(Undefined) == "Undefined" |
| 52 |
|
| 53 |
def inspect_none(): |
| 54 |
assert inspect(None) == "None" |
| 55 |
|
| 56 |
def inspect_boolean(): |
| 57 |
assert inspect(True) == "True" |
| 58 |
assert inspect(False) == "False" |
| 59 |
|
| 60 |
def inspect_string(): |
| 61 |
for s in "", "abc", "foo\tbar \u265e\0", "'", "'": |
| 62 |
assert inspect(s) == repr(s) |
| 63 |
|
| 64 |
def overly_large_string(): |
| 65 |
s = "foo" * 100 |
| 66 |
r = repr(s) |
| 67 |
assert inspect(s) == r[:118] + "..." + r[-119:] |
| 68 |
with increased_str_size(): |
| 69 |
assert inspect(s) == r |
| 70 |
|
| 71 |
def inspect_bytes(): |
| 72 |
for b in b"", b"abc", b"foo\tbar \x7f\xff\0", b"'", b"'": |
| 73 |
assert inspect(b) == repr(b) |
| 74 |
a = bytearray(b) |
| 75 |
assert inspect(a) == repr(a) |
| 76 |
|
| 77 |
def overly_many_bytes(): |
| 78 |
b = b"foo" * 100 |
| 79 |
r = repr(b) |
| 80 |
assert inspect(b) == r[:118] + "..." + r[-119:] |
| 81 |
a = bytearray(b) |
| 82 |
r = repr(a) |
| 83 |
assert inspect(a) == r[:118] + "..." + r[-119:] |
| 84 |
|
| 85 |
def inspect_numbers(): |
| 86 |
assert inspect(0) == "0" |
| 87 |
assert inspect(0.0) == "0.0" |
| 88 |
assert inspect(314) == "314" |
| 89 |
assert inspect(3.14) == "3.14" |
| 90 |
assert inspect(complex(1, 2)) == "(1+2j)" |
| 91 |
assert inspect(nan) == "nan" |
| 92 |
assert inspect(inf) == "inf" |
| 93 |
assert inspect(-inf) == "-inf" |
| 94 |
|
| 95 |
def overly_large_int(): |
| 96 |
n = int("123" * 100) |
| 97 |
r = repr(n) |
| 98 |
assert inspect(n) == r[:118] + "..." + r[-119:] |
| 99 |
with increased_str_size(): |
| 100 |
assert inspect(n) == r |
| 101 |
|
| 102 |
def inspect_function(): |
| 103 |
assert inspect(lambda: 0) == "<function>" # pragma: no cover |
| 104 |
|
| 105 |
def test_func(): |
| 106 |
pass |
| 107 |
|
| 108 |
assert inspect(test_func) == "<function test_func>" |
| 109 |
|
| 110 |
def inspect_exception(): |
| 111 |
assert inspect(ValueError) == "<exception class ValueError>" |
| 112 |
assert inspect(ArithmeticError(42)) == "<exception ArithmeticError>" |
| 113 |
|
| 114 |
def inspect_class_and_method(): |
| 115 |
class TestClass: |
| 116 |
def test_method(self): |
| 117 |
pass |
| 118 |
|
| 119 |
assert inspect(TestClass) == "<class TestClass>" |
| 120 |
assert inspect(TestClass()) == "<TestClass instance>" |
| 121 |
assert inspect(TestClass.test_method) == "<function test_method>" |
| 122 |
assert inspect(TestClass().test_method) == "<method test_method>" |
| 123 |
|
| 124 |
def inspect_unknown_object(): |
| 125 |
class MetaClass(type): |
| 126 |
__name__ = "" |
| 127 |
|
| 128 |
class TestClass(metaclass=MetaClass): |
| 129 |
pass |
| 130 |
|
| 131 |
assert inspect(TestClass()) == "<object>" |
| 132 |
|
| 133 |
def inspect_generator(): |
| 134 |
def test_generator(): |
| 135 |
yield None # pragma: no cover |
| 136 |
|
| 137 |
assert inspect(test_generator) == "<generator function test_generator>" |
| 138 |
assert inspect(test_generator()) == "<generator test_generator>" |
| 139 |
|
| 140 |
@mark.asyncio |
| 141 |
async def inspect_coroutine(): |
| 142 |
async def test_coroutine(): |
| 143 |
pass |
| 144 |
|
| 145 |
assert inspect(test_coroutine) == "<coroutine function test_coroutine>" |
| 146 |
coroutine_object = test_coroutine() |
| 147 |
assert inspect(coroutine_object) == "<coroutine test_coroutine>" |
| 148 |
await coroutine_object # avoid warning |
| 149 |
|
| 150 |
def inspect_async_generator(): |
| 151 |
async def test_async_generator(): |
| 152 |
yield None # pragma: no cover |
| 153 |
|
| 154 |
assert inspect(test_async_generator) == ( |
| 155 |
"<async generator function test_async_generator>" |
| 156 |
) |
| 157 |
assert inspect(test_async_generator()) == ( |
| 158 |
"<async generator test_async_generator>" |
| 159 |
) |
| 160 |
|
| 161 |
def inspect_lists(): |
| 162 |
assert inspect([]) == "[]" |
| 163 |
assert inspect([None]) == "[None]" |
| 164 |
assert inspect([[None]]) == "[[None]]" |
| 165 |
assert inspect([1, nan]) == "[1, nan]" |
| 166 |
assert inspect([["a", "b"], "c"]) == "[['a', 'b'], 'c']" |
| 167 |
|
| 168 |
def inspect_overly_large_list(): |
| 169 |
s: List[int] = list(range(20)) |
| 170 |
assert inspect(s) == "[0, 1, 2, 3, 4, ..., 16, 17, 18, 19]" |
| 171 |
with increased_list_size(): |
| 172 |
assert inspect(s) == repr(s) |
| 173 |
|
| 174 |
def inspect_overly_nested_list(): |
| 175 |
s: List[List[List]] = [[[]]] |
| 176 |
assert inspect(s) == "[[[]]]" |
| 177 |
s = [[[1, 2, 3]]] |
| 178 |
assert inspect(s) == "[[[...]]]" |
| 179 |
with increased_recursive_depth(): |
| 180 |
assert inspect(s) == repr(s) |
| 181 |
|
| 182 |
def inspect_recursive_list(): |
| 183 |
s: List[Any] = [1, 2, 3] |
| 184 |
s[1] = s |
| 185 |
assert inspect(s) == "[1, [...], 3]" |
| 186 |
|
| 187 |
def inspect_tuples(): |
| 188 |
assert inspect(()) == "()" |
| 189 |
assert inspect((None,)) == "(None,)" |
| 190 |
assert inspect(((None,),)) == "((None,),)" |
| 191 |
assert inspect((1, nan)) == "(1, nan)" |
| 192 |
assert inspect((("a", "b"), "c")) == "(('a', 'b'), 'c')" |
| 193 |
|
| 194 |
def inspect_overly_large_tuple(): |
| 195 |
s = tuple(range(20)) |
| 196 |
assert inspect(s) == "(0, 1, 2, 3, 4, ..., 16, 17, 18, 19)" |
| 197 |
with increased_list_size(): |
| 198 |
assert inspect(s) == repr(s) |
| 199 |
|
| 200 |
def inspect_overly_nested_tuple(): |
| 201 |
s: Tuple[Tuple[Tuple]] = (((),),) |
| 202 |
assert inspect(s) == "(((),),)" |
| 203 |
s = (((1, 2, 3),),) |
| 204 |
assert inspect(s) == "(((...),),)" |
| 205 |
with increased_recursive_depth(): |
| 206 |
assert inspect(s) == repr(s) |
| 207 |
|
| 208 |
def inspect_recursive_tuple(): |
| 209 |
s: List[Any] = [1, 2, 3] |
| 210 |
s[1] = s |
| 211 |
t = tuple(s) |
| 212 |
assert inspect(t) == "(1, [1, [...], 3], 3)" |
| 213 |
|
| 214 |
def mixed_lists_and_tuples(): |
| 215 |
assert inspect(["a", ("b",)]) == "['a', ('b',)]" |
| 216 |
|
| 217 |
def mixed_lists_and_tuples_with_various_objects(): |
| 218 |
class TestClass: |
| 219 |
pass |
| 220 |
|
| 221 |
assert inspect([TestClass, (TestClass,), ValueError()]) == ( |
| 222 |
"[<class TestClass>, (<class TestClass>,), <exception ValueError>]" |
| 223 |
) |
| 224 |
|
| 225 |
def inspect_dicts(): |
| 226 |
assert inspect({}) == "{}" |
| 227 |
assert inspect({"a": 1}) == "{'a': 1}" |
| 228 |
assert inspect({"a": 1, "b": 2}) == "{'a': 1, 'b': 2}" |
| 229 |
assert inspect({"list": [None, 0]}) == "{'list': [None, 0]}" |
| 230 |
assert inspect({"a": True, "b": None}) == "{'a': True, 'b': None}" |
| 231 |
|
| 232 |
def inspect_overly_large_dict(): |
| 233 |
s = dict(zip((chr(97 + i) for i in range(20)), range(20))) |
| 234 |
assert ( |
| 235 |
inspect(s) == "{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4," |
| 236 |
" ..., 'q': 16, 'r': 17, 's': 18, 't': 19}" |
| 237 |
) |
| 238 |
with increased_list_size(): |
| 239 |
assert inspect(s) == repr(s) |
| 240 |
|
| 241 |
def inspect_overly_nested_dict(): |
| 242 |
s: Dict[str, Dict[str, Dict]] = {"a": {"b": {}}} |
| 243 |
assert inspect(s) == "{'a': {'b': {}}}" |
| 244 |
s = {"a": {"b": {"c": 3}}} |
| 245 |
assert inspect(s) == "{'a': {'b': {...}}}" |
| 246 |
with increased_recursive_depth(): |
| 247 |
assert inspect(s) == repr(s) |
| 248 |
|
| 249 |
def inspect_recursive_dict(): |
| 250 |
s: Dict[int, Any] = {} |
| 251 |
s[1] = s |
| 252 |
assert inspect(s) == "{1: {...}}" |
| 253 |
|
| 254 |
def inspect_sets(): |
| 255 |
assert inspect(set()) == "set()" |
| 256 |
assert inspect({"a"}) == "{'a'}" |
| 257 |
assert inspect({"a", 1}) in ("{'a', 1}", "{1, 'a'}") # sets are unordered |
| 258 |
|
| 259 |
def inspect_overly_large_set(): |
| 260 |
s = set(range(20)) |
| 261 |
r = inspect(s) |
| 262 |
assert r.startswith("{") and r.endswith("}") |
| 263 |
assert "..., " in r and "5" not in s # sets are unordered |
| 264 |
assert len(r) == 36 |
| 265 |
with increased_list_size(): |
| 266 |
assert inspect(s) == repr(s) |
| 267 |
|
| 268 |
def inspect_overly_nested_set(): |
| 269 |
s: List[List[Set]] = [[set()]] |
| 270 |
assert inspect(s) == "[[set()]]" |
| 271 |
s = [[{1, 2, 3}]] |
| 272 |
assert inspect(s) == "[[set(...)]]" |
| 273 |
with increased_recursive_depth(): |
| 274 |
assert inspect(s) == repr(s) |
| 275 |
|
| 276 |
def inspect_frozensets(): |
| 277 |
assert inspect(frozenset()) == "frozenset()" |
| 278 |
assert inspect(frozenset(["a"])) == "frozenset({'a'})" |
| 279 |
assert inspect(frozenset(["a", 1])) in ( |
| 280 |
"frozenset({'a', 1})", |
| 281 |
"frozenset({1, 'a'})", |
| 282 |
) # frozensets are unordered |
| 283 |
|
| 284 |
def inspect_overly_large_frozenset(): |
| 285 |
s = frozenset(range(20)) |
| 286 |
r = inspect(s) |
| 287 |
assert r.startswith("frozenset({") and r.endswith("})") |
| 288 |
assert "..., " in r and "5" not in s # frozensets are unordered |
| 289 |
assert len(r) == 47 |
| 290 |
with increased_list_size(): |
| 291 |
assert inspect(s) == repr(s) |
| 292 |
|
| 293 |
def inspect_overly_nested_frozenset(): |
| 294 |
s: FrozenSet[FrozenSet[FrozenSet]] = frozenset([frozenset([frozenset()])]) |
| 295 |
assert inspect(s) == "frozenset({frozenset({frozenset()})})" |
| 296 |
s = frozenset([frozenset([frozenset([1, 2, 3])])]) |
| 297 |
assert inspect(s) == "frozenset({frozenset({frozenset(...)})})" |
| 298 |
with increased_recursive_depth(): |
| 299 |
assert inspect(s) == repr(s) |
| 300 |
|
| 301 |
def mixed_recursive_dict_and_list(): |
| 302 |
s: Any = {1: []} |
| 303 |
s[1].append(s) |
| 304 |
assert inspect(s) == "{1: [{...}]}" |
| 305 |
s = [1, 2, 3] |
| 306 |
s[1] = {2: s} |
| 307 |
assert inspect(s) == "[1, {2: [...]}, 3]" |
| 308 |
|
| 309 |
def mixed_dicts_and_sets(): |
| 310 |
assert inspect({"a": {"b"}}) == "{'a': {'b'}}" |
| 311 |
assert inspect({1: [], 2: (), 3: set()}) == "{1: [], 2: (), 3: set()}" |
| 312 |
assert inspect([(set(),), {None: {()}}]) == "[(set(),), {None: set(...)}]" |
| 313 |
|
| 314 |
def mixed_dicts_and_sets_with_various_objects(): |
| 315 |
class TestClass: |
| 316 |
pass |
| 317 |
|
| 318 |
assert inspect({TestClass: {ValueError()}, ValueError: {TestClass()}}) == ( |
| 319 |
"{<class TestClass>: {<exception ValueError>}," |
| 320 |
" <exception class ValueError>: {<TestClass instance>}}" |
| 321 |
) |
| 322 |
|
| 323 |
def inspect_graphql_types(): |
| 324 |
assert inspect(GraphQLInt) == "Int" |
| 325 |
assert inspect(GraphQLString) == "String" |
| 326 |
assert inspect(GraphQLNonNull(GraphQLString)) == "String!" |
| 327 |
assert inspect(GraphQLList(GraphQLString)) == "[String]" |
| 328 |
test_object_type = GraphQLObjectType( |
| 329 |
"TestObjectType", {"test": GraphQLField(GraphQLString)} |
| 330 |
) |
| 331 |
assert inspect(test_object_type) == "TestObjectType" |
| 332 |
test_directive = GraphQLDirective("TestDirective", []) |
| 333 |
assert inspect(test_directive) == "@TestDirective" |
| 334 |
|
| 335 |
def custom_inspect(): |
| 336 |
class TestClass: |
| 337 |
@staticmethod |
| 338 |
def __inspect__(): |
| 339 |
return "<custom magic method inspect>" |
| 340 |
|
| 341 |
assert inspect(TestClass()) == "<custom magic method inspect>" |
| 342 |
|
| 343 |
def custom_inspect_that_uses_self(): |
| 344 |
class TestClass: |
| 345 |
str = "Hello World!" |
| 346 |
|
| 347 |
def __inspect__(self): |
| 348 |
return self.str |
| 349 |
|
| 350 |
assert inspect(TestClass()) == "Hello World!" |
| 351 |
|
| 352 |
def custom_inspect_that_returns_a_list(): |
| 353 |
class TestClass: |
| 354 |
@staticmethod |
| 355 |
def __inspect__(): |
| 356 |
return [1, 2, 3] |
| 357 |
|
| 358 |
assert inspect(TestClass()) == "[1, 2, 3]" |
| 359 |
|
| 360 |
def custom_inspect_that_returns_an_overly_large_string(): |
| 361 |
s = "foo" * 100 |
| 362 |
|
| 363 |
class TestClass: |
| 364 |
@staticmethod |
| 365 |
def __inspect__(): |
| 366 |
return s |
| 367 |
|
| 368 |
value = TestClass() |
| 369 |
|
| 370 |
assert inspect(value) == s[:118] + "..." + s[-119:] |
| 371 |
with increased_str_size(): |
| 372 |
assert inspect(value) == s |
| 373 |
|
| 374 |
def custom_inspect_that_is_recursive(): |
| 375 |
class TestClass: |
| 376 |
def __inspect__(self): |
| 377 |
return self |
| 378 |
|
| 379 |
assert inspect(TestClass()) == "<TestClass instance>" |