Skip to content

Commit 3c1d4ef

Browse files
authored
Merge pull request #76 from maresb/fix-doctests-without-numba
Fix doctests when Numba is disabled
2 parents 42e2e8f + 65d4ec4 commit 3c1d4ef

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/tcpyPI/numba.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,33 @@
88
import os
99
from functools import wraps
1010

11+
import numpy as np
12+
1113

1214
def noop_njit(*args, **kwargs):
13-
"""No-op decorator to replace @njit when numba is disabled."""
15+
"""No-op decorator to replace @njit when numba is disabled.
16+
17+
Note that this decorator isn't entirely a noop, because numba does some casting
18+
of NumPy types to Python types. Since NumPy v2, NumPy types like np.float64 are
19+
printed explicitly, so the doctests would fail without casting scalars back
20+
to Python types.
21+
22+
See <https://github.com/numba/numba/issues/704> for more details about Numba's
23+
behavior.
24+
"""
1425

1526
def decorator(func):
1627
@wraps(func)
1728
def wrapper(*args, **kwargs):
18-
return func(*args, **kwargs)
29+
result = func(*args, **kwargs)
30+
if isinstance(result, np.floating):
31+
result = float(result)
32+
elif isinstance(result, tuple):
33+
# Cast components to float
34+
result = tuple(
35+
float(x) if isinstance(x, np.floating) else x for x in result
36+
)
37+
return result
1938

2039
return wrapper
2140

src/tcpyPI/utilities.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,10 @@ def decompose_pi(pi,sstk,t0,CKCD=0.9):
292292
tuple: (lnpi, lneff, lndiseq, lnCKCD)
293293
294294
Examples:
295-
>>> result = decompose_pi(70, 300, 200, 0.9)
296-
>>> [x for x in result]
297-
[8.496..., -0.693..., 9.295..., -0.105...]
298-
>>> result = decompose_pi(50, 295, 210, 0.9)
299-
>>> [x for x in result]
300-
[7.824..., -0.904..., 8.833..., -0.105...]
295+
>>> decompose_pi(70, 300, 200, 0.9)
296+
(8.496..., -0.693..., 9.295..., -0.105...)
297+
>>> decompose_pi(50, 295, 210, 0.9)
298+
(7.824..., -0.904..., 8.833..., -0.105...)
301299
302300
Exceptional cases:
303301
- Efficiency is non-positive

0 commit comments

Comments
 (0)