Exemplo n.º 1
0
    def test_parse_mypy_result_multiple(self):
        """Test if the parse_mypy_result method returns the correct types."""
        mypy_result = "<string>:1: note: Revealed type is 'builtins.int'"
        mypy_result += "\n<string>:2: note: Revealed type is 'builtins.str'"

        result = TypeInference.parse_mypy_result(mypy_result)
        assert result == [(1, "'builtins.int'"), (2, "'builtins.str'")]
Exemplo n.º 2
0
 def visit_module(self, module: astroid.Module):
     """Visit module and infer which libraries the variables are from."""
     try:
         self._variable_types = TypeInference.infer_library_variable_first_types(
             module)
     except:  # pylint: disable = bare-except
         ExceptionHandler.handle(self, module)
Exemplo n.º 3
0
    def test_add_reveal_type_calls_block(self):
        """Test if the reveal_type() call is added to the body of a block statement."""
        code = "y = ''\nfor x in y.join([]):\n\tpass"
        nodes = [astroid.parse(code).body[1].iter]

        result = TypeInference.add_reveal_type_calls(
            code, nodes, lambda node: node.func.expr.name)
        assert result == "y = ''\nfor x in y.join([]):\n\tpass; reveal_type(y)"
Exemplo n.º 4
0
    def test_add_reveal_type_calls(self):
        """Test the add_reveal_type_calls() method with a single expression."""
        code = "a = b.c(d)"
        nodes = [astroid.parse(code).body[0].value]

        result = TypeInference.add_reveal_type_calls(
            code, nodes, lambda node: node.func.expr.name)
        assert result == "a = b.c(d); reveal_type(b)"
Exemplo n.º 5
0
    def test_add_reveal_type_calls_multiple(self):
        """Test the add_reveal_type_calls() method with multiple expressions."""
        code = "a = b.c(d)\nx = 5 \ne = f.g()"
        tree = astroid.parse(code)
        nodes = [tree.body[0].value, tree.body[2].value]

        result = TypeInference.add_reveal_type_calls(
            code, nodes, lambda node: node.func.expr.name)
        assert result == "a = b.c(d); reveal_type(b)\nx = 5 \ne = f.g(); reveal_type(f)"
Exemplo n.º 6
0
    def visit_module(self, node: astroid.Module):
        """
        When an Module node is visited, scan for Call nodes and get type the function is called on.

        :param node: Node which is visited.
        """
        try:
            # noinspection PyTypeChecker
            self._call_types = TypeInference.infer_types(
                node, astroid.Call, lambda x: x.func.expr.name)
        except:  # pylint: disable=bare-except
            ExceptionHandler.handle(self, node)
Exemplo n.º 7
0
 def visit_module(self, module: astroid.Module):
     try:
         self._variables_with_processing_operation = TypeInference.infer_variable_full_types(
             module)
     except:  # pylint: disable = bare-except
         ExceptionHandler.handle(self, module)
Exemplo n.º 8
0
 def test_combine_nodes_with_inferred_types(self):
     """Test if combine_nodes_with_inferred_types returns a correct dict on a single line."""
     nodes = [astroid.extract_node("a.b()")]
     types = [(1, "builtins.str")]
     result = TypeInference.combine_nodes_with_inferred_types(nodes, types)
     assert result == {nodes[0]: types[0][1]}
Exemplo n.º 9
0
 def test_parse_mypy_result(self):
     """Test if the parse_mypy_result method returns the correct type."""
     mypy_result = "<string>:1: note: Revealed type is 'builtins.int'"
     assert TypeInference.parse_mypy_result(mypy_result) == [
         (1, "'builtins.int'")
     ]
Exemplo n.º 10
0
 def test_run_mypy_error(self):
     """Test if mypy returns an error when code is incorrect."""
     result = TypeInference.run_mypy("a: str = 5")
     assert result.splitlines(
     )[1] == "Found 1 error in 1 file (checked 1 source file)"
Exemplo n.º 11
0
 def test_run_mypy_success(self):
     """Test if mypy is ran successfully on some correct code."""
     result = TypeInference.run_mypy("a = 5")
     assert result == "Success: no issues found in 1 source file\n"