Skip to content

mypy broken for numpy typing #2553

Open
@matthewfeickert

Description

@matthewfeickert

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 #2535 (comment)

Metadata

Metadata

Labels

bugSomething isn't workinghelp wantedExtra attention is needed / contributions welcomepre-commitRelated to pre-commit hookstype checkingRelated to types and type checking

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions