def test_hypothesis_signature_parses(sig):
    hy_sig = nps._hypothesis_parse_gufunc_signature(sig, all_checks=False)
    try:
        np_sig = np.lib.function_base._parse_gufunc_signature(sig)
        assert np_sig == hy_sig_2_np_sig(hy_sig)
    except ValueError:
        assert "?" in sig
        # We can always fix this up, and it should then always validate.
        sig = sig.replace("?", "")
        hy_sig = nps._hypothesis_parse_gufunc_signature(sig, all_checks=False)
        np_sig = np.lib.function_base._parse_gufunc_signature(sig)
        assert np_sig == hy_sig_2_np_sig(hy_sig)
def gufunc_sig_to_einsum_sig(gufunc_sig):
    """E.g. (i,j),(j,k)->(i,k) becomes ...ij,...jk->...ik"""
    def einlabels(labels):
        assert "x" not in labels, "we reserve x for fixed-dimensions"
        return "..." + "".join(i if not i.isdigit() else "x" for i in labels)

    gufunc_sig = nps._hypothesis_parse_gufunc_signature(gufunc_sig)
    input_sig = ",".join(map(einlabels, gufunc_sig.input_shapes))
    return input_sig + "->" + einlabels(gufunc_sig.result_shape)
def test_numpy_signature_parses(sig):
    if sig == "(m?,n),(n,p?)->(m?,p?)":  # matmul example
        return

    np_sig = np.lib.function_base._parse_gufunc_signature(sig)
    try:
        hy_sig = nps._hypothesis_parse_gufunc_signature(sig, all_checks=False)
        assert np_sig == hy_sig_2_np_sig(hy_sig)
    except InvalidArgument:
        shape_too_long = any(len(s) > 32 for s in np_sig[0] + np_sig[1])
        multiple_outputs = len(np_sig[1]) > 1
        assert shape_too_long or multiple_outputs

        # Now, if we can fix this up does it validate?
        in_, out = sig.split("->")
        sig = in_ + "->" + out.split(",(")[0]
        np_sig = np.lib.function_base._parse_gufunc_signature(sig)
        if all(len(s) <= 32 for s in np_sig[0] + np_sig[1]):
            hy_sig = nps._hypothesis_parse_gufunc_signature(sig,
                                                            all_checks=False)
            assert np_sig == hy_sig_2_np_sig(hy_sig)
def test_frozen_dims_signature():
    nps._hypothesis_parse_gufunc_signature("(2),(3)->(4)")