Пример #1
0
def check_hammer_step_function(func: HammerStepFunction) -> None:
    """Internal alias for checking HammerStepFunction signatures."""
    assert_function_type(func, args=[HammerTool], return_type=bool)
Пример #2
0
def check_extraction_func(func: ExtractionFunctionType) -> None:
    """
    Check that the given function obeys the paths_func type specification.
    """
    assert_function_type(func, ["Library", List[str]],
                         List[str])  # type: ignore
Пример #3
0
def check_filter_func(func: Callable[["Library"], bool]) -> None:
    """
    Check that the given function obeys the filter_func type specification.
    """
    assert_function_type(func, ["Library"], bool)  # type: ignore
Пример #4
0
    def test_check_function_type(self) -> None:
        def test1(x: int) -> str:
            return str(x + 5)

        assert_function_type(test1, [int], str)

        def test2(a: int, b: float) -> None:
            print("{a}{b}".format(a=a, b=b))

        assert_function_type(test2, [int, float], None)  # type: ignore

        def test3(a: int, b: int) -> List[int]:
            return [a, b]

        assert_function_type(test3, [int, int], List[int])

        def test4(a: int, b: int) -> List[int]:
            return [a, b]

        assert_function_type(test4, [int, int], List[int])

        # Check that dict == typing.Dict, etc.
        def test5(a: int) -> dict:
            return {"a": a}

        assert_function_type(test5, [int], Dict)

        # Check that dict == typing.Dict, etc.
        def test6(a: int) -> Optional[dict]:
            return {"a": a}

        assert_function_type(test6, [int], Optional[Dict])

        # Check that Union types get handled properly.
        def test7(a: Union[int, float]) -> Union[bool, str]:
            return str(a)

        assert_function_type(test7, [Union[int, float]], Union[bool, str])

        # Check that stringly-typed annotations work.
        def test8(a: "int") -> "list":
            return [a + a]

        assert_function_type(test8, ["int"], "list")  # type: ignore
        assert_function_type(test8, ["int"], list)  # type: ignore
        assert_function_type(test8, [int], "list")  # type: ignore
        assert_function_type(test8, [int], list)

        with self.assertRaises(TypeError):
            # Different # of arguments
            assert_function_type(test1, [int, int], str)
        with self.assertRaises(TypeError):
            # Different return type
            assert_function_type(test1, [int], bool)
        with self.assertRaises(TypeError):
            # Different argument type
            assert_function_type(test1, [str], str)
        with self.assertRaises(TypeError):
            # Different # of arguments and different return type
            assert_function_type(test3, [int], bool)
        with self.assertRaises(TypeError):
            # Entirely different
            assert_function_type(test3, [], dict)
Пример #5
0
def check_hammer_step_function(func: HammerStepFunction) -> None:
    """Internal alias for checking HammerStepFunction signatures."""
    assert_function_type(func, args=[HammerTool], return_type=bool)
Пример #6
0
    def test_check_function_type(self) -> None:
        def test1(x: int) -> str:
            return str(x + 5)

        assert_function_type(test1, [int], str)

        def test2(a: int, b: float) -> None:
            print("{a}{b}".format(a=a, b=b))

        assert_function_type(test2, [int, float], None)  # type: ignore

        def test3(a: int, b: int) -> List[int]:
            return [a, b]

        assert_function_type(test3, [int, int], List[int])

        def test4(a: int, b: int) -> List[int]:
            return [a, b]

        assert_function_type(test4, [int, int], List[int])

        # Check that dict == typing.Dict, etc.
        def test5(a: int) -> dict:
            return {"a": a}

        assert_function_type(test5, [int], Dict)

        # Check that dict == typing.Dict, etc.
        def test6(a: int) -> Optional[dict]:
            return {"a": a}

        assert_function_type(test6, [int], Optional[Dict])

        # Check that Union types get handled properly.
        def test7(a: Union[int, float]) -> Union[bool, str]:
            return str(a)

        assert_function_type(test7, [Union[int, float]], Union[bool, str])

        # Check that stringly-typed annotations work.
        def test8(a: "int") -> "list":
            return [a + a]

        assert_function_type(test8, ["int"], "list")  # type: ignore
        assert_function_type(test8, ["int"], list)  # type: ignore
        assert_function_type(test8, [int], "list")  # type: ignore
        assert_function_type(test8, [int], list)

        # Check that untyped arguments don't crash.
        def test9(x) -> str:
            return str(x)

        test9_return = check_function_type(test9, [int], str)
        assert test9_return is not None
        self.assertTrue("incorrect signature" in test9_return)

        with self.assertRaises(TypeError):
            # Different # of arguments
            assert_function_type(test1, [int, int], str)
        with self.assertRaises(TypeError):
            # Different return type
            assert_function_type(test1, [int], bool)
        with self.assertRaises(TypeError):
            # Different argument type
            assert_function_type(test1, [str], str)
        with self.assertRaises(TypeError):
            # Different # of arguments and different return type
            assert_function_type(test3, [int], bool)
        with self.assertRaises(TypeError):
            # Entirely different
            assert_function_type(test3, [], dict)
Пример #7
0
def check_filter_func(func: Callable[["Library"], bool]) -> None:
    """
    Check that the given function obeys the filter_func type specification.
    """
    assert_function_type(func, ["Library"], bool)  # type: ignore
Пример #8
0
def check_extraction_func(func: ExtractionFunctionType) -> None:
    """
    Check that the given function obeys the paths_func type specification.
    """
    assert_function_type(func, ["Library", List[str]], List[str])  # type: ignore