Beispiel #1
0
def test_relative_imports():
    tree = ast.parse(CODE_RELATIVE_IMPORTS)
    node1 = tree.body[2].value
    node2 = tree.body[3].value
    name1 = decode(tree, node1)
    name2 = decode(tree, node2)
    assert name1 == ".foo.X"
    assert name2 == "..bar.Y"
Beispiel #2
0
def test_assignment():
    tree = ast.parse(CODE_ASSIGNMENT)
    node1 = tree.body[2].value
    node2 = tree.body[3].value
    name1 = decode(tree, node1)
    name2 = decode(tree, node2)
    assert name1 == "os.path"
    assert name2 == "os.path.join"
Beispiel #3
0
def test_noop():
    tree = ast.parse(CODE_NOOP)
    node1 = tree.body[1].value.func
    node2 = tree.body[2].body[0].value.func
    name1 = decode(tree, node1)
    name2 = decode(tree, node2)
    assert name1 == "foo"
    assert name2 == "b.ar"
Beispiel #4
0
def test_global_import():
    tree = ast.parse(CODE_GLOBAL_IMPORT)
    node1 = tree.body[3].value
    node2 = tree.body[4].value
    node3 = tree.body[5].value
    node4 = tree.body[6].value
    name1 = decode(tree, node1)
    name2 = decode(tree, node2)
    name3 = decode(tree, node3)
    name4 = decode(tree, node4)
    assert name1 == "collections.defaultdict"
    assert name2 == "xml.et"
    assert name3 == "xml.et.ElementTree"
    assert name4 == "os.path"
Beispiel #5
0
def test_ancestry():
    tree = ast.parse(CODE_ANCESTRY)
    node1 = tree.body[1].body[2].body[1].value
    node2 = tree.body[1].body[2].body[2].value
    node3 = tree.body[1].body[2].body[3].value
    node4 = tree.body[1].body[2].body[4].value
    node5 = tree.body[1].body[2].body[5].value
    name1 = decode(tree, node1)
    name2 = decode(tree, node2)
    name3 = decode(tree, node3)
    name4 = decode(tree, node4)
    name5 = decode(tree, node5)
    assert name1 == "typing.List"
    assert name2 == "typing.Dict"
    assert name3 == "Union"
    assert name4 == "typing.Tuple"
    assert name5 == "typing.Set"
Beispiel #6
0
def test_nontrivial_assign():
    tree = ast.parse(CODE_NONTRIVIAL_ASSIGN)
    node1 = tree.body[0].targets[0]
    node2 = tree.body[1].targets[0]
    node3 = tree.body[2].targets[0]
    decode(tree, node1.value)
    decode(tree, node2.value.func)
    decode(tree, node3)
Beispiel #7
0
    def _use_better_alternative(
            self, type_hint: ast.AST,
            error_code: int) -> Iterable[Tuple[int, int, str, type]]:
        if error_code in ERROR_CODES_GENERIC_ALT and not self.flags.generic_alt:
            return []
        if error_code in ERROR_CODES_ALIAS_ALT and not self.flags.alias_alt:
            return []
        if error_code in ERROR_CODES_GENERAL_ARGS and not self.flags.general_args:
            return []

        while isinstance(type_hint, ast.Subscript):
            type_hint = type_hint.value

        if isinstance(type_hint, ast.Name) or isinstance(
                type_hint, ast.Attribute):
            if (ast_import_decode.decode(self.tree, type_hint)
                    in BETTER_ALTERNATIVES[error_code]):
                yield (
                    type_hint.lineno,
                    type_hint.col_offset,
                    f"TYCO{error_code} " + ERROR_MESSAGES[error_code],
                    Checker,
                )
Beispiel #8
0
def test_try_if_while():
    tree = ast.parse(CODE_TRY_IF_WHILE)
    node1 = tree.body[3].value
    node2 = tree.body[4].value
    node3 = tree.body[5].value
    node4 = tree.body[6].value
    node5 = tree.body[7].value
    node6 = tree.body[8].value
    name1 = decode(tree, node1)
    name2 = decode(tree, node2)
    name3 = decode(tree, node3)
    name4 = decode(tree, node4)
    name5 = decode(tree, node5)
    name6 = decode(tree, node6)
    assert name1 == "path"
    assert name2 == "col"
    assert name3 == "typing.List"
    assert name4 == "Set"
    assert name5 == "typing.Dict"
    assert name6 == "typing.Tuple"