Skip to content

Commit d7754fb

Browse files
ENH: fix raising exceptions with confusing tracebacks (#122)
* fix raising exceptions with confusing tracebacks Plus: - removing some pragmas (why?) * typo in code - str not documented as valid input data * Fix tests for improved error message * Pytest: im too bad at reading xd * dang it, im too tired * pragma: no cover - i see what you did there. sry for the emails * Adam's code review Co-Authored-By: Adam R. Jensen <39184289+AdamRJensen@users.noreply.github.com> --------- Co-authored-by: Adam R. Jensen <39184289+AdamRJensen@users.noreply.github.com>
1 parent 41fb2dd commit d7754fb

5 files changed

Lines changed: 24 additions & 12 deletions

File tree

src/solposx/solarposition/psa.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections.abc import Iterable
2+
13
import numpy as np
24
import pandas as pd
35

@@ -72,13 +74,21 @@ def psa(times, latitude, longitude, *, coefficients=2020):
7274
:doi:`10.1016/j.solener.2020.10.084`
7375
"""
7476

75-
if isinstance(coefficients, (int, str)):
77+
if isinstance(coefficients, int):
7678
try:
7779
p = _PSA_PARAMS[coefficients]
7880
except KeyError:
79-
raise ValueError(f"unknown coefficients set: {coefficients}")
80-
else:
81+
raise ValueError(
82+
f"Unknown coefficients set: {coefficients}. "
83+
f"Available options are: {_PSA_PARAMS.keys()}."
84+
) from None
85+
elif isinstance(coefficients, Iterable) and len(coefficients) == 15:
8186
p = coefficients
87+
else:
88+
raise ValueError(
89+
f"Coefficients must be one of: {_PSA_PARAMS.keys()}, "
90+
"or a list of 15 coefficients."
91+
)
8292

8393
phi = np.radians(latitude)
8494
lambda_t = longitude

src/solposx/solarposition/sg2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ def sg2_c(times, latitude, longitude, elevation=0, *, pressure=101325,
287287
except ImportError: # pragma: no cover
288288
# Raise an error if package is not available
289289
raise ImportError(
290-
'The sg2_c function requires the sg2 Python package.'
291-
) # pragma: no cover
290+
"The sg2_c function requires the sg2 Python package."
291+
) from None
292292

293293
# list of geopoints as 2D array of (N,3) where each row is repectively
294294
# longitude in degrees, latitude in degrees and altitude in meters.

src/solposx/solarposition/skyfield.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def skyfield(times, latitude, longitude, *, de='de440.bsp'):
4949
except ImportError: # pragma: no cover
5050
# Raise an error if package is not available
5151
raise ImportError(
52-
'The skyfield function requires the skyfield Python package.'
53-
) # pragma: no cover
52+
"The skyfield function requires the skyfield Python package."
53+
) from None
5454

5555
if isinstance(de, str):
5656
de = load(de)

src/solposx/tools.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ def _pandas_to_utc(pd_object):
2424
try:
2525
pd_object_utc = pd_object.tz_convert('UTC')
2626
except TypeError:
27-
raise TypeError('The provided time stamps are timezone naive.'
28-
'Please make the time stamps timezone aware.'
29-
'See ``pandas.Timestamp.tz_localize``.')
27+
raise TypeError(
28+
"The provided time stamps are timezone naive."
29+
"Please make the time stamps timezone aware."
30+
"See ``pandas.Timestamp.tz_localize``."
31+
) from None
3032
return pd_object_utc
3133

3234

tests/test_solarposition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,13 @@ def test_michalsky_julian_date_value_error():
495495

496496

497497
def test_psa_coefficients_value_error():
498-
with pytest.raises(ValueError, match='unknown coefficients set: 1999'):
498+
with pytest.raises(ValueError, match='Unknown coefficients set: 1999'):
499499
_ = psa(
500500
times=pd.date_range('2020-01-01', '2020-01-02', tz='UTC'),
501501
latitude=50, longitude=10,
502502
coefficients=1999, # not a correct option
503503
)
504-
with pytest.raises(ValueError, match='unknown coefficients set'):
504+
with pytest.raises(ValueError, match='Coefficients must be one of'):
505505
_ = psa(
506506
times=pd.date_range('2020-01-01', '2020-01-02', tz='UTC'),
507507
latitude=50, longitude=10,

0 commit comments

Comments
 (0)