def update(localization, proxy_obj, arguments): ret = get_self(proxy_obj) param = arguments[0] if can_store_keypairs(param): keys = get_key_types(param) if isinstance(keys, union_type.UnionType): keys = keys.types else: keys = list(keys) for key in keys: value = get_contained_elements_type_for_key(param, key) set_contained_elements_type_for_key(ret, key, value) else: if param.can_store_elements(): contents = get_contained_elements_type(param) if isinstance(contents, tuple): keys = get_contained_elements_type(contents) values = get_contained_elements_type(contents) for key in keys: set_contained_elements_type_for_key(ret, key, values) else: return StypyTypeError.invalid_length_error( localization, "Dictionary 'update' sequence", 1, 2) else: return StypyTypeError.object_must_be_type_error( localization, "The 'update' method second parameter", "dict or an iterable object") return ret
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 sinc(localization, proxy_obj, arguments): if Number == type(arguments[0]): return numpy.float64() if Number == type(get_contained_elements_type(arguments[0])): return call_utilities.create_numpy_array( get_contained_elements_type(arguments[0])) return arguments[0]
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 __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 fromarrays(localization, proxy, arguments): dvar = call_utilities.parse_varargs_and_kwargs( localization, arguments, [ 'dtype', 'shape', 'formats', 'names', 'titles', 'aligned', 'byteorder' ], { 'dtype': type, 'shape': AnyType, 'formats': AnyType, 'names': [Str, IterableDataStructureWithTypedElements(Str)], 'titles': [Str, IterableDataStructureWithTypedElements(Str)], 'aligned': bool, 'byteorder': AnyType }, 'fromarrays') if isinstance(dvar, StypyTypeError): return dvar arr = arguments[0] contained = get_contained_elements_type(arr) if isinstance(contained, union_type.UnionType): contained = contained.types try: return numpy.core.records.fromarrays(contained, dvar) except Exception as ex: return DynamicType
def will_iterate_loop(localization, condition_type): """ A loop must iterate an iterable object or data structure or an string. This function checks if the iterable object is empty (its contents are of the type UndefinedType). In that case, it does not iterate :param localization: Caller information :param condition_type: Type of the condition :return: """ if is_error_type(condition_type): return False if type(condition_type) is file: return True if condition_type is undefined_type.UndefinedType: return False if (can_store_elements(condition_type) or ( can_represent_type(IterableObject, condition_type)) or call_utilities.is_iterable(condition_type)): try: Localization.set_current(localization) t = get_contained_elements_type(condition_type) if type(t) is undefined_type.UndefinedType or t is undefined_type.UndefinedType: return False except: pass return True return True
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 __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, '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 __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 nan_to_num(localization, proxy_obj, arguments): if RealNumber == type(arguments[0]): return call_utilities.cast_to_numpy_type(arguments[0]) if can_store_elements(arguments[0]): return call_utilities.create_numpy_array( get_contained_elements_type(arguments[0])) return call_utilities.create_numpy_array(arguments[0].wrapped_type)
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, "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 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 i0(localization, proxy_obj, arguments): if call_utilities.is_numpy_array(arguments[0]): return arguments[0] else: if Number == type(arguments[0]): return call_utilities.create_numpy_array(arguments[0]) else: return call_utilities.create_numpy_array( get_contained_elements_type(arguments[0]))
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 get_type_of_for_loop_variable(localization, condition_type): """ A loop must iterate an iterable object or data structure or an string. This function returns the contents of whatever the loop is iterating :param localization: Caller information :param condition_type: Type of the condition :return: """ if type(condition_type) is RecursionType: return condition_type if type(condition_type) is StypyTypeError: return condition_type if type(condition_type) is types.FileType: return str() if condition_type is undefined_type.UndefinedType: return condition_type if can_store_keypairs(condition_type): return wrap_contained_type(get_key_types(condition_type)) # If the type of the condition can store elements, return the type of stored elements if can_store_elements(condition_type): Localization.set_current(localization) return wrap_contained_type(get_contained_elements_type(condition_type)) # If the type of the condition is some kind of string, return the type of string if can_represent_type(Str, condition_type): return wrap_contained_type(condition_type) # If the type of the condition is something iterable, return the result of calling its __iter__ method if can_represent_type(IterableObject, condition_type): iter_method = condition_type.get_type_of_member(localization, "__iter__") return wrap_contained_type(stypy_interface.invoke(localization, iter_method)) if call_utilities.is_numpy_array(condition_type): return condition_type.get_contained_type() if call_utilities.is_ndenumerate(condition_type): contained = None for typ in condition_type.iter.coords: contained = union_type.UnionType.add(contained, typ) t = wrap_contained_type((contained,)) t.set_contained_type(contained) return union_type.UnionType.add(t, numpy.int32) if call_utilities.is_ndindex(condition_type): t = wrap_contained_type((numpy.int32(),)) t.set_contained_type(numpy.int32()) return t return StypyTypeError(localization, "Invalid iterable type for a loop target ({0})".format(str(condition_type)))
def get_contained_elements_type(localization, container, multi_assign_arity=-1, multi_assign_index=-1): """ Gets the type stored in a certain container :param localization: :param container: :return: """ if is_error_type(container): return container return type_containers.get_contained_elements_type(container, multi_assign_arity, multi_assign_index)
def gradient(localization, proxy_obj, arguments): dvar = call_utilities.parse_varargs_and_kwargs( localization, arguments, ['vargargs', 'edge_order'], { 'varargs': IterableDataStructureWithTypedElements(RealNumber), 'edge_order': Integer, }, 'gradient') if isinstance(dvar, StypyTypeError): return dvar return call_utilities.create_numpy_array( get_contained_elements_type(arguments[0]))
def diff(localization, proxy_obj, arguments): dvar = call_utilities.parse_varargs_and_kwargs(localization, arguments, ['n', 'axis'], { 'n': Integer, 'axis': Integer, }, 'diff') if isinstance(dvar, StypyTypeError): return dvar return call_utilities.create_numpy_array( get_contained_elements_type(arguments[0]))
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 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 __repr__(self): """ str operator overload :return: """ txt = type(self.wrapped_type).__name__ if can_store_keypairs(self): txt += "{" keys = get_key_types(self) if is_union_type(keys): keys = keys.get_types() else: keys = list(keys) if len(keys) == 0: txt += "UndefinedType" else: for key in keys: values = get_contained_elements_type_for_key(self, key) if not isinstance(values, TypeWrapper): contents = type(values).__name__ else: contents = str(values) txt += type(key).__name__ + ": " + contents + "; " txt = txt[:-2] return txt + "}\n" if can_store_elements(self): contained_type = get_contained_elements_type(self) if not isinstance(contained_type, TypeWrapper): if is_undefined(contained_type): contents = "UndefinedType" else: contents = type(contained_type).__name__ else: contents = str(contained_type) return txt + "[" + contents + "]" return txt
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 __call__(self, localization, *call_args, **call_kwargs): contained_elements = get_contained_elements_type(self.type_) if isinstance(contained_elements, union_type.UnionType): types_to_examine = contained_elements.types else: types_to_examine = [contained_elements] right_types = [] wrong_types = [] for type_ in types_to_examine: match_found = False for declared_contained_type in self.content_types: if declared_contained_type == IterableDataStructureWithTypedElements.__get_type_of( type_): if isinstance(declared_contained_type, DependentType): declared_contained_type.set_type(type_) if hasattr(declared_contained_type, 'member'): declared_contained_type.member_obj = getattr( type_, declared_contained_type.member) if declared_contained_type.call_arity == 0: correct, return_type = declared_contained_type( localization) else: correct, return_type = declared_contained_type( localization, type_) if correct: match_found = True if type_ not in right_types: right_types.append(type_) if type_ in wrong_types: wrong_types.remove(type_) else: if type_ not in wrong_types and type_ not in right_types: wrong_types.append(type_) else: match_found = True right_types.append(type_) if not match_found: if type_ not in wrong_types and type_ not in right_types: wrong_types.append(type_) if self.report_errors: # All types are wrong if len(right_types) == 0: if len(wrong_types) > 0: StypyTypeError( localization, "None of the iterable contained types: {0} match the expected ones {1}" .format(str(types_to_examine), str(self.content_types))) else: if len(wrong_types) > 0: TypeWarning( localization, "Some of the iterable contained types: {0} do not match the expected ones {1}" .format(str(wrong_types), str(self.content_types))) else: if len(right_types) == 0 and len(wrong_types) > 0: TypeWarning( localization, "Some of the iterable contained types: {0} do not match the expected ones {1}" .format(str(wrong_types), str(self.content_types))) if len(right_types) > 0: return True, None else: return False, wrong_types
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 get_elements_type(obj): return get_contained_elements_type(obj)