@classmethod
    def ensure_value_type(cls, value_type, parameter=None):
        """Raises a :exc:`TypeError` if the given ``value_type`` is not
        an instance of nor a subclass of the class.

        .. sourcecode:: pycon

           >>> Integer.ensure_value_type(Bulk
           ... )  # doctest: +NORMALIZE_WHITESPACE
           Traceback (most recent call last):
             ...
           TypeError: expected a subtype of sider.types.Integer,
                      but sider.types.Bulk was passed
           >>> Integer.ensure_value_type(UnicodeString()
           ... )  # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
           Traceback (most recent call last):
             ...
           TypeError: expected an instance of sider.types.Integer,
                      but <sider.types.UnicodeString object at ...>
                      was passed
           >>> Bulk.ensure_value_type(1)
           Traceback (most recent call last):
             ...
           TypeError: expected a type, not 1

        Otherwise it simply returns an instance of
        the given ``value_type``.

        .. sourcecode:: pycon

           >>> Bulk.ensure_value_type(Bulk)  # doctest: +ELLIPSIS
           <sider.types.Bulk object at ...>
           >>> Bulk.ensure_value_type(ByteString)  # doctest: +ELLIPSIS
           <sider.types.ByteString object at ...>
           >>> ByteString.ensure_value_type(ByteString
           ... )  # doctest: +ELLIPSIS
           <sider.types.ByteString object at ...>
           >>> bytestr = ByteString()
           >>> ByteString.ensure_value_type(bytestr) # doctest: +ELLIPSIS
           <sider.types.ByteString object at ...>

        If an optional ``parameter`` name has present, the error message
        becomes better.

        .. sourcecode:: pycon

           >>> Integer.ensure_value_type(Bulk,
           ...   parameter='argname')  # doctest: +NORMALIZE_WHITESPACE
           Traceback (most recent call last):
             ...
           TypeError: argname must be a subtype of sider.types.Integer,
                      but sider.types.Bulk was passed
           >>> Integer.ensure_value_type(UnicodeString(),
           ...   parameter='argname'
           ... )  # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
           Traceback (most recent call last):
             ...
           TypeError: argname must be an instance of sider.types.Integer,
                      but <sider.types.UnicodeString object at ...>
                      was passed
           >>> Bulk.ensure_value_type(1, parameter='argname')
           Traceback (most recent call last):
             ...
           TypeError: argname must be a type, not 1

        :param value_type: a type expected to be a subtype of the class
        :type value_type: :class:`Value`, :class:`type`
        :param parameter: an optional parameter name.
                          if present the error message becomes better
        :type parameter: :class:`str`
        :raises exceptions.TypeError:
           if the given ``subtype`` is not a subclass of the class

        """
        typename = '.'.join((cls.__module__, cls.__name__))
        if isinstance(value_type, type):
            subname = '.'.join((value_type.__module__, value_type.__name__))
            if issubclass(value_type, cls):
                try:
                    return value_type()
                except __HOLE__ as e:
                    raise TypeError(
                        '{0} must implement the constructor which takes '
                        'no arguments; {1}'.format(subname, e)
                    )
            else:
                if parameter:
                    msg = '{0} must be a subtype of {1}, but {2} was passed'
                else:
                    msg = 'expected a subtype of {1}, but {2} was passed'
                raise TypeError(msg.format(parameter, typename, subname))
        elif isinstance(value_type, Value):
            if isinstance(value_type, cls):
                return value_type
            else:
                if parameter:
                    msg = '{0} must be an instance of {1}, ' \
                          'but {2!r} was passed'
                else:
                    msg = 'expected an instance of {1}, but {2!r} was passed'
                raise TypeError(msg.format(parameter, typename, value_type))
        else:
            if parameter:
                msg = '{0} must be a type, not {1!r}'
            else:
                msg = 'expected a type, not {1!r}'
            raise TypeError(msg.format(parameter, value_type))
dataset/ETHPy150Open dahlia/sider/sider/types.py/Value.ensure_value_type