def test_inference_invalid_slice(node): sub_node = astroid.Subscript() slice = astroid.Slice() slice.postinit(astroid.Const(0), astroid.Const('a')) sub_node.postinit(node, slice) module, _ = cs._parse_text(sub_node) for subscript_node in module.nodes_of_class(astroid.Subscript): assert isinstance(subscript_node.inf_type, TypeFail)
def subscript_node(draw, value=None, slice=index_node()): value = value or subscriptable_expr node = astroid.Subscript() node.postinit( draw(value), draw(slice) ) return node
def test_inference_dict_subscript(node): """Note that this test only takes in a dictionary because the subscript index must be the same type as the dictionary's keys in order to type check. """ for key, _ in node.items: new_node = astroid.Subscript() new_node.postinit(node, key) module, _ = cs._parse_text(new_node) for subscript_node in module.nodes_of_class(astroid.Subscript): dict_node = subscript_node.value assert subscript_node.inf_type.getValue() == list(dict_node.items)[0][1].inf_type.getValue()