Beispiel #1
0
def convert_dotted_as_name(config: ParserConfig,
                           children: Sequence[Any]) -> Any:
    if len(children) == 1:
        (dotted_name, ) = children
        return ImportAlias(name=dotted_name, asname=None)
    else:
        dotted_name, astoken, name = children
        return ImportAlias(
            name=dotted_name,
            asname=AsName(
                whitespace_before_as=parse_parenthesizable_whitespace(
                    config, astoken.whitespace_before),
                whitespace_after_as=parse_parenthesizable_whitespace(
                    config, astoken.whitespace_after),
                name=Name(name.string),
            ),
        )
Beispiel #2
0
 def leave_ImportFrom(
     self, original_node: ImportFrom, updated_node: ImportFrom
 ) -> Union[BaseSmallStatement, RemovalSentinel]:
     if self._test_import_from(updated_node):
         new_names = []
         new_import_missing = True
         new_import_alias = None
         for import_alias in original_node.names:
             if import_alias.evaluated_name == self.old_name:
                 new_import_alias = ImportAlias(name=Name(self.new_name))
             else:
                 if import_alias.evaluated_name == self.new_name:
                     new_import_missing = False
                 new_names.append(import_alias)
         if new_import_missing and new_import_alias is not None:
             new_names.append(new_import_alias)
         new_names = list(sorted(new_names, key=lambda n: n.evaluated_name))
         return ImportFrom(module=updated_node.module, names=new_names)
     return super().leave_ImportFrom(original_node, updated_node)