Skip to content

Commit

Permalink
fix: Add TYPE_CHECKING guard for numpy.typing (#2208)
Browse files Browse the repository at this point in the history
* The addition of typing.TYPE_CHECKING avoids situations in which
  the version of NumPy (whose lower bounds is enforced by SciPy)
  doesn't have numpy.typing, which was added in NumPy v1.21.0.
* Exclude lines that match 'if TYPE_CHECKING:' from the coverage
  report to avoid artificial drops in code coverage.
   - c.f. https://coverage.readthedocs.io/en/stable/excluding.html#advanced-exclusion

Co-authored-by: Giordon Stark <kratsg@gmail.com>
  • Loading branch information
matthewfeickert and kratsg authored May 18, 2023
1 parent 30dc756 commit 0efb2ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ omit = ["*/pyhf/typing.py"]
precision = 1
sort = "cover"
show_missing = true
exclude_also = [
"if TYPE_CHECKING:"
]

[tool.mypy]
files = "src"
Expand Down
10 changes: 8 additions & 2 deletions src/pyhf/tensor/numpy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
from __future__ import annotations

import logging
from typing import Callable, Generic, Mapping, Sequence, TypeVar, Union
from typing import TYPE_CHECKING, Callable, Generic, Mapping, Sequence, TypeVar, Union

import numpy as np
from numpy.typing import ArrayLike, DTypeLike, NBitBase, NDArray

# Needed while numpy lower bound is older than v1.21.0
if TYPE_CHECKING:
from numpy.typing import ArrayLike, DTypeLike, NBitBase, NDArray
else:
NBitBase = "NBitBase"

from scipy import special
from scipy.special import gammaln, xlogy
from scipy.stats import norm, poisson
Expand Down

0 comments on commit 0efb2ee

Please sign in to comment.