Minor nit -- this should be a ValueError.  Also I think this should be repr, not str.