From a798bd21f7192b35da97d1f73641646e6a83080d Mon Sep 17 00:00:00 2001 From: Jimmy Date: Fri, 15 Apr 2022 14:49:49 +1200 Subject: fix upstream bug 16e1c0679a25529 When the target rename was something like "a.b.c" and the file happened to do a simple "import a" it would fail with `CSTValidationError: Cannot have empty name identifier.` when trying to construct a new import alias. Because the result of gen_replacement_module() in this case was an empty string. Which is apparently used for "no replacement required", which is accurate in this case. This is a bandaid, the problem is more likely to be that the `old_name.startswith(import_alias_full_name+'.')` conditional is too simplistic. --- scripts/codemods/rename_pyqt.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/scripts/codemods/rename_pyqt.py b/scripts/codemods/rename_pyqt.py index 306203c82..ba20f4f54 100644 --- a/scripts/codemods/rename_pyqt.py +++ b/scripts/codemods/rename_pyqt.py @@ -139,33 +139,31 @@ class RenameCommand(VisitorBasedCodemodCommand): if import_alias_full_name is None: raise Exception("Could not parse full name for ImportAlias.name node.") + replacement_module = self.gen_replacement_module(import_alias_full_name) if isinstance(import_alias_name, cst.Name) and self.old_name.startswith( import_alias_full_name + "." - ): + ) and replacement_module: # Might, be in use elsewhere in the code, so schedule a potential removal, and add another alias. new_names.append(import_alias) self.scheduled_removals.add(original_node) - print(f"leave_Import {import_alias_full_name=} -> {self.gen_replacement_module(import_alias_full_name)=} {self.old_name=} {import_alias=} {updated_node=} {import_alias=}") - replacement_name = self.gen_replacement_module(import_alias_full_name) + print(f"leave_Import down {original_node=} {import_alias_full_name=} -> {self.gen_replacement_module(import_alias_full_name)=} {self.old_name=} {import_alias=} {updated_node=} {import_alias=}") new_names.append( cst.ImportAlias( name=cst.Name( - value=replacement_name, + value=replacement_module, ) ) ) self.bypass_import = True elif isinstance( import_alias_name, cst.Attribute - ) and self.old_name.startswith(import_alias_full_name + "."): + ) and self.old_name.startswith(import_alias_full_name + ".") and replacement_module: # Same idea as above. new_names.append(import_alias) self.scheduled_removals.add(original_node) new_name_node: Union[ cst.Attribute, cst.Name - ] = self.gen_name_or_attr_node( - self.gen_replacement_module(import_alias_full_name) - ) + ] = self.gen_name_or_attr_node(replacement_module) new_names.append(cst.ImportAlias(name=new_name_node)) self.bypass_import = True else: -- cgit v1.2.3-54-g00ecf