How to use the pyupgrade.Py2CompatibleVisitor function in pyupgrade

To help you get started, we’ve selected a few pyupgrade examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github asottile / pyupgrade / pyupgrade.py View on Github external
def _fix_py2_compatible(contents_text):  # type: (str) -> str
    try:
        ast_obj = ast_parse(contents_text)
    except SyntaxError:
        return contents_text
    visitor = Py2CompatibleVisitor()
    visitor.visit(ast_obj)
    if not any((
            visitor.dicts,
            visitor.sets,
            visitor.set_empty_literals,
            visitor.is_literal,
    )):
        return contents_text

    try:
        tokens = src_to_tokens(contents_text)
    except tokenize.TokenError:  # pragma: no cover (bpo-2180)
        return contents_text
    for i, token in reversed_enumerate(tokens):
        if token.offset in visitor.dicts:
            _process_dict_comp(tokens, i, visitor.dicts[token.offset])

pyupgrade

A tool to automatically upgrade syntax for newer versions.

MIT
Latest version published 17 days ago

Package Health Score

85 / 100
Full package analysis

Similar packages