Make sure that elements given in prioritized will not corrupt any index given in grouped.
(
grouped: dict[Hashable, list[MergeElement]],
prioritized: Mapping[Any, MergeElement],
)
| 192 | |
| 193 | |
| 194 | def _assert_prioritized_valid( |
| 195 | grouped: dict[Hashable, list[MergeElement]], |
| 196 | prioritized: Mapping[Any, MergeElement], |
| 197 | ) -> None: |
| 198 | """Make sure that elements given in prioritized will not corrupt any |
| 199 | index given in grouped. |
| 200 | """ |
| 201 | prioritized_names = set(prioritized) |
| 202 | grouped_by_index: dict[int, list[Hashable]] = defaultdict(list) |
| 203 | indexes: dict[int, Index] = {} |
| 204 | |
| 205 | for name, elements_list in grouped.items(): |
| 206 | for _, index in elements_list: |
| 207 | if index is not None: |
| 208 | grouped_by_index[id(index)].append(name) |
| 209 | indexes[id(index)] = index |
| 210 | |
| 211 | # An index may be corrupted when the set of its corresponding coordinate name(s) |
| 212 | # partially overlaps the set of names given in prioritized |
| 213 | for index_id, index_coord_names in grouped_by_index.items(): |
| 214 | index_names = set(index_coord_names) |
| 215 | common_names = index_names & prioritized_names |
| 216 | if common_names and len(common_names) != len(index_names): |
| 217 | common_names_str = ", ".join(f"{k!r}" for k in common_names) |
| 218 | index_names_str = ", ".join(f"{k!r}" for k in index_coord_names) |
| 219 | raise ValueError( |
| 220 | f"cannot set or update variable(s) {common_names_str}, which would corrupt " |
| 221 | f"the following index built from coordinates {index_names_str}:\n" |
| 222 | f"{indexes[index_id]!r}" |
| 223 | ) |
| 224 | |
| 225 | |
| 226 | def merge_collected( |
no test coverage detected
searching dependent graphs…