Add an import, handling conflicts with existing imports. This method is called after successful code execution, so we know the import is valid.
(
self, module_name: str, original_name: str, resolved_name: str
)
| 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): |