Example #1
0
def dict_literal_uses_type_hint_when_valid():
    assert_equal(
        types.dict_type(types.object_type, types.int_type),
        infer(nodes.dict_literal([
            (nodes.str_literal("Hello"), nodes.int_literal(42)),
        ]), hint=types.dict_type(types.object_type, types.int_type))
    )
    assert_equal(
        types.dict_type(types.str_type, types.object_type),
        infer(nodes.dict_literal([
            (nodes.str_literal("Hello"), nodes.int_literal(42)),
        ]), hint=types.dict_type(types.str_type, types.object_type))
    )
Example #2
0
def dict_literal_type_hint_is_ignored_if_hint_item_type_is_not_super_type_of_actual_item_types():
    assert_equal(
        types.dict_type(types.str_type, types.int_type),
        infer(nodes.dict_literal([
            (nodes.str_literal("Hello"), nodes.int_literal(42)),
        ]), hint=types.dict_type(types.bottom_type, types.int_type))
    )
    assert_equal(
        types.dict_type(types.str_type, types.int_type),
        infer(nodes.dict_literal([
            (nodes.str_literal("Hello"), nodes.int_literal(42)),
        ]), hint=types.dict_type(types.str_type, types.bottom_type))
    )
Example #3
0
def test_parse_dict_literal():
    _assert_expression_parse(
        nodes.dict_literal([
            (nodes.str_literal("hello"), nodes.int_literal(4)),
            (nodes.str_literal("there"), nodes.int_literal(5)),
        ]),
        "{'hello': 4, 'there': 5}"
    )
Example #4
0
def type_of_dict_is_determined_by_unifying_types_of_keys_and_values():
    assert_equal(
        types.dict_type(types.str_type, types.int_type),
        infer(nodes.dict_literal([
            (nodes.str_literal("Hello"), nodes.int_literal(42)),
            (nodes.str_literal("Blah"), nodes.int_literal(16)),
        ]))
    )
Example #5
0
 def dict_containing_literal_key_and_literal_value_is_literal(self):
     assert is_literal(nodes.dict_literal([(nodes.none(), nodes.none())]))
Example #6
0
 def dict_containing_reference_value_is_not_literal(self):
     assert not is_literal(nodes.dict_literal([(nodes.none(), nodes.ref("x"))]))
Example #7
0
 def empty_dict_is_literal(self):
     assert is_literal(nodes.dict_literal([]))
Example #8
0
def empty_dict_has_key_and_value_type_of_bottom():
    assert_equal(
        types.dict_type(types.bottom_type, types.bottom_type),
        infer(nodes.dict_literal([]))
    )
Example #9
0
def dict_literal_type_hint_is_ignored_if_hint_type_is_not_dict():
    assert_equal(
        types.dict_type(types.bottom_type, types.bottom_type),
        infer(nodes.dict_literal([]), hint=types.int_type)
    )