Ejemplo n.º 1
0
    def additional_byte_cost(self):
        """
        How many additional bytes would be used, if this was renamed
        """

        arg_rename = False
        additional_bytes = 0

        for node in self._references:
            if isinstance(node, ast.Name):
                if isinstance(node.ctx, (ast.Load, ast.Store, ast.Del)):
                    pass
                else:
                    # Python 2 Param context
                    if not arg_rename_in_place(node):
                        arg_rename = True
            elif is_ast_node(
                    node, (ast.ClassDef, ast.FunctionDef, 'AsyncFunctionDef')):
                pass
            elif isinstance(node, ast.ExceptHandler):
                pass
            elif is_ast_node(node, (ast.Global, 'Nonlocal')):
                pass
            elif isinstance(node, ast.alias):
                if node.asname is None:
                    additional_bytes += 4  # ' as '
            elif isinstance(node, ast.arguments):
                if node.vararg == self._name:
                    pass
                if node.kwarg == self._name:
                    pass
            elif is_ast_node(node, 'arg'):
                if not arg_rename_in_place(node):
                    arg_rename = True

            elif is_ast_node(node, 'MatchAs'):
                if node.name is None:
                    additional_bytes += 4  # ' as '
            elif is_ast_node(node, 'MatchStar'):
                pass
            elif is_ast_node(node, 'MatchMapping'):
                pass

            else:
                raise AssertionError('Unknown reference node')

        return additional_bytes + (2 if arg_rename else 0)
Ejemplo n.º 2
0
    def old_mention_count(self):
        """
        The number of times the old name would be mentioned in the source code, if this binding was renamed
        """

        arg_rename = False
        mentions = 0

        for node in self._references:
            if isinstance(node, ast.Name):
                if isinstance(node.ctx, (ast.Load, ast.Store, ast.Del)):
                    pass
                else:
                    # Python 2 Param context
                    if not arg_rename_in_place(node):
                        mentions += 1
                        arg_rename = True

            elif is_ast_node(
                    node, (ast.ClassDef, ast.FunctionDef, 'AsyncFunctionDef')):
                pass
            elif isinstance(node, ast.ExceptHandler):
                pass
            elif is_ast_node(node, (ast.Global, 'Nonlocal')):
                pass
            elif isinstance(node, ast.alias):
                if node.asname is None:
                    # import foo -> import foo as bar
                    mentions += 1
            elif isinstance(node, ast.arguments):
                pass
            elif is_ast_node(node, 'arg'):
                if not arg_rename_in_place(node):
                    mentions += 1
                    arg_rename = True

            elif is_ast_node(node, 'MatchAs'):
                pass
            elif is_ast_node(node, 'MatchStar'):
                pass
            elif is_ast_node(node, 'MatchMapping'):
                pass

            else:
                raise AssertionError('Unknown reference node')

        return mentions + (1 if arg_rename else 0)
Ejemplo n.º 3
0
    def visit_arg(self, node):
        binding = self.get_binding(node.arg, node.namespace)

        if arg_rename_in_place(node):
            binding.add_reference(node)
        else:
            binding.add_reference(node, reserved=node.arg)

            if isinstance(node.namespace, ast.Lambda):
                # Lambda function arguments can't be renamed without breaking keyword arguments
                binding.disallow_rename()

        self.generic_visit(node)
Ejemplo n.º 4
0
    def visit_Name(self, node):
        if isinstance(node.ctx, (ast.Store, ast.Del)):
            self.get_binding(node.id, node.namespace).add_reference(node)

        if isinstance(node.ctx, ast.Param):
            binding = self.get_binding(node.id, node.namespace)

            if arg_rename_in_place(node):
                binding.add_reference(node)
            else:
                binding.add_reference(node, reserved=node.id)

                if isinstance(node.namespace, ast.Lambda):
                    # Lambda function arguments can't be renamed without breaking keyword arguments
                    binding.disallow_rename()
Ejemplo n.º 5
0
    def visit_arg(self, node):
        binding = self.get_binding(node.arg, node.namespace)

        if arg_rename_in_place(node):
            binding.add_reference(node)
        else:
            binding.add_reference(
                node, reserved=node.arg, rename_cost=(len(node.arg) * 2) +
                2)  # Assuming that the new name is only a single character

            if isinstance(node.namespace, ast.Lambda):
                # Lambda function arguments can't be renamed without breaking keyword arguments
                binding.disallow_rename()

        self.generic_visit(node)
Ejemplo n.º 6
0
    def visit_Name(self, node):
        if isinstance(node.ctx, ast.Store) or isinstance(node.ctx, ast.Del):
            self.get_binding(node.id, node.namespace).add_reference(node)

        if isinstance(node.ctx, ast.Param):
            binding = self.get_binding(node.id, node.namespace)

            if arg_rename_in_place(node):
                binding.add_reference(node)
            else:
                binding.add_reference(
                    node, reserved=node.id, rename_cost=(len(node.id) * 2) +
                    2)  # Assuming that the new name is only a single character

                if isinstance(node.namespace, ast.Lambda):
                    # Lambda function arguments can't be renamed without breaking keyword arguments
                    binding.disallow_rename()
Ejemplo n.º 7
0
    def rename(self, new_name):
        """
        Rename this binding and all nodes that reference it

        :param str new_name: The new name to use

        """

        for node in self.references:

            if isinstance(node, ast.Name):

                if isinstance(node.ctx, (ast.Load, ast.Store, ast.Del)):
                    node.id = new_name
                else:
                    # Python 2 Param context

                    if arg_rename_in_place(node):
                        node.id = new_name

                    else:
                        node.namespace.body = list(
                            insert(
                                node.namespace.body,
                                ast.Assign(
                                    targets=[
                                        ast.Name(id=new_name, ctx=ast.Store())
                                    ],
                                    value=ast.Name(id=node.id, ctx=ast.Load()),
                                ),
                            ))

            elif isinstance(node, ast.FunctionDef) or (
                    hasattr(ast, 'AsyncFunctionDef')
                    and isinstance(node, ast.AsyncFunctionDef)):
                node.name = new_name
            elif isinstance(node, ast.ClassDef):
                node.name = new_name
            elif isinstance(node, ast.alias):
                if new_name == node.name:
                    node.asname = None
                else:
                    node.asname = new_name
            elif hasattr(ast, 'arg') and isinstance(node, ast.arg):

                if arg_rename_in_place(node):
                    node.arg = new_name

                else:
                    node.namespace.body = list(
                        insert(
                            node.namespace.body,
                            ast.Assign(
                                targets=[
                                    ast.Name(id=new_name, ctx=ast.Store())
                                ],
                                value=ast.Name(id=node.arg, ctx=ast.Load()),
                            ),
                        ))

            elif isinstance(node, ast.ExceptHandler):
                node.name = new_name
            elif isinstance(
                    node, ast.Global) or (hasattr(ast, 'Nonlocal')
                                          and isinstance(node, ast.Nonlocal)):
                node.names = [
                    new_name if n == self._name else n for n in node.names
                ]
            elif isinstance(node, ast.arguments):

                if node.vararg == self._name:
                    node.vararg = new_name
                if node.kwarg == self._name:
                    node.kwarg = new_name

        self._name = new_name
Ejemplo n.º 8
0
    def rename(self, new_name):
        """
        Rename this binding and all nodes that reference it

        :param str new_name: The new name to use

        """

        func_namespace_binding = None

        for node in self.references:

            if isinstance(node, ast.Name):

                if isinstance(node.ctx, (ast.Load, ast.Store, ast.Del)):
                    node.id = new_name
                else:
                    # Python 2 Param context

                    if arg_rename_in_place(node):
                        node.id = new_name

                    else:
                        if func_namespace_binding is None:
                            func_namespace_binding = node.namespace
                        else:
                            assert func_namespace_binding is node.namespace

            elif is_ast_node(node, (ast.FunctionDef, 'AsyncFunctionDef')):
                node.name = new_name
            elif isinstance(node, ast.ClassDef):
                node.name = new_name
            elif isinstance(node, ast.alias):
                if new_name == node.name:
                    node.asname = None
                else:
                    node.asname = new_name
            elif is_ast_node(node, 'arg'):

                if arg_rename_in_place(node):
                    node.arg = new_name

                else:
                    if func_namespace_binding is None:
                        func_namespace_binding = node.namespace
                    else:
                        assert func_namespace_binding is node.namespace

            elif isinstance(node, ast.ExceptHandler):
                node.name = new_name
            elif is_ast_node(node, (ast.Global, 'Nonlocal')):
                node.names = [
                    new_name if n == self._name else n for n in node.names
                ]
            elif isinstance(node, ast.arguments):

                rename_vararg = (node.vararg == self._name) and not getattr(
                    node, 'vararg_renamed', False)
                rename_kwarg = (node.kwarg == self._name) and not getattr(
                    node, 'kwarg_renamed', False)

                if rename_vararg:
                    node.vararg = new_name
                    node.vararg_renamed = True
                if rename_kwarg:
                    node.kwarg = new_name
                    node.kwarg_renamed = True

            elif is_ast_node(node, 'MatchAs'):
                node.name = new_name
            elif is_ast_node(node, 'MatchStar'):
                node.name = new_name
            elif is_ast_node(node, 'MatchMapping'):
                node.rest = new_name

        if func_namespace_binding is not None:
            func_namespace_binding.body = list(
                insert(
                    func_namespace_binding.body,
                    ast.Assign(
                        targets=[ast.Name(id=new_name, ctx=ast.Store())],
                        value=ast.Name(id=self._name, ctx=ast.Load()),
                    ),
                ))

        self._name = new_name