Beispiel #1
0
def check_CLIActionType_type(func: CLIActionType) -> None:
    """
    Check that the given CLIActionType obeys its function type signature.
    Raises TypeError if the function is of the incorrect type.
    """
    config_check = check_function_type(func, [HammerDriver, Callable[[str], None]], Optional[dict])
    if config_check is None:
        return

    string_check = check_function_type(func, [HammerDriver, Callable[[str], None]], Optional[str])
    if string_check is None:
        return

    raise TypeError(
        "func does not appear to be a CLIActionType. Check for config returned {config}; check for string returned {string}".format(
            config=config_check, string=string_check))
Beispiel #2
0
def check_CLIActionType_type(func: CLIActionType) -> None:
    """
    Check that the given CLIActionType obeys its function type signature.
    Raises TypeError if the function is of the incorrect type.
    """
    config_check = check_function_type(func,
                                       [HammerDriver, Callable[[str], None]],
                                       Optional[dict])
    if config_check is None:
        return

    string_check = check_function_type(func,
                                       [HammerDriver, Callable[[str], None]],
                                       Optional[str])
    if string_check is None:
        return

    raise TypeError(
        "func does not appear to be a CLIActionType. Check for config returned {config}; check for string returned {string}"
        .format(config=config_check, string=string_check))
Beispiel #3
0
    def test_check_function_type(self) -> None:
        def test1(x: int) -> str:
            return str(x + 5)

        check_function_type(test1, [int], str)

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

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

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

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

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

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

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

        check_function_type(test5, [int], Dict)

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

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

        with self.assertRaises(TypeError):
            # Different # of arguments
            check_function_type(test1, [int, int], str)
        with self.assertRaises(TypeError):
            # Different return type
            check_function_type(test1, [int], bool)
        with self.assertRaises(TypeError):
            # Different argument type
            check_function_type(test1, [str], str)
        with self.assertRaises(TypeError):
            # Different # of arguments and different return type
            check_function_type(test3, [int], bool)
        with self.assertRaises(TypeError):
            # Entirely different
            check_function_type(test3, [], dict)
Beispiel #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)

        # 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)
Beispiel #5
0
def is_string_action(func: CLIActionType) -> bool:
    """Return True if the given function is a CLIActionConfigType."""
    return check_function_type(func, [HammerDriver, Callable[[str], None]],
                               Optional[str]) is None
Beispiel #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)
Beispiel #7
0
def is_string_action(func: CLIActionType) -> bool:
    """Return True if the given function is a CLIActionConfigType."""
    return check_function_type(func, [HammerDriver, Callable[[str], None]], Optional[str]) is None