def NoneSafeType(_type):
    """
    A hack for a "None-safe" typecaster. Given a type, it casts all
    values to that type as the type would, except that None is always
    cast to None.
    """
    def caster(value):
        if value is None:
            return None
        else:
            return _type(value)
    return caster