MCPcopy
hub / github.com/python/mypy / assert_blobs_same

Function assert_blobs_same

mypyc/test/test_serialization.py:34–69  ·  view source on GitHub ↗

Compare two blobs of IR as best we can. FuncDecls, FuncIRs, and ClassIRs are compared by fullname to avoid infinite recursion. (More detailed comparisons should be done manually.) Types and signatures are compared using mypyc.sametype. Containers are compared recursively.

(x: Any, y: Any, trail: tuple[Any, ...])

Source from the content-addressed store, hash-verified

32
33
34def assert_blobs_same(x: Any, y: Any, trail: tuple[Any, ...]) -> None:
35 """Compare two blobs of IR as best we can.
36
37 FuncDecls, FuncIRs, and ClassIRs are compared by fullname to avoid
38 infinite recursion.
39 (More detailed comparisons should be done manually.)
40
41 Types and signatures are compared using mypyc.sametype.
42
43 Containers are compared recursively.
44
45 Anything else is compared with ==.
46
47 The `trail` argument is used in error messages.
48 """
49
50 assert type(x) is type(y), (f"Type mismatch at {trail}", type(x), type(y))
51 if isinstance(x, (FuncDecl, FuncIR, ClassIR)):
52 assert x.fullname == y.fullname, f"Name mismatch at {trail}"
53 elif isinstance(x, dict):
54 assert len(x.keys()) == len(y.keys()), f"Keys mismatch at {trail}"
55 for (xk, xv), (yk, yv) in zip(x.items(), y.items()):
56 assert_blobs_same(xk, yk, trail + ("keys",))
57 assert_blobs_same(xv, yv, trail + (xk,))
58 elif isinstance(x, Iterable) and not isinstance(x, (str, set)):
59 # Special case iterables to generate better assert error messages.
60 # We can't use this for sets since the ordering is unpredictable,
61 # and strings should be treated as atomic values.
62 for i, (xv, yv) in enumerate(zip(x, y)):
63 assert_blobs_same(xv, yv, trail + (i,))
64 elif isinstance(x, RType):
65 assert is_same_type(x, y), f"RType mismatch at {trail}"
66 elif isinstance(x, FuncSignature):
67 assert is_same_signature(x, y), f"Signature mismatch at {trail}"
68 else:
69 assert x == y, f"Value mismatch at {trail}"
70
71
72def assert_modules_same(ir1: ModuleIR, ir2: ModuleIR) -> None:

Callers 1

assert_modules_sameFunction · 0.85

Calls 9

is_same_typeFunction · 0.90
is_same_signatureFunction · 0.90
typeClass · 0.85
isinstanceFunction · 0.85
lenFunction · 0.85
zipFunction · 0.85
enumerateFunction · 0.85
keysMethod · 0.80
itemsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…