MCPcopy
hub / github.com/msgspec/msgspec / temp_module

Function temp_module

tests/unit/utils.py:10–23  ·  view source on GitHub ↗

Mutually recursive struct types defined inside functions don't work (and probably never will). To avoid populating a bunch of test structs in the top level of this module, we instead create a temporary module per test to exec whatever is needed for that test

(code)

Source from the content-addressed store, hash-verified

8
9@contextmanager
10def temp_module(code):
11 """Mutually recursive struct types defined inside functions don't work (and
12 probably never will). To avoid populating a bunch of test structs in the
13 top level of this module, we instead create a temporary module per test to
14 exec whatever is needed for that test"""
15 code = textwrap.dedent(code)
16 name = f"temp_{uuid.uuid4().hex}"
17 mod = types.ModuleType(name)
18 sys.modules[name] = mod
19 try:
20 exec(code, mod.__dict__)
21 yield mod
22 finally:
23 sys.modules.pop(name, None)
24
25
26@contextmanager

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…