You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
isso/isso/utils/types.py

25 lines
702 B

# -*- encoding: utf-8 -*-
def _TypeError(expected, val):
if isinstance(expected, (list, tuple)):
expected = ", ".join(ex.__name__ for ex in expected)
else:
expected = expected.__name__
return TypeError("Expected {0}, not {1}".format(
expected, val.__class__.__name__))
def require(val, expected):
"""Assure that :param val: is an instance of :param expected: or raise a
:exception TypeError: indicating what's expected.
>>> require(23, int)
>>> require(None, bool)
Traceback (most recent call last):
...
TypeError: Expected bool, not NoneType
"""
if not isinstance(val, expected):
raise _TypeError(expected, val)