Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mypy broken for numpy typing #2553

Open
matthewfeickert opened this issue Nov 1, 2024 · 0 comments
Open

mypy broken for numpy typing #2553

matthewfeickert opened this issue Nov 1, 2024 · 0 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed / contributions welcome pre-commit Related to pre-commit hooks type checking Related to types and type checking

Comments

@matthewfeickert
Copy link
Member

matthewfeickert commented Nov 1, 2024

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)

@matthewfeickert matthewfeickert added bug Something isn't working help wanted Extra attention is needed / contributions welcome pre-commit Related to pre-commit hooks type checking Related to types and type checking labels Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed / contributions welcome pre-commit Related to pre-commit hooks type checking Related to types and type checking
Projects
None yet
Development

No branches or pull requests

2 participants