| 474 | |
| 475 | |
| 476 | class ImportFromTracker: |
| 477 | def __init__(self, imports_froms: dict, symbol_map: dict): |
| 478 | self.imports_froms = imports_froms |
| 479 | # symbol_map maps original_name -> list of resolved_names |
| 480 | self.symbol_map = {} |
| 481 | if symbol_map: |
| 482 | for module_name, mappings in symbol_map.items(): |
| 483 | self.symbol_map[module_name] = {} |
| 484 | for original_name, resolved_names in mappings.items(): |
| 485 | if isinstance(resolved_names, list): |
| 486 | self.symbol_map[module_name][original_name] = resolved_names[:] |
| 487 | else: |
| 488 | self.symbol_map[module_name][original_name] = [resolved_names] |
| 489 | else: |
| 490 | self.symbol_map = symbol_map or {} |
| 491 | |
| 492 | def add_import( |
| 493 | self, module_name: str, original_name: str, resolved_name: str |
| 494 | ) -> None: |
| 495 | """Add an import, handling conflicts with existing imports. |
| 496 | |
| 497 | This method is called after successful code execution, so we know the import is valid. |
| 498 | """ |
| 499 | if module_name not in self.imports_froms: |
| 500 | self.imports_froms[module_name] = [] |
| 501 | if module_name not in self.symbol_map: |
| 502 | self.symbol_map[module_name] = {} |
| 503 | |
| 504 | # Check if there's already a different mapping for the same resolved_name from a different original_name |
| 505 | # We need to remove any conflicting mappings |
| 506 | for orig_name, res_names in list(self.symbol_map[module_name].items()): |
| 507 | if resolved_name in res_names and orig_name != original_name: |
| 508 | # Remove the conflicting resolved_name from the other original_name's list |
| 509 | res_names.remove(resolved_name) |
| 510 | if ( |
| 511 | not res_names |
| 512 | ): # If the list is now empty, remove the original_name entirely |
| 513 | if orig_name in self.imports_froms[module_name]: |
| 514 | self.imports_froms[module_name].remove(orig_name) |
| 515 | del self.symbol_map[module_name][orig_name] |
| 516 | |
| 517 | # Add the new mapping |
| 518 | if original_name not in self.imports_froms[module_name]: |
| 519 | self.imports_froms[module_name].append(original_name) |
| 520 | |
| 521 | if original_name not in self.symbol_map[module_name]: |
| 522 | self.symbol_map[module_name][original_name] = [] |
| 523 | |
| 524 | # Add the resolved_name if it's not already in the list |
| 525 | if resolved_name not in self.symbol_map[module_name][original_name]: |
| 526 | self.symbol_map[module_name][original_name].append(resolved_name) |
| 527 | |
| 528 | |
| 529 | def append_obj(module, d, name, obj, autoload=False): |
no outgoing calls
searching dependent graphs…