MCPcopy
hub / github.com/python-trio/trio / fixup_module_metadata

Function fixup_module_metadata

src/trio/_util.py:199–228  ·  view source on GitHub ↗
(
    module_name: str,
    namespace: collections.abc.Mapping[str, object],
)

Source from the content-addressed store, hash-verified

197
198
199def fixup_module_metadata(
200 module_name: str,
201 namespace: collections.abc.Mapping[str, object],
202) -> None:
203 seen_ids: set[int] = set()
204
205 def fix_one(qualname: str, name: str, obj: object) -> None:
206 # avoid infinite recursion (relevant when using
207 # typing.Generic, for example)
208 if id(obj) in seen_ids:
209 return
210 seen_ids.add(id(obj))
211
212 mod = getattr(obj, "__module__", None)
213 if mod is not None and mod.startswith("trio."):
214 obj.__module__ = module_name
215 # Modules, unlike everything else in Python, put fully-qualified
216 # names into their __name__ attribute. We check for "." to avoid
217 # rewriting these.
218 if hasattr(obj, "__name__") and "." not in obj.__name__:
219 obj.__name__ = name
220 if hasattr(obj, "__qualname__"):
221 obj.__qualname__ = qualname
222 if isinstance(obj, type):
223 for attr_name, attr_value in obj.__dict__.items():
224 fix_one(objname + "." + attr_name, attr_name, attr_value)
225
226 for objname, obj in namespace.items():
227 if not objname.startswith("_"): # ignore private attributes
228 fix_one(objname, objname, obj)
229
230
231def _init_final_cls(cls: type[object]) -> NoReturn:

Callers 3

__init__.pyFile · 0.85
__init__.pyFile · 0.85

Calls 1

fix_oneFunction · 0.85

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…