def type_of_iterable_views_is_sniffed_from_elements():
    assert_equal(types.iterable(types.describe(int)), describe_type_of({1: "Kentucky Pill"}.keys()))
    assert_equal(types.iterable(types.describe(str)), describe_type_of({1: "Kentucky Pill"}.values()))
    assert_equal(
        types.iterable(types.tuple_((types.describe(int), types.describe(str)))),
        describe_type_of({1: "Kentucky Pill"}.items()),
    )
示例#2
0
def common_superclass_is_preferred_over_union_of_four_or_more_types_even_when_additional_methods_are_defined():
    class Base(object):
        __metaclass__ = abc.ABCMeta
        
        @abc.abstractmethod
        def f(self):
            pass
    
    class A(Base):
        def f(self):
            pass
    
    class B(Base):
        def f(self):
            pass
        
        def g(self):
            pass
    
    class C(Base):
        def f(self):
            pass
    
    class D(Base):
        def f(self):
            pass
    
    assert_equal(
        union([describe(Base)]),
        common_super_type([describe(A), describe(B), describe(C), describe(D)])
    )
示例#3
0
def returns_from_inner_and_outer_function_can_be_distinguished():
    program = """
def answer():
    def f():
        return 42
    return float(f())

print(answer())
"""
    trace = _run_and_trace(program)
    assert_that(trace, m.has_items(
        m.has_properties({
            "location": m.has_properties({
                "lineno": 2,
                "col_offset": 0
            }),
            "returns": describe(float)
        }),
        m.has_properties({
            "location": m.has_properties({
                "lineno": 3,
                "col_offset": 4
            }),
            "returns": describe(int)
        }),
    ))
示例#4
0
def callable_has_callable_type():
    assert_equal(
        "Callable[[int, str], bool]",
        format_type(types.callable_(
            [(None, types.describe(int)), (None, types.describe(str))],
            types.describe(bool)
        )),
    )
示例#5
0
def type_of_keyword_only_arguments_is_traced():
    program = """
def repeat(x, *, y):
    return x * y

print(repeat("hello ", y=3))
"""
    trace = _run_and_trace(program)
    assert_that(trace, m.has_item(m.has_properties({
        "location": m.has_properties({
            "lineno": 2,
            "col_offset": 0
        }),
        "args": m.has_entries({
            "x": describe(str),
            "y": describe(int),
        })
    })))
示例#6
0
def abstract_base_class_is_selected_as_type_if_all_types_are_instances_of_base_class():
    class Base(object):
        __metaclass__ = abc.ABCMeta
        
        @abc.abstractmethod
        def f(self):
            pass
    
    class A(Base):
        def f(self):
            pass
    
    class B(Base):
        def f(self):
            pass
    
    assert_equal(
        describe(Base),
        common_super_type([describe(A), describe(B)])
    )
示例#7
0
def abstract_base_class_is_not_selected_as_type_if_sub_type_defines_additional_method():
    class Base(object):
        __metaclass__ = abc.ABCMeta
        
        @abc.abstractmethod
        def f(self):
            pass
    
    class A(Base):
        def f(self):
            pass
    
    class B(Base):
        def f(self):
            pass
        
        def g(self):
            pass
    
    assert_equal(
        union([describe(A), describe(B)]),
        common_super_type([describe(A), describe(B)])
    )
示例#8
0
def type_of_return_with_explicit_value_is_traced():
    program = """
def answer():
    return 42

print(answer())
"""
    trace = _run_and_trace(program)
    assert_that(trace, m.has_item(m.has_properties({
        "location": m.has_properties({
            "lineno": 2,
            "col_offset": 0
        }),
        "returns": describe(int)
    })))
示例#9
0
def type_of_return_with_implicit_value_is_traced():
    program = """
def do_nothing():
    return

print(do_nothing())
"""
    trace = _run_and_trace(program)
    assert_that(trace, m.has_item(m.has_properties({
        "location": m.has_properties({
            "lineno": 2,
            "col_offset": 0
        }),
        "returns": describe(type(None))
    })))
示例#10
0
def type_of_raised_exception_is_traced():
    program = """
def do_nothing():
    assert False

print(do_nothing())
"""
    trace = _run_and_trace(program)
    assert_that(trace, m.has_item(m.has_properties({
        "location": m.has_properties({
            "lineno": 2,
            "col_offset": 0
        }),
        "returns": None,
        "raises": describe(AssertionError)
    })))
示例#11
0
def builtin_is_formatted_as_name_of_builtin():
    assert_equal("int", format_type(types.describe(int)))
示例#12
0
def unions_are_flattened():
    assert_equal(
        types.union([types.describe(int), types.describe(str), types.describe(float)]),
        types.union([types.describe(int), types.union([types.describe(str), types.describe(float)])])
    )
示例#13
0
def union_of_one_type_is_that_type():
    assert_equal(types.describe(int), types.union([types.describe(int)]))
示例#14
0
def union_uses_union_class_with_getitem():
    type_ = types.union([types.describe(type(None)), types.describe(int)])
    assert_equal("Union[None, int]", format_type(type_))
示例#15
0
def unions_are_order_agnostic():
    assert_equal(
        types.union([types.describe(str), types.describe(int)]),
        types.union([types.describe(int), types.describe(str)])
    )
示例#16
0
def none_is_formatted_as_none_instead_of_none_type():
    assert_equal("None", format_type(types.describe(type(None))))
示例#17
0
def list_uses_list_class_from_pep_484():
    assert_equal("List[int]", format_type(types.List(types.describe(int))))
示例#18
0
def lists_of_different_types_have_super_type_of_list_of_super_type_of_elements():
    assert_equal(
        union([List(union([describe(str), describe(int)]))]),
        common_super_type([List(describe(str)), List(describe(int))])
    )
示例#19
0
def common_super_type_of_dict_of_different_element_types_is_union_of_dict_types():
    assert_equal(
        union([Dict(describe(int), describe(str)), Dict(describe(str), describe(int))]),
        common_super_type([Dict(describe(int), describe(str)), Dict(describe(str), describe(int))])
    )
示例#20
0
def type_of_dict_is_sniffed_from_keys_and_values():
    assert_equal(types.dict_(types.describe(int), types.describe(str)), describe_type_of({1: "Kentucky Pill"}))
示例#21
0
def list_of_concrete_type_squashes_list_of_any_type_in_presence_of_other_types():
    assert_equal(
        union([List(describe(int)), describe(type(None))]),
        common_super_type([List(any_), List(describe(int)), describe(type(None))])
    )
示例#22
0
def iterable_uses_iterable_class_from_pep_484():
    assert_equal("Iterable[int]", format_type(types.iterable(types.describe(int))))
示例#23
0
def common_super_type_of_repeated_single_type_is_that_type():
    assert_equal(describe(int), common_super_type([describe(int), describe(int)]))
示例#24
0
def dict_uses_dict_class_from_pep_484():
    assert_equal("Dict[int, str]", format_type(types.Dict(types.describe(int), types.describe(str))))
示例#25
0
def type_of_tuple_is_sniffed_from_elements():
    assert_equal(types.tuple_((types.describe(int), types.describe(str))), describe_type_of((1, "Kentucky Pill")))
示例#26
0
def common_super_type_of_dict_of_any_to_any_and_other_dict_is_other_dict():
    assert_equal(
        Dict(describe(int), describe(str)),
        common_super_type([Dict(any_, any_), Dict(describe(int), describe(str))])
    )
示例#27
0
def common_super_type_of_types_with_none_is_union_with_none():
    assert_equal(
        union([describe(type(None)), describe(int)]),
        common_super_type([describe(type(None)), describe(int)])
    )
示例#28
0
def tuple_uses_tuple_class_from_pep_484():
    assert_equal("Tuple[int, str]", format_type(types.Tuple((types.describe(int), types.describe(str)))))
示例#29
0
def common_super_type_of_list_of_any_and_list_of_other_type_is_list_of_other_type():
    assert_equal(
        List(describe(int)),
        common_super_type([List(any_), List(describe(int))])
    )
示例#30
0
def type_of_list_of_ints_is_list_of_ints():
    assert_equal(types.list_(types.describe(int)), describe_type_of([42]))