Beispiel #1
0
    def leave_FunctionDef(
        self, original_node: "FunctionDef", updated_node: "FunctionDef"
    ) -> Union["BaseStatement", RemovalSentinel]:
        modified_defaults = []
        mutable_args = {}

        for default_param in original_node.params.default_params:
            if isinstance(default_param.default, (List, Dict)):
                mutable_args[
                    default_param.name] = default_param.default.deep_clone()
                modified_defaults.append(
                    default_param.with_changes(default=Name("None")))
            else:
                modified_defaults.append(default_param)

        modified_params = original_node.params.with_changes(
            default_params=modified_defaults)

        inits = [
            If(
                test=Comparison(
                    left=Name(value=arg.value, lpar=[], rpar=[]),
                    comparisons=[
                        ComparisonTarget(
                            operator=Is(),
                            comparator=Name(value="None", lpar=[], rpar=[]),
                        )
                    ],
                ),
                body=IndentedBlock(
                    body=[
                        SimpleStatementLine(body=[
                            Assign(
                                targets=[
                                    AssignTarget(target=Name(value=arg.value))
                                ],
                                value=init,
                            )
                        ])
                    ],
                    footer=[Newline(value=None)],
                ),
            ) for arg, init in mutable_args.items()
        ]

        modified_body = (*inits, *original_node.body.body)

        return updated_node.with_changes(
            params=modified_params,
            body=original_node.body.with_changes(body=modified_body),
        )
Beispiel #2
0
    def leave_Module(self, original_node: "Module",
                     updated_node: "Module") -> "Module":
        if not self.names:
            return original_node

        modified_body = list(original_node.body)

        indented_space = ParenthesizedWhitespace(
            first_line=TrailingWhitespace(
                whitespace=SimpleWhitespace(value=""),
                comment=None,
                newline=Newline(value=None),
            ),
            empty_lines=[],
            indent=True,
            last_line=SimpleWhitespace(value="    "),
        )

        indented_comma = Comma(
            whitespace_before=SimpleWhitespace(value=""),
            whitespace_after=indented_space,
        )
        line_break = ParenthesizedWhitespace(first_line=TrailingWhitespace(
            whitespace=SimpleWhitespace(value=""),
            comment=None,
            newline=Newline(value=None),
        ))

        list_values = [
            Element(SimpleString(value=f'"{global_name}"'),
                    comma=indented_comma) for global_name in self.names[:-1]
        ]
        list_values.append(
            Element(
                SimpleString(value=f'"{self.names[-1]}"'),
                comma=Comma(
                    whitespace_before=SimpleWhitespace(value=""),
                    whitespace_after=line_break,
                ),
            ))

        all_names = Assign(
            targets=(AssignTarget(target=Name(value="__all__")), ),
            value=List(
                list_values,
                lbracket=LeftSquareBracket(
                    whitespace_after=ParenthesizedWhitespace(
                        first_line=TrailingWhitespace(
                            whitespace=SimpleWhitespace(value=""),
                            comment=None,
                            newline=Newline(value=None),
                        ),
                        empty_lines=[],
                        indent=True,
                        last_line=SimpleWhitespace(value="    "),
                    )),
                rbracket=RightSquareBracket(whitespace_before=SimpleWhitespace(
                    value="")),
            ),
        )

        modified_body.append(Newline())
        modified_body.append(all_names)
        return updated_node.with_changes(body=modified_body)