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 __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 __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 __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 __iter__(localization, proxy_obj, arguments): self_object = get_self(proxy_obj) listiterator = iter(self_object) wrap = StandardWrapper(listiterator) set_contained_elements_type( wrap, get_contained_elements_type(get_self(proxy_obj))) return wrap
def __iter__(localization, proxy_obj, arguments): iter = StandardWrapper(get_self(proxy_obj).__iter__()) set_contained_elements_type( iter, get_contained_elements_type( TypeWrapper.get_wrapper_of(get_self(proxy_obj)))) return iter
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 append(localization, callable_, arguments): self_instance = StandardWrapper.get_wrapper_of(callable_.__self__) existing_type = get_contained_elements_type(self_instance) if existing_type is undefined_type.UndefinedType: new_type = arguments[0] else: new_type = union_type.UnionType.add(existing_type, arguments[0]) set_contained_elements_type(self_instance, new_type) return types.NoneType
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 itervalues(localization, proxy_obj, arguments): ret_type = StandardWrapper(get_self(proxy_obj).itervalues()) 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 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 popitem(localization, proxy_obj, arguments): 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) return container_type
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 deque(localization, proxy_obj, arguments): ret_type = wrap_contained_type(collections.deque()) 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 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 __setslice__(localization, proxy_obj, arguments): existing_type = get_contained_elements_type(get_self(proxy_obj)) params = arguments[2] if can_store_elements(params): new_type = existing_type other_type = get_contained_elements_type(params) if not isinstance(other_type, collections.Iterable): other_type = [other_type] for par in other_type: new_type = union_type.UnionType.add(new_type, par) else: new_type = union_type.UnionType.add(existing_type, arguments[2]) set_contained_elements_type(get_self(proxy_obj), new_type) return types.NoneType
def iteritems(localization, proxy_obj, arguments): ret_type = StandardWrapper(get_self(proxy_obj).iteritems()) 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 __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 extend(localization, proxy_obj, arguments): existing_type = get_contained_elements_type(get_self(proxy_obj)) params = arguments[0] if can_store_elements(params): new_type = existing_type other_type = get_contained_elements_type(params) if not isinstance(other_type, collections.Iterable): other_type = [other_type] for par in other_type: new_type = union_type.UnionType.add(new_type, par) else: return StypyTypeError.wrong_parameter_type_error( localization, "iterable", type(params).__name__) set_contained_elements_type(get_self(proxy_obj), new_type) return types.NoneType
def __iter__(localization, proxy_obj, arguments): ret_type = StandardWrapper(get_self(proxy_obj).__iter__()) key_list = get_key_types(get_self(proxy_obj)) stored_keys_type = None if isinstance(key_list, union_type.UnionType): key_list = key_list.types else: key_list = list(key_list) for key in key_list: stored_keys_type = union_type.UnionType.add(stored_keys_type, key) 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 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 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_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 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
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 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 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 __set_contained_elements_type(localization, container, elements): """ Modifies the types stored by a container, not dealing with union type indexes :param localization: :param container: :param elements: :return: """ Localization.set_current(localization) if is_error_type(container): return container try: if type(elements) is tuple: # Key, value data structures if type_containers.can_store_keypairs(container): return type_containers.set_contained_elements_type_for_key(container, elements[0], elements[1]) # Indexable data structures if type_containers.can_store_elements(container): if not type_group_generator.Integer == type(elements[0]): if not hasattr(container, '__index__'): # Ellipsis if not (type_inspection.compare_type(elements[0], slice) and (type_inspection.compare_type( elements[1], slice) or type_inspection.compare_type( elements[1], list))): return StypyTypeError(localization, "Indexes of indexable containers must be Integers or instances that " "implement the __index__ method") else: index_getter = getattr(container, '__index__') if index_getter is not None: if hasattr(index_getter, '__objclass__') and not index_getter.__objclass__.__name__ == "ndarray": # res = invoke(localization, container, '__index__') res = invoke(localization, index_getter) if not type_group_generator.Integer == type(res): return StypyTypeError(localization, "Indexes of indexable containers must be Integers or instances that " "implement the __index__ method") else: # Other classes that define __setitem__ m_set = type_containers.get_setitem_method(container) if m_set is not None: try: return m_set(localization, *elements) except TypeError: return m_set(*elements) if type_containers.is_slice(elements[0]): if type_containers.can_store_elements(elements[1]): return type_containers.set_contained_elements_type(container, type_containers.get_contained_elements_type( elements[1])) else: return type_containers.set_contained_elements_type(container, elements[1]) else: return type_containers.set_contained_elements_type(container, elements[1]) type_containers.set_contained_elements_type(container, elements) except Exception as ex: return StypyTypeError(localization, "Cannot store elements '{1}' into an object of type '{0}': {2}".format( type(container), str(elements), str(ex)) )