MCPcopy
hub / github.com/msgspec/msgspec / _build_name_map

Function _build_name_map

src/msgspec/_json_schema.py:186–213  ·  view source on GitHub ↗

A mapping from nameable subcomponents to a generated name. The generated name is usually a normalized version of the class name. In the case of conflicts, the name will be expanded to also include the full import path.

(component_types: dict[Any, mi.Type])

Source from the content-addressed store, hash-verified

184
185
186def _build_name_map(component_types: dict[Any, mi.Type]) -> dict[Any, str]:
187 """A mapping from nameable subcomponents to a generated name.
188
189 The generated name is usually a normalized version of the class name. In
190 the case of conflicts, the name will be expanded to also include the full
191 import path.
192 """
193
194 def normalize(name):
195 return re.sub(r"[^a-zA-Z0-9.\-_]", "_", name)
196
197 def fullname(cls):
198 return normalize(f"{cls.__module__}.{cls.__qualname__}")
199
200 conflicts = set()
201 names: dict[str, Any] = {}
202
203 for cls in component_types:
204 name = normalize(_get_class_name(cls))
205 if name in names:
206 old = names.pop(name)
207 conflicts.add(name)
208 names[fullname(old)] = old
209 if name in conflicts:
210 names[fullname(cls)] = cls
211 else:
212 names[name] = cls
213 return {v: k for k, v in names.items()}
214
215
216class _SchemaGenerator:

Callers 1

schema_componentsFunction · 0.85

Calls 3

normalizeFunction · 0.85
_get_class_nameFunction · 0.85
fullnameFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…