Esempio n. 1
0
def convert_annassign(config: ParserConfig, children: Sequence[Any]) -> Any:
    if len(children) == 2:
        # Variable annotation only
        colon, annotation = children
        annotation = annotation.value
        equal = None
        value = None
    elif len(children) == 4:
        # Variable annotation and assignment
        colon, annotation, equal, value = children
        annotation = annotation.value
        value = value.value
        equal = AssignEqual(
            whitespace_before=parse_simple_whitespace(config,
                                                      equal.whitespace_before),
            whitespace_after=parse_simple_whitespace(config,
                                                     equal.whitespace_after),
        )
    else:
        raise Exception("Invalid parser state!")

    return AnnAssignPartial(
        annotation=Annotation(
            whitespace_before_indicator=parse_simple_whitespace(
                config, colon.whitespace_before),
            whitespace_after_indicator=parse_simple_whitespace(
                config, colon.whitespace_after),
            annotation=annotation,
        ),
        equal=equal,
        value=value,
    )
Esempio n. 2
0
def convert_assign(config: ParserConfig, children: Sequence[Any]) -> Any:
    equal, expr = children
    return AssignPartial(
        equal=AssignEqual(
            whitespace_before=parse_simple_whitespace(config, equal.whitespace_before),
            whitespace_after=parse_simple_whitespace(config, equal.whitespace_after),
        ),
        value=expr.value,
    )
Esempio n. 3
0
def convert_fpdef_assign(config: ParserConfig, children: Sequence[Any]) -> Any:
    if len(children) == 1:
        (child, ) = children
        return child

    param, equal, default = children
    return param.with_changes(
        equal=AssignEqual(
            whitespace_before=parse_parenthesizable_whitespace(
                config, equal.whitespace_before),
            whitespace_after=parse_parenthesizable_whitespace(
                config, equal.whitespace_after),
        ),
        default=default.value,
    )
Esempio n. 4
0
def convert_arg_assign_comp_for(
        config: ParserConfig,
        children: typing.Sequence[typing.Any]) -> typing.Any:
    if len(children) == 1:
        # Simple test
        (child, ) = children
        return Arg(value=child.value)
    elif len(children) == 2:
        elt, for_in = children
        return Arg(value=GeneratorExp(elt.value, for_in, lpar=(), rpar=()))
    else:
        # "key = value" assignment argument
        lhs, equal, rhs = children
        return Arg(
            keyword=lhs.value,
            equal=AssignEqual(
                whitespace_before=parse_parenthesizable_whitespace(
                    config, equal.whitespace_before),
                whitespace_after=parse_parenthesizable_whitespace(
                    config, equal.whitespace_after),
            ),
            value=rhs.value,
        )