Пример #1
0
def test_type_hint_annotations():
    # Annotated functions in the form `def annotated_func(x1: int, x2: int):` will cause syntax errors in Python 2
    # To enable testing on all supported runtimes, fake the annotations by setting __annotations__ directly
    def annotated_func(x1, x2):
        pass
    annotated_func.__annotations__ = {'x1': int, 'x2': int}
    result = parse_type_hints(annotated_func)
    assert result == dict(x1=('int', False), x2=('int', False))
Пример #2
0
def test_parse_type_hints_source():
    def bad_func(x1, x2):
        return x1 + x2

    with pytest.raises(ValueError):
        parse_type_hints(bad_func)

    def func1(x1, x2):
        # type: (str, int) -> (float, bool)
        pass
    result = parse_type_hints(func1)
    assert result == OrderedDict([('x1', ('str', False)), ('x2', ('int', False)),
                                   ('out1', ('float', True)), ('out2', ('bool', True))])

    def func2(x1, x2):
        # type: float, float -> float
        pass
    result = parse_type_hints(func2)
    assert result == OrderedDict([('x1', ('float', False)), ('x2', ('float', False)), ('out1', ('float', True))])

    def func3(x1, x2):
        # type: float, float -> float, int
        pass
    result = parse_type_hints(func3)
    assert result == OrderedDict([('x1', ('float', False)), ('x2', ('float', False)),
                                  ('out1', ('float', True)), ('out2', ('int', True))])

    def func4(x1, x2):
        # type: str, int
        pass
    result = parse_type_hints(func4)
    assert result == OrderedDict([('x1', ('str', False)), ('x2', ('int', False))])

    def func5(x1, x2):
        # type: (str, int)
        pass
    result = parse_type_hints(func5)
    assert result == OrderedDict([('x1', ('str', False)), ('x2', ('int', False))])

    def func6(self, x1, x2):
        # type: (str, int)
        pass
    result = parse_type_hints(func6)
    assert result == OrderedDict([('x1', ('str', False)), ('x2', ('int', False))])

    class TestClass:
        def func(self, x1, x2):
            # type: (str, int)
            pass
    result = parse_type_hints(TestClass().func)
    assert result == OrderedDict([('x1', ('str', False)), ('x2', ('int', False))])