I have not had time to test this entirely, but the following should be equivalent:

    dists[dists > threshold] = np.inf
    matched_keypoints2 = keypoints2[np.argmin(dists, axis=1)]
    matched_keypoint_pairs = np.concatenate((keypoints1, matched_keypoints2), ...)