def test_set_conversions(T1, T2): set_conversion(T1, T2, lambda x: T2(x.data)) a, b = T1(1), T2(1) assert a != b assert convert(a, T2) == b with pytest.raises(TypeError): convert(b, T1)
def test_custom_promotions(T1, T2): a, b = T1(1), T2(2) # Prepare conversions set_conversion(T2, T1, lambda x: T1(x.data)) set_promotion_rule(T1, T2, T1) @set_promotion(T1, int, restype=int) @set_promotion(T2, int, restype=int) def promote_to_int(x, y): return convert(x.data, int), y # Create examples and test assert_allsame(promote(a, b), (a, T1(2))) assert_allsame(promote(a, 2), (1, 2)) assert_allsame(promote(a, b, 3), (1, 2, 3)) assert_allsame(promote(3, b, a), (3, 2, 1))
# Fast-track common cases u_dtype = u.dtype v_dtype = v.dtype if u_dtype is float and v_dtype is int: return u, v.convert(float) elif u_dtype is int and v_dtype is float: return u.convert(float), v zipped = [promote(x, y) for (x, y) in zip(u, v)] u = Vec(*[x for (x, y) in zipped]) v = Vec(*[y for (x, y) in zipped]) return u, v # Conversions and promotions between vec types and tuples/lists set_conversion(Vec, tuple, tuple) set_conversion(Vec, list, list) for T in [Vec, mVec]: set_conversion(tuple, T, T) set_conversion(list, T, T) @set_promotion(Vec, tuple, symmetric=True, restype=Vec) @set_promotion(Vec, list, symmetric=True, restype=Vec) def promote(u, v): return u, u.__origin__.from_seq(v) def asvector_overload(op, tt): real_op = getattr(operator, op.__name__)