def __typecheck__(self, func, to_check): if not isinstance(to_check, Tree): raise _TC_TypeError(to_check, self._type) try: check_type(self._type, func, to_check.val) except _TC_Exception, e: raise _TC_TypeError(to_check.val, self._type.type)
def __typecheck__(self, func, to_check): if not isinstance(to_check, Tree): raise _TC_TypeError(to_check, self._type) try: typecheck.check_type(self._type, func, to_check.val) except _TC_Exception: raise _TC_TypeError(to_check.val, self._type.type) for side in ('right', 'left'): child = getattr(to_check, side) if child is not None: try: typecheck.check_type(self, func, child) except _TC_Exception, e: raise _TC_TreeChildTypeError(side, e)
def __typecheck__(self, func, to_check): try: length = len(to_check) except TypeError: raise _TC_TypeError(to_check, "something with a __len__ method") if length != self._length: raise _TC_LengthError(length, self._length)
def __typecheck__(self, func, to_check): if not isinstance(to_check, self.__cls): raise _TC_TypeError(to_check, self) for i, item in enumerate(to_check): try: check_type(self._type, func, item) except _TC_Exception, e: raise _TC_IterationError(i, item, e)
def __typecheck__(self, func, to_check): if not isinstance(to_check, set): raise _TC_TypeError(to_check, self.type) if len(self._types) == 0 and len(to_check) > 0: raise _TC_LengthError(len(to_check), 0) for obj in to_check: error = False for type in self._types: try: check_type(type, func, obj) except _TC_Exception: error = True continue else: error = False break if error: raise _TC_KeyError(obj, _TC_TypeError(obj, self._type))
def __typecheck__(self, func, to_check): if not isinstance(to_check, list): raise _TC_TypeError(to_check, self.type) # lists are patterned, meaning that [int, float] requires that the # to-be-checked list contain an alternating sequence of integers and # floats. The pattern must be completed (e.g, [5, 5.0, 6, 6.0] but not # [5, 5.0, 6]) for the list to typecheck successfully. if len(to_check) % len(self._types): raise _TC_LengthError(len(to_check)) pat_len = len(self._types) for i, val in enumerate(to_check): type = self._types[i % pat_len] try: check_type(type, func, val) except _TC_Exception, e: raise _TC_IndexError(i, e)
def __typecheck__(self, func, to_check): if to_check != self.type: raise _TC_TypeError(to_check, self.type)