hypothesis icon indicating copy to clipboard operation
hypothesis copied to clipboard

hypothesis.extra.numpy imports not added to ghostwritten tests

Open KRRT7 opened this issue 9 months ago • 1 comments

import numpy as np

def numpy_matmul(A: np.ndarray, B: np.ndarray) -> np.ndarray:
    rows_A, cols_A = A.shape
    rows_B, cols_B = B.shape
    if cols_A != rows_B:
        raise ValueError("Incompatible matrices")
    result = np.zeros((rows_A, cols_B))
    for i in range(rows_A):
        for j in range(cols_B):
            for k in range(cols_A):
                result[i, j] += A[i, k] * B[k, j]
    return result
hypothesis write reproducer.numpy_matmul
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import reproducer
from hypothesis import given, strategies as st

numpy_matmul_operands = arrays(dtype=scalar_dtypes(), shape=array_shapes(max_dims=2))


@given(a=numpy_matmul_operands, b=numpy_matmul_operands, c=numpy_matmul_operands)
def test_associative_binary_operation_numpy_matmul(a, b, c) -> None:
    left = reproducer.numpy_matmul(A=a, B=reproducer.numpy_matmul(A=b, B=c))
    right = reproducer.numpy_matmul(A=reproducer.numpy_matmul(A=a, B=b), B=c)
    assert left == right, (left, right)


@given(a=numpy_matmul_operands, b=numpy_matmul_operands)
def test_commutative_binary_operation_numpy_matmul(a, b) -> None:
    left = reproducer.numpy_matmul(A=a, B=b)
    right = reproducer.numpy_matmul(A=b, B=a)
    assert left == right, (left, right)


@given(a=numpy_matmul_operands)
def test_identity_binary_operation_numpy_matmul(a) -> None:
    identity = array([False])
    assert a == reproducer.numpy_matmul(A=a, B=identity)
    assert a == reproducer.numpy_matmul(A=identity, B=a)

KRRT7 avatar Mar 15 '25 06:03 KRRT7

import numpy as np

def dot_product(a: np.ndarray, b: np.ndarray) -> float:
    if len(a) != len(b):
        raise ValueError("Vectors must have the same length")

    result = 0
    for i in range(len(a)):
        result += a[i] * b[i]
    return result

similarly

hypothesis write reproducer.dot_product
# This test code was written by the `hypothesis.extra.ghostwriter` module
# and is provided under the Creative Commons Zero public domain dedication.

import numpy
import reproducer
from hypothesis import given, strategies as st

dot_product_operands = arrays(dtype=scalar_dtypes(), shape=array_shapes(max_dims=2))


@given(a=dot_product_operands, b=dot_product_operands, c=dot_product_operands)
def test_associative_binary_operation_dot_product(
    a: numpy.ndarray, b: numpy.ndarray, c
) -> None:
    left = reproducer.dot_product(a=a, b=reproducer.dot_product(a=b, b=c))
    right = reproducer.dot_product(a=reproducer.dot_product(a=a, b=b), b=c)
    assert left == right, (left, right)


@given(a=dot_product_operands, b=dot_product_operands)
def test_commutative_binary_operation_dot_product(
    a: numpy.ndarray, b: numpy.ndarray
) -> None:
    left = reproducer.dot_product(a=a, b=b)
    right = reproducer.dot_product(a=b, b=a)
    assert left == right, (left, right)


@given(a=dot_product_operands)
def test_identity_binary_operation_dot_product(a: numpy.ndarray) -> None:
    identity = array([False])
    assert a == reproducer.dot_product(a=a, b=identity)
    assert a == reproducer.dot_product(a=identity, b=a)

this is missing the same imports, plus the array in the last test.

KRRT7 avatar Mar 15 '25 07:03 KRRT7

This was also fixed by https://github.com/HypothesisWorks/hypothesis/pull/4578 🙂

Zac-HD avatar Nov 03 '25 03:11 Zac-HD