Return names that are different in two snapshots of a symbol table. Only shallow (intra-module) differences are considered. References to things defined outside the module are compared based on the name of the target only. Recurse into class symbol tables (if the class is defined in th
(
name_prefix: str, snapshot1: dict[str, SymbolSnapshot], snapshot2: dict[str, SymbolSnapshot]
)
| 121 | |
| 122 | |
| 123 | def compare_symbol_table_snapshots( |
| 124 | name_prefix: str, snapshot1: dict[str, SymbolSnapshot], snapshot2: dict[str, SymbolSnapshot] |
| 125 | ) -> set[str]: |
| 126 | """Return names that are different in two snapshots of a symbol table. |
| 127 | |
| 128 | Only shallow (intra-module) differences are considered. References to things defined |
| 129 | outside the module are compared based on the name of the target only. |
| 130 | |
| 131 | Recurse into class symbol tables (if the class is defined in the target module). |
| 132 | |
| 133 | Return a set of fully-qualified names (e.g., 'mod.func' or 'mod.Class.method'). |
| 134 | """ |
| 135 | # Find names only defined only in one version. |
| 136 | names1 = {f"{name_prefix}.{name}" for name in snapshot1} |
| 137 | names2 = {f"{name_prefix}.{name}" for name in snapshot2} |
| 138 | triggers = names1 ^ names2 |
| 139 | |
| 140 | # Look for names defined in both versions that are different. |
| 141 | for name in set(snapshot1.keys()) & set(snapshot2.keys()): |
| 142 | item1 = snapshot1[name] |
| 143 | item2 = snapshot2[name] |
| 144 | kind1 = item1[0] |
| 145 | kind2 = item2[0] |
| 146 | item_name = f"{name_prefix}.{name}" |
| 147 | if kind1 != kind2: |
| 148 | # Different kind of node in two snapshots -> trivially different. |
| 149 | triggers.add(item_name) |
| 150 | elif kind1 == "TypeInfo": |
| 151 | if item1[:-1] != item2[:-1]: |
| 152 | # Record major difference (outside class symbol tables). |
| 153 | triggers.add(item_name) |
| 154 | # Look for differences in nested class symbol table entries. |
| 155 | assert isinstance(item1[-1], dict) |
| 156 | assert isinstance(item2[-1], dict) |
| 157 | triggers |= compare_symbol_table_snapshots(item_name, item1[-1], item2[-1]) |
| 158 | else: |
| 159 | # Shallow node (no interesting internal structure). Just use equality. |
| 160 | if snapshot1[name] != snapshot2[name]: |
| 161 | triggers.add(item_name) |
| 162 | |
| 163 | return triggers |
| 164 | |
| 165 | |
| 166 | def snapshot_symbol_table(name_prefix: str, table: SymbolTable) -> dict[str, SymbolSnapshot]: |
searching dependent graphs…