pyhf icon indicating copy to clipboard operation
pyhf copied to clipboard

mypy broken for numpy typing

Open matthewfeickert opened this issue 1 year ago • 0 comments
trafficstars

Looks like there's a slightly different type coercion needed here (taking one as an example0

src/pyhf/tensor/numpy_backend.py:38: error: Argument 2 to "poisson_logpdf" of "numpy_backend" has incompatible type

got:
"Union[ndarray[Any, dtype[number[T@__init__]]], ndarray[Any, dtype[bool]]]"

expected:
"Union[ndarray[Any, dtype[number[T@log_prob]]], ndarray[Any, dtype[bool]]]"

so need to dig into why. The @ notation is throwing me off a little (must be related to the generics). /cc @henryiii or @alexander-held if they've seen this before.

with this code

class _BasicPoisson:
   def __init__(self, rate: Tensor[T]):
       self.rate = rate

   def sample(self, sample_shape: Shape) -> ArrayLike:
       return poisson(self.rate).rvs(size=sample_shape + self.rate.shape)  # type: ignore[no-any-return]

   def log_prob(self, value: NDArray[np.number[T]]) -> ArrayLike:
       tensorlib: numpy_backend[T] = numpy_backend()
       return tensorlib.poisson_logpdf(value, self.rate)

here, it seems to complain about self.rate which is typed as Tensor[T].

Originally posted by @kratsg in https://github.com/scikit-hep/pyhf/issues/2535#issuecomment-2271715966

matthewfeickert avatar Nov 01 '24 08:11 matthewfeickert