Exemplo n.º 1
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 == "url":
                 AddImportsVisitor.add_needed_import(
                     self.context,
                     "django.urls",
                     "re_path",
                 )
             else:
                 if import_alias.evaluated_name == "re_path":
                     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)
         if not new_names:
             return RemoveFromParent()
         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)
Exemplo n.º 2
0
 def leave_ImportFrom(
     self, original_node: ImportFrom, updated_node: ImportFrom
 ) -> Union[BaseSmallStatement, RemovalSentinel]:
     if self._test_import_from(updated_node):
         new_names = []
         for import_alias in updated_node.names:
             if import_alias.evaluated_name == self.old_name:
                 as_name = (
                     import_alias.asname.name.value if import_alias.asname else None
                 )
                 AddImportsVisitor.add_needed_import(
                     context=self.context,
                     module=".".join(self.new_module_parts),
                     obj=self.new_name,
                     asname=as_name,
                 )
             else:
                 new_names.append(import_alias)
         if not new_names:
             return RemoveFromParent()
         # sort imports
         new_names = sorted(new_names, key=lambda n: n.evaluated_name)
         # remove any trailing commas
         last_name = new_names[-1]
         if last_name.comma != MaybeSentinel.DEFAULT:
             new_names[-1] = last_name.with_changes(comma=MaybeSentinel.DEFAULT)
         return updated_node.with_changes(names=new_names)
     return super().leave_ImportFrom(original_node, updated_node)
Exemplo n.º 3
0
def convert_import_from(config: ParserConfig, children: Sequence[Any]) -> Any:
    fromtoken, import_relative, importtoken, *importlist = children

    if len(importlist) == 1:
        (possible_star, ) = importlist
        if isinstance(possible_star, Token):
            # Its a "*" import, so we must construct this node.
            names = ImportStar()
        else:
            # Its an import as names partial, grab the names from that.
            names = possible_star.names
        lpar = None
        rpar = None
    else:
        # Its an import as names partial with parens
        lpartoken, namespartial, rpartoken = importlist
        lpar = LeftParen(whitespace_after=parse_parenthesizable_whitespace(
            config, lpartoken.whitespace_after))
        names = namespartial.names
        rpar = RightParen(whitespace_before=parse_parenthesizable_whitespace(
            config, rpartoken.whitespace_before))

    # If we have a relative-only import, then we need to relocate the space
    # after the final dot to be owned by the import token.
    if len(import_relative.relative) > 0 and import_relative.module is None:
        whitespace_before_import = import_relative.relative[
            -1].whitespace_after
        relative = (
            *import_relative.relative[:-1],
            import_relative.relative[-1].with_changes(
                whitespace_after=SimpleWhitespace("")),
        )
    else:
        whitespace_before_import = parse_simple_whitespace(
            config, importtoken.whitespace_before)
        relative = import_relative.relative

    return WithLeadingWhitespace(
        ImportFrom(
            whitespace_after_from=parse_simple_whitespace(
                config, fromtoken.whitespace_after),
            relative=relative,
            module=import_relative.module,
            whitespace_before_import=whitespace_before_import,
            whitespace_after_import=parse_simple_whitespace(
                config, importtoken.whitespace_after),
            lpar=lpar,
            names=names,
            rpar=rpar,
        ),
        fromtoken.whitespace_before,
    )
Exemplo n.º 4
0
 def leave_ImportFrom(
     self, original_node: ImportFrom, updated_node: ImportFrom
 ) -> Union[BaseSmallStatement, RemovalSentinel]:
     """Update import statements for matching old module name."""
     if not import_from_matches(updated_node, self.old_module_parts):
         return super().leave_ImportFrom(original_node, updated_node)
     # This is a match
     new_names = list(self.gen_new_imported_names(updated_node.names))
     if not new_names:
         # Nothing left in the import statement: remove it
         return RemoveFromParent()
     # Some imports are left, update the statement
     cleaned_names = self.tidy_new_imported_names(new_names)
     return updated_node.with_changes(names=cleaned_names)
Exemplo n.º 5
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)