|
19 | 19 | from static_analyzer.graph_definitions import GraphIndex, potential_calls, implemented_by |
20 | 20 | from static_analyzer.incremental_orchestrator import ( |
21 | 21 | _declarations, |
| 22 | + _filter_to_live_files, |
22 | 23 | _restore_inbound_edges_via_definitions, |
23 | 24 | update_cfg_for_changed_files, |
24 | 25 | ) |
@@ -191,6 +192,63 @@ def test_diagnostics_merge_with_new_winning(self) -> None: |
191 | 192 |
|
192 | 193 | self.assertEqual(merged.diagnostics, {"a.py": ["old-a"], "b.py": ["new-b"]}) |
193 | 194 |
|
| 195 | + def test_changed_superclass_keeps_unchanged_subclasses(self) -> None: |
| 196 | + # ``Child extends Base`` lives in the unchanged child.py; only ``Base`` is re-LSPed, so |
| 197 | + # its fresh entry carries no subclasses. The merge must not drop the cached reverse link. |
| 198 | + cached = _result( |
| 199 | + CallGraph(language="python"), |
| 200 | + source_files=["base.py", "child.py"], |
| 201 | + class_hierarchies={ |
| 202 | + "pkg.Base": {"file_path": "base.py", "superclasses": [], "subclasses": ["pkg.Child"]}, |
| 203 | + "pkg.Child": {"file_path": "child.py", "superclasses": ["pkg.Base"], "subclasses": []}, |
| 204 | + }, |
| 205 | + ) |
| 206 | + new = _result( |
| 207 | + CallGraph(language="python"), |
| 208 | + source_files=["base.py"], |
| 209 | + class_hierarchies={ |
| 210 | + "pkg.Base": {"file_path": "base.py", "superclasses": [], "subclasses": []}, |
| 211 | + }, |
| 212 | + ) |
| 213 | + |
| 214 | + merged = merge_results(_analysis_data(cached), new) |
| 215 | + |
| 216 | + self.assertEqual(merged.class_hierarchies["pkg.Base"]["subclasses"], ["pkg.Child"]) |
| 217 | + self.assertEqual(merged.class_hierarchies["pkg.Child"]["superclasses"], ["pkg.Base"]) |
| 218 | + |
| 219 | + def test_subclasses_derived_from_superclasses_ignore_stale_links(self) -> None: |
| 220 | + # A stale reverse link to a class that no longer declares this base must not survive: |
| 221 | + # ``subclasses`` is rebuilt from the superclasses the merged hierarchy actually holds. |
| 222 | + # The fresh half carries no hierarchy, so nothing overwrites the cached entry — only the |
| 223 | + # rebuild can drop the stale link. |
| 224 | + cached = _result( |
| 225 | + CallGraph(language="python"), |
| 226 | + source_files=["base.py"], |
| 227 | + class_hierarchies={ |
| 228 | + "pkg.Base": {"file_path": "base.py", "superclasses": [], "subclasses": ["pkg.Stale"]}, |
| 229 | + }, |
| 230 | + ) |
| 231 | + new = _result(CallGraph(language="python"), source_files=["other.py"]) |
| 232 | + |
| 233 | + merged = merge_results(_analysis_data(cached), new) |
| 234 | + |
| 235 | + self.assertEqual(merged.class_hierarchies["pkg.Base"]["subclasses"], []) |
| 236 | + |
| 237 | + def test_a_base_named_twice_lists_its_child_once(self) -> None: |
| 238 | + cached = _result( |
| 239 | + CallGraph(language="python"), |
| 240 | + source_files=["base.py", "child.py"], |
| 241 | + class_hierarchies={ |
| 242 | + "pkg.Base": {"superclasses": [], "subclasses": []}, |
| 243 | + "pkg.Child": {"superclasses": ["pkg.Base", "pkg.Base"], "subclasses": []}, |
| 244 | + }, |
| 245 | + ) |
| 246 | + new = _result(CallGraph(language="python"), source_files=["other.py"]) |
| 247 | + |
| 248 | + merged = merge_results(_analysis_data(cached), new) |
| 249 | + |
| 250 | + self.assertEqual(merged.class_hierarchies["pkg.Base"]["subclasses"], ["pkg.Child"]) |
| 251 | + |
194 | 252 |
|
195 | 253 | class TestWarmStartDeletion(unittest.TestCase): |
196 | 254 | def test_deleted_changed_file_is_removed_from_cached_cfg(self) -> None: |
@@ -231,6 +289,93 @@ def test_deleted_changed_file_is_removed_from_cached_cfg(self) -> None: |
231 | 289 | self.assertIn("b.bar", updated["call_graph"].nodes) |
232 | 290 | self.assertEqual([str(path) for path in updated["source_files"]], [str(live_file)]) |
233 | 291 |
|
| 292 | + def test_a_deleted_subclass_is_dropped_from_its_base_reverse_links(self) -> None: |
| 293 | + # The changed base kept a reverse link to a child whose file is gone; the merged hierarchy |
| 294 | + # derives it before the live-file filter runs, so the filter has to drop it as well. |
| 295 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 296 | + project_path = Path(temp_dir) |
| 297 | + base_file = project_path / "base.py" |
| 298 | + base_file.write_text("class Base:\n pass\n", encoding="utf-8") |
| 299 | + |
| 300 | + cg = CallGraph(language="python") |
| 301 | + cg.add_node( |
| 302 | + Node( |
| 303 | + fully_qualified_name="pkg.Base", |
| 304 | + node_type=NodeType.CLASS, |
| 305 | + file_path=str(base_file), |
| 306 | + line_start=1, |
| 307 | + line_end=2, |
| 308 | + ) |
| 309 | + ) |
| 310 | + cached = _result( |
| 311 | + cg, |
| 312 | + source_files=[str(base_file)], |
| 313 | + class_hierarchies={ |
| 314 | + "pkg.Base": {"superclasses": [], "subclasses": ["pkg.Child"]}, |
| 315 | + }, |
| 316 | + ) |
| 317 | + cached["class_hierarchies"]["pkg.Child"] = {"superclasses": ["pkg.Base"], "subclasses": []} |
| 318 | + cached["source_files"] = [base_file, project_path / "child.py"] |
| 319 | + |
| 320 | + adapter = MagicMock() |
| 321 | + adapter.file_extensions = [".py"] |
| 322 | + adapter.language = "python" |
| 323 | + engine_client = MagicMock() |
| 324 | + engine_client.get_collected_diagnostics.return_value = {} |
| 325 | + ignore_manager = MagicMock() |
| 326 | + ignore_manager.should_ignore.return_value = False |
| 327 | + |
| 328 | + updated = update_cfg_for_changed_files( |
| 329 | + cached, |
| 330 | + {project_path / "child.py"}, |
| 331 | + adapter, |
| 332 | + project_path, |
| 333 | + project_path, |
| 334 | + engine_client, |
| 335 | + ignore_manager, |
| 336 | + ) |
| 337 | + |
| 338 | + self.assertNotIn("pkg.Child", updated["class_hierarchies"]) |
| 339 | + self.assertEqual(updated["class_hierarchies"]["pkg.Base"]["subclasses"], []) |
| 340 | + |
| 341 | + def test_a_class_name_reused_by_a_function_leaves_the_base_reverse_links(self) -> None: |
| 342 | + # ``Child`` edited from ``class Child(Base)`` into ``def Child(...)`` keeps its qname, so a |
| 343 | + # live-node test that ignores kinds would keep the cached entry and restore the stale link. |
| 344 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 345 | + project_path = Path(temp_dir) |
| 346 | + base_file = project_path / "base.py" |
| 347 | + base_file.write_text("class Base:\n pass\n", encoding="utf-8") |
| 348 | + child_file = project_path / "child.py" |
| 349 | + child_file.write_text("def Child():\n pass\n", encoding="utf-8") |
| 350 | + |
| 351 | + cg = CallGraph(language="python") |
| 352 | + cg.add_node( |
| 353 | + Node( |
| 354 | + fully_qualified_name="pkg.Base", |
| 355 | + node_type=NodeType.CLASS, |
| 356 | + file_path=str(base_file), |
| 357 | + line_start=1, |
| 358 | + line_end=2, |
| 359 | + ) |
| 360 | + ) |
| 361 | + # The edited file now declares a function under the class's old qualified name. |
| 362 | + cg.add_node(_node("pkg.Child", str(child_file))) |
| 363 | + merged = AnalysisData.from_dict( |
| 364 | + _result( |
| 365 | + cg, |
| 366 | + source_files=[str(base_file), str(child_file)], |
| 367 | + class_hierarchies={ |
| 368 | + "pkg.Base": {"superclasses": [], "subclasses": ["pkg.Child"]}, |
| 369 | + "pkg.Child": {"superclasses": ["pkg.Base"], "subclasses": []}, |
| 370 | + }, |
| 371 | + ) |
| 372 | + ) |
| 373 | + |
| 374 | + live = _filter_to_live_files(merged) |
| 375 | + |
| 376 | + self.assertNotIn("pkg.Child", live.class_hierarchies) |
| 377 | + self.assertEqual(live.class_hierarchies["pkg.Base"]["subclasses"], []) |
| 378 | + |
234 | 379 |
|
235 | 380 | def _declared(index: GraphIndex, definition: dict) -> Node | None: |
236 | 381 | location = definition_location(definition) |
|
0 commit comments