Skip to content

Commit 9842e82

Browse files
authored
docs: Add typehint aliases (#1969)
* Use Sphinx autodoc typehint aliases to improve readability of documentation. - c.f. https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_type_aliases
1 parent 62aad71 commit 9842e82

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

docs/conf.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,36 @@ def setup(app):
141141
'tensorflow_probability',
142142
]
143143

144+
145+
_type_aliases_inverted = {
146+
'pyhf.typing': [
147+
'PathOrStr',
148+
'ParameterBase',
149+
'Parameter',
150+
'Measurement',
151+
'ModifierBase',
152+
'NormSys',
153+
'NormFactor',
154+
'HistoSys',
155+
'StatError',
156+
'ShapeSys',
157+
'ShapeFactor',
158+
'LumiSys',
159+
'Modifier',
160+
'Sample',
161+
'Channel',
162+
'Observation',
163+
'Workspace',
164+
'Literal',
165+
],
166+
'numpy.typing': ['ArrayLike', 'DTypeLike', 'NBitBase', 'NDArray'],
167+
}
168+
autodoc_type_aliases = {
169+
item: f'{k}.{item}' for k, v in _type_aliases_inverted.items() for item in v
170+
}
171+
172+
autodoc_typehints_format = 'fully-qualified'
173+
144174
# List of patterns, relative to source directory, that match files and
145175
# directories to ignore when looking for source files.
146176
# This patterns also effect to html_static_path and html_extra_path

src/pyhf/tensor/numpy_backend.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
T = TypeVar("T", bound=NBitBase)
1616

1717
Tensor = Union["NDArray[np.number[T]]", "NDArray[np.bool_]"]
18-
18+
FloatIntOrBool = Literal["float", "int", "bool"]
1919
log = logging.getLogger(__name__)
2020

2121

@@ -53,7 +53,7 @@ def __init__(self, **kwargs: dict[str, str]):
5353
self.name = 'numpy'
5454
self.precision = kwargs.get('precision', '64b')
5555
self.dtypemap: Mapping[
56-
Literal['float', 'int', 'bool'],
56+
FloatIntOrBool,
5757
DTypeLike, # Type[np.floating[T]] | Type[np.integer[T]] | Type[np.bool_],
5858
] = {
5959
'float': np.float64 if self.precision == '64b' else np.float32,
@@ -206,7 +206,7 @@ def isfinite(self, tensor: Tensor[T]) -> NDArray[np.bool_]:
206206
return np.isfinite(tensor)
207207

208208
def astensor(
209-
self, tensor_in: ArrayLike, dtype: Literal['float'] = 'float'
209+
self, tensor_in: ArrayLike, dtype: FloatIntOrBool = 'float'
210210
) -> ArrayLike:
211211
"""
212212
Convert to a NumPy array.
@@ -247,9 +247,7 @@ def product(self, tensor_in: Tensor[T], axis: Shape | None = None) -> ArrayLike:
247247
def abs(self, tensor: Tensor[T]) -> ArrayLike:
248248
return np.abs(tensor)
249249

250-
def ones(
251-
self, shape: Shape, dtype: Literal["float", "int", "bool"] = "float"
252-
) -> ArrayLike:
250+
def ones(self, shape: Shape, dtype: FloatIntOrBool = "float") -> ArrayLike:
253251
try:
254252
dtype_obj = self.dtypemap[dtype]
255253
except KeyError:
@@ -261,9 +259,7 @@ def ones(
261259

262260
return np.ones(shape, dtype=dtype_obj)
263261

264-
def zeros(
265-
self, shape: Shape, dtype: Literal["float", "int", "bool"] = "float"
266-
) -> ArrayLike:
262+
def zeros(self, shape: Shape, dtype: FloatIntOrBool = "float") -> ArrayLike:
267263
try:
268264
dtype_obj = self.dtypemap[dtype]
269265
except KeyError:

0 commit comments

Comments
 (0)