def geterrobj(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'list') set_contained_elements_type( ret_type, union_type.UnionType.add( get_builtin_python_type_instance(localization, 'int'), types.NoneType)) return ret_type
def test_tuple__setattr__(self): __temp_67 = self.type_store.get_type_of(Localization(__file__), "sample_tuple1") __temp_68 = self.type_store.get_type_of_member(Localization(__file__), __temp_67, "__setattr__") __temp_69 = invoke( Localization(__file__), __temp_68, stypy_interface.get_builtin_python_type_instance( None, 'str', 'foo'), stypy_interface.get_builtin_python_type_instance(None, 'int')) assert_if_not_error(__temp_69)
def fromkeys(localization, proxy_obj, arguments): if len(arguments) > 0: param1 = arguments[0] else: param1 = types.NoneType if len(arguments) > 1: param2 = arguments[1] else: param2 = types.NoneType ret = get_builtin_python_type_instance(localization, "dict") # There are several cases: # A dictionary: Return a copy # A dictionary and any other object: {<each dict key>: other object} if can_store_keypairs(param1): if param2 == types.NoneType: return param1 else: temp = param1 set_contained_elements_type(temp, get_builtin_python_type_instance(localization, "dict")) keys = get_key_types(param1) if isinstance(keys, union_type.UnionType): keys = keys.types else: keys = [keys] for key in keys: set_contained_elements_type_for_key(temp, key, param2) return temp else: # A list or a tuple: {each structure element type: None} # A list or a tuple and any other object: {<each dict key>: other object} t1 = get_contained_elements_type(param1) if isinstance(t1, union_type.UnionType): t1 = t1.types else: t1 = [t1] if param2 == types.NoneType: value = types.NoneType else: value = param2 for t in t1: set_contained_elements_type_for_key(ret, t, value) return ret
def list(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'list') if len(arguments) > 0: params = arguments[0] if is_str(type(params)): set_contained_elements_type( ret_type, get_builtin_python_type_instance(localization, type(params).__name__)) else: existing_type = get_contained_elements_type(params) if existing_type is not None: set_contained_elements_type(ret_type, existing_type) return ret_type
def __getslice__(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'list') set_contained_elements_type( ret_type, get_contained_elements_type(get_self(proxy_obj))) return ret_type
def __add__(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'list') existing_type = get_contained_elements_type(get_self(proxy_obj)) params = arguments[0] if can_store_elements(params): if existing_type is not None: if not isinstance(existing_type, union_type.UnionType): new_type = existing_type else: new_type = existing_type.duplicate() else: new_type = None other_type = get_contained_elements_type(params) if not isinstance(other_type, union_type.UnionType): other_type = [other_type] else: other_type = other_type.types for par in other_type: new_type = union_type.UnionType.add(new_type, par) else: new_type = union_type.UnionType.add(existing_type, arguments[0]) set_contained_elements_type(ret_type, new_type) return ret_type
def __create_dict(self, dict_name, list_keys, list_values): sample_dict = stypy_interface.get_builtin_python_type_instance(None, "dict") for i in range(len(list_keys)): add_key_and_value_type(sample_dict, list_keys[i], list_values[i]) self.type_store.set_type_of(Localization(__file__), dict_name, sample_dict)
def __getnewargs__(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, "tuple") # existing_type = get_contained_elements_type(get_self(proxy_obj)) set_contained_elements_type( ret_type, TypeWrapper.get_wrapper_of(get_self(proxy_obj))) return ret_type
def __mul__(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, "tuple") existing_type = get_contained_elements_type( TypeWrapper.get_wrapper_of(get_self(proxy_obj))) new_type = existing_type set_contained_elements_type(ret_type, new_type) return ret_type
def __mul__(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'list') existing_type = get_contained_elements_type(get_self(proxy_obj)) new_type = existing_type.clone() set_contained_elements_type(ret_type, new_type) return ret_type
def items(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'list') key_types = get_contained_elements_type( TypeModifiers.iterkeys(localization, proxy_obj, arguments)) value_types = get_contained_elements_type( TypeModifiers.itervalues(localization, proxy_obj, arguments)) container_type = get_builtin_python_type_instance( localization, "tuple") union = union_type.UnionType.add(key_types, value_types) set_contained_elements_type(container_type, union) set_contained_elements_type(ret_type, container_type) return ret_type
def values(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'list') stored_values_type = get_value_types(get_self(proxy_obj)) if stored_values_type is not None: set_contained_elements_type(ret_type, stored_values_type) else: set_contained_elements_type(ret_type, UndefinedType) return ret_type
def test_tuple_creation(self): tuple_ = self.type_store.get_type_of(Localization(__file__), "sample_tuple1") getitem = self.type_store.get_type_of_member(self.loc, tuple_, "__getitem__") items = invoke( self.loc, getitem, stypy_interface.get_builtin_python_type_instance(None, 'int')) compare_types(tuple_, tuple) compare_types(items, [int(), str()])
def __create_tuple(self, type_store, name, types): tuple_ = stypy_interface.get_builtin_python_type(self.loc, "tuple") list_ = stypy_interface.get_builtin_python_type_instance( self.loc, "list") append_method = self.type_store.get_type_of_member( self.loc, list_, "append") for type in types: stypy_interface.invoke(self.loc, append_method, type) tuple_ = invoke(self.loc, tuple_, list_) type_store.set_type_of(self.loc, name, tuple_)
def __getitem__(localization, proxy_obj, arguments): if compare_type(arguments[0], slice): ret_type = get_builtin_python_type_instance(localization, "tuple") set_contained_elements_type( ret_type, get_contained_elements_type( TypeWrapper.get_wrapper_of(get_self(proxy_obj)))) return ret_type return get_contained_elements_type( TypeWrapper.get_wrapper_of(get_self(proxy_obj)))
def test_right_call_builtin_module_class_method(self): type_ = get_builtin_python_type_instance(self.localization, "list") compare_types(type_, list) method = self.context.get_type_of_member(self.localization, type_, "append") compare_types(type(method), types.BuiltinFunctionType) ret = invoke(self.localization, method, int()) compare_types(type(ret), types.NoneType) method = self.context.get_type_of_member(self.localization, type_, "__getitem__") ret = invoke(self.localization, method, int()) compare_types(type(ret), int)
def tuple(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'tuple') if len(arguments) > 0: params = arguments[0] if is_str(type(params)): set_contained_elements_type( ret_type, get_builtin_python_type_instance(localization, type(params).__name__)) return ret_type existing_type = get_contained_elements_type(params) if isinstance(existing_type, union_type.UnionType): types_ = existing_type.types ordered = None for type_ in types_: ordered = union_type.UnionType.add(ordered, type_) set_contained_elements_type(ret_type, ordered) else: set_contained_elements_type(ret_type, existing_type) # ret_type.known_elements = True return ret_type
def test_tuple__getattribute__(self): __temp_67 = self.type_store.get_type_of(Localization(__file__), "sample_tuple1") __temp_68 = self.type_store.get_type_of_member(Localization(__file__), __temp_67, "__getattribute__") __temp_69 = invoke( Localization(__file__), __temp_68, stypy_interface.get_builtin_python_type_instance(None, 'str')) self.assertTrue(len(dir(tuple)) == len(__temp_69.types)) self.__obtain_tuple_item("sample_tuple1", "x") compare_types(self.type_store.get_type_of(Localization(__file__), "x"), [int(), str()])
def __getitem__(localization, callable_, arguments): self_obj = callable_.__self__ get_item = getattr(self_obj, "__getitem__") def invoke_getitem(): try: return get_item(0) except IndexError as ie: return undefined_type.UndefinedType if compare_type(arguments[0], slice): ret_type = get_builtin_python_type_instance(localization, 'list') set_contained_elements_type(ret_type, invoke_getitem()) return ret_type return invoke_getitem()
def dict(localization, proxy_obj, arguments): ret = get_builtin_python_type_instance(localization, 'dict') if len(arguments) == 0: return ret if can_store_keypairs(arguments[0]): if compare_type(arguments[0], types.DictProxyType): if isinstance(arguments[0], StandardWrapper): contents = arguments[0].get_wrapped_type() else: contents = arguments[0] ret = dict(contents) ret = StandardWrapper(ret) return ret return arguments[0] else: contents = get_contained_elements_type(arguments[0]) if not compare_type(contents, tuple): return StypyTypeError.object_must_be_type_error( localization, 'Iterable argument to build a dictionary', '(key,value) tuple') else: keys = get_contained_elements_type(contents) values = keys if isinstance(keys, union_type.UnionType): keys = keys.types else: keys = [keys] if len(keys) == 2: # Special case in which we use a two-element tuple to initialize a dict. We assume that the first # element is the key and the second the value set_contained_elements_type_for_key(ret, keys[0], keys[1]) else: for key in keys: set_contained_elements_type_for_key(ret, key, values) return ret
def keys(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'list') if len(get_self(proxy_obj)) == 0: return ret_type key_list = get_key_types(get_self(proxy_obj)) if isinstance(key_list, union_type.UnionType): key_list = key_list.types else: key_list = list(key_list) stored_keys_type = None for value in key_list: stored_keys_type = union_type.UnionType.add( stored_keys_type, value) if stored_keys_type is not None: set_contained_elements_type(ret_type, stored_keys_type) else: set_contained_elements_type(ret_type, UndefinedType) return ret_type
def test_dict_fromkeys(self): # x = l[3] #int or str __temp_1 = self.type_store.get_type_of(Localization(__file__), "sample_dict1") # Test objects self.__create_dict("other_dict", [bool], [tuple]) other_list = stypy_interface.get_builtin_python_type_instance(None, "list") union = UnionType.add(str, float) set_contained_elements_type(other_list, union) self.type_store.set_type_of(Localization(__file__), "other_list", other_list) other_object = int() self.type_store.set_type_of(Localization(__file__), "other_object", other_object) iter_call = self.type_store.get_type_of_member(Localization(__file__), other_list, "__iter__") iterator = invoke(Localization(__file__), iter_call) self.type_store.set_type_of(Localization(__file__), "iterator", iterator) # There are several cases: # A dictionary: Return a copy param = self.type_store.get_type_of(Localization(__file__), "other_dict") __temp_2 = self.type_store.get_type_of_member(Localization(__file__), __temp_1, "fromkeys") __temp_3 = invoke(Localization(__file__), __temp_2, param) self.type_store.set_type_of(Localization(__file__), "item", __temp_3) compare_types(self.type_store.get_type_of(Localization(__file__), "item"), dict) getitem_call = self.type_store.get_type_of_member(Localization(__file__), __temp_3, "__getitem__") ret = invoke(Localization(__file__), getitem_call, bool) compare_types(ret, tuple) # A dictionary and any other object: {<each dict key>: other object} param = self.type_store.get_type_of(Localization(__file__), "other_dict") param2 = self.type_store.get_type_of(Localization(__file__), "other_object") __temp_2 = self.type_store.get_type_of_member(Localization(__file__), __temp_1, "fromkeys") __temp_3 = invoke(Localization(__file__), __temp_2, param, param2) self.type_store.set_type_of(Localization(__file__), "item", __temp_3) compare_types(self.type_store.get_type_of(Localization(__file__), "item"), dict) getitem_call = self.type_store.get_type_of_member(Localization(__file__), __temp_3, "__getitem__") ret = invoke(Localization(__file__), getitem_call, bool) compare_types(ret, [tuple, int()]) # A list or a tuple: {each structure element type: None} param = self.type_store.get_type_of(Localization(__file__), "other_list") __temp_2 = self.type_store.get_type_of_member(Localization(__file__), __temp_1, "fromkeys") __temp_3 = invoke(Localization(__file__), __temp_2, param, types.NoneType) self.type_store.set_type_of(Localization(__file__), "item", __temp_3) compare_types(self.type_store.get_type_of(Localization(__file__), "item"), dict) getitem_call = self.type_store.get_type_of_member(Localization(__file__), __temp_3, "__getitem__") ret = invoke(Localization(__file__), getitem_call, str) compare_types(ret, None) ret = invoke(Localization(__file__), getitem_call, float) compare_types(ret, None) # A list or a tuple and any other object: {<each dict key>: other object} param = self.type_store.get_type_of(Localization(__file__), "other_list") param2 = self.type_store.get_type_of(Localization(__file__), "other_object") __temp_2 = self.type_store.get_type_of_member(Localization(__file__), __temp_1, "fromkeys") __temp_3 = invoke(Localization(__file__), __temp_2, param, param2) self.type_store.set_type_of(Localization(__file__), "item", __temp_3) compare_types(self.type_store.get_type_of(Localization(__file__), "item"), dict) getitem_call = self.type_store.get_type_of_member(Localization(__file__), __temp_3, "__getitem__") ret = invoke(Localization(__file__), getitem_call, str) compare_types(ret, self.type_store.get_type_of(Localization(__file__), "other_object")) ret = invoke(Localization(__file__), getitem_call, float) compare_types(ret, self.type_store.get_type_of(Localization(__file__), "other_object")) # Any __iter__ object: {each structure element type: None} param = self.type_store.get_type_of(Localization(__file__), "iterator") __temp_2 = self.type_store.get_type_of_member(Localization(__file__), __temp_1, "fromkeys") __temp_3 = invoke(Localization(__file__), __temp_2, param, types.NoneType) self.type_store.set_type_of(Localization(__file__), "item", __temp_3) compare_types(self.type_store.get_type_of(Localization(__file__), "item"), dict) getitem_call = self.type_store.get_type_of_member(Localization(__file__), __temp_3, "__getitem__") ret = invoke(Localization(__file__), getitem_call, str) compare_types(ret, None) ret = invoke(Localization(__file__), getitem_call, float) compare_types(ret, None) # Any __iter__ object and any other object: {each structure element type: other object} param = self.type_store.get_type_of(Localization(__file__), "iterator") param2 = self.type_store.get_type_of(Localization(__file__), "other_object") __temp_2 = self.type_store.get_type_of_member(Localization(__file__), __temp_1, "fromkeys") __temp_3 = invoke(Localization(__file__), __temp_2, param, param2) self.type_store.set_type_of(Localization(__file__), "item", __temp_3) compare_types(self.type_store.get_type_of(Localization(__file__), "item"), dict) getitem_call = self.type_store.get_type_of_member(Localization(__file__), __temp_3, "__getitem__") ret = invoke(Localization(__file__), getitem_call, str) compare_types(ret, self.type_store.get_type_of(Localization(__file__), "other_object")) ret = invoke(Localization(__file__), getitem_call, float) compare_types(ret, self.type_store.get_type_of(Localization(__file__), "other_object"))
def test_dict_creation(self): compare_types(type(self.type_store.get_type_of(Localization(__file__), "sample_dict1")), type(stypy_interface.get_builtin_python_type_instance(None, "dict")))
def rpartition(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'tuple') set_contained_elements_type( ret_type, get_builtin_python_type_instance(localization, 'str')) return ret_type
def _formatter_field_name_split(localization, proxy_obj, arguments): result = "test"._formatter_field_name_split() ret_type = get_builtin_python_type_instance(localization, "tuple") set_contained_elements_type(ret_type, result[1]) return ret_type
def defaultdict(localization, proxy_obj, arguments): ret = StandardWrapper(defaultdict()) ret.stypy_default_value = types.NoneType if len(arguments) == 0: return ret if len(arguments) == 2: if hasattr(arguments[1], 'wrapped_type') and type( arguments[1].wrapped_type) is defaultdict: d = arguments[1] else: d = StandardWrapper(dict()) set_contained_elements_type_for_key( d, get_contained_elements_type(arguments[1]), get_contained_elements_type(arguments[1])) return StandardWrapper(defaultdict(arguments[0], d)) else: if can_store_keypairs(arguments[0]): if compare_type(arguments[0], types.DictProxyType): if isinstance(arguments[0], StandardWrapper): contents = arguments[0].get_wrapped_type() else: contents = arguments[0] ret = defaultdict(contents) ret.stypy_default_value = contents ret = StandardWrapper(ret) return ret return arguments[0] else: if can_store_elements(arguments[0]): contents = get_contained_elements_type(arguments[0]) if not compare_type(contents, tuple): return StypyTypeError.object_must_be_type_error( localization, 'Iterable argument to build a dictionary', '(key,value) tuple') else: keys = get_contained_elements_type(contents) values = keys if isinstance(keys, union_type.UnionType): keys = keys.types else: keys = [keys] for key in keys: set_contained_elements_type_for_key( ret, key, values) return ret else: ret.setdefault(arguments[0]) if arguments[0] is types.NoneType: ret.stypy_default_value = arguments[0] else: if hasattr(arguments[0], '__name__'): ret.stypy_default_value = get_builtin_python_type_instance( localization, arguments[0].__name__) else: return StypyTypeError( localization, "Invalid first parameter of defaultdict constructor: it must be a type or None" ) return ret
def readlines(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'list') set_contained_elements_type(ret_type, str()) return ret_type
def stypy____str__(localization, proxy_obj, arguments): return get_builtin_python_type_instance(localization, "str")
def split(localization, proxy_obj, arguments): ret_type = get_builtin_python_type_instance(localization, 'list') set_contained_elements_type( ret_type, get_builtin_python_type_instance(localization, 'str')) return ret_type