Exemple #1
0
def _fix_import(i: int, tokens: List[Token]) -> None:
    fix_brace(
        tokens,
        _find_import(i, tokens),
        add_comma=True,
        remove_comma=True,
    )
Exemple #2
0
def _fix_src(contents_text: str, min_version: Tuple[int, ...]) -> str:
    try:
        ast_obj = ast_parse(contents_text)
    except SyntaxError:
        return contents_text

    callbacks = visit(FUNCS, ast_obj, min_version)

    tokens = src_to_tokens(contents_text)
    for i, token in _changing_list(tokens):
        # DEDENT is a zero length token
        if not token.src:
            continue

        # though this is a defaultdict, by using `.get()` this function's
        # self time is almost 50% faster
        for callback in callbacks.get(token.offset, ()):
            callback(i, tokens)

        if token.src in START_BRACES:
            fix_brace(
                tokens,
                find_simple(i, tokens),
                add_comma=False,
                remove_comma=False,
            )

    return tokens_to_src(tokens)
Exemple #3
0
def _fix_class(
    i: int,
    tokens: List[Token],
    *,
    arg_offsets: Set[Offset],
) -> None:
    fix_brace(
        tokens,
        find_call(arg_offsets, i, tokens),
        add_comma=True,
        remove_comma=True,
    )
Exemple #4
0
def _fix_tuple(
    i: int,
    tokens: List[Token],
    *,
    one_el_tuple: bool,
) -> None:
    fix_brace(
        tokens,
        _find_tuple(i, tokens),
        add_comma=True,
        remove_comma=not one_el_tuple,
    )
Exemple #5
0
def _fix_literal(
    i: int,
    tokens: List[Token],
    *,
    one_el_tuple: bool,
) -> None:
    if tokens[i].src in START_BRACES:  # pragma: no branch (<py38)
        fix_brace(
            tokens,
            find_simple(i, tokens),
            add_comma=True,
            remove_comma=not one_el_tuple,
        )
Exemple #6
0
def _fix_call(
    i: int,
    tokens: List[Token],
    *,
    add_comma: bool,
    arg_offsets: Set[Offset],
) -> None:
    return fix_brace(
        tokens,
        find_call(arg_offsets, i, tokens),
        add_comma=add_comma,
        remove_comma=True,
    )