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])
| 184 | |
| 185 | |
| 186 | def _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 | |
| 216 | class _SchemaGenerator: |
no test coverage detected
searching dependent graphs…