Skip to content

Commit aaec2c1

Browse files
committed
use add_note for exceptions
1 parent 990083a commit aaec2c1

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

template/utils/_checks.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ def ensure_int(item: Any, item_name: str | None = None) -> int:
4444
if isinstance(item, bool):
4545
raise TypeError
4646
item = int(operator.index(item))
47-
except TypeError:
47+
except TypeError as exc:
4848
item_name = "Item" if item_name is None else f"'{item_name}'"
49-
raise TypeError(f"{item_name} must be an integer, got {type(item)} instead.")
49+
exc.add_note(f"{item_name} must be an integer, got {type(item)} instead.")
50+
raise
5051

5152
return item
5253

@@ -240,15 +241,16 @@ def ensure_path(item: Any, must_exist: bool) -> Path:
240241
"""
241242
try:
242243
item = Path(item)
243-
except TypeError:
244+
except TypeError as exc:
244245
try:
245246
str_ = f"'{str(item)}' "
246247
except Exception:
247248
str_ = ""
248-
raise TypeError(
249+
exc.add_note(
249250
f"The provided path {str_}is invalid and can not be converted. Please "
250251
f"provide a str, an os.PathLike or a pathlib.Path object, not {type(item)}."
251252
)
253+
raise
252254
if must_exist and not item.exists():
253255
raise FileNotFoundError(f"The provided path '{str(item)}' does not exist.")
254256
return item

template/utils/_docs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ def fill_doc(f: Callable[..., Any]) -> Callable[..., Any]:
8989

9090
try:
9191
f.__doc__ = docstring % indented
92-
except (TypeError, ValueError, KeyError) as exp:
92+
except (TypeError, ValueError, KeyError) as exc:
9393
funcname = f.__name__
9494
funcname = docstring.split("\n")[0] if funcname is None else funcname
95-
raise RuntimeError(f"Error documenting {funcname}:\n{str(exp)}")
95+
exc.add_note(f"Error documenting {funcname}.")
96+
raise
9697

9798
return f
9899

template/utils/tests/test_docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def foo():
3030
assert foo.__doc__ is None
3131

3232
# test filling docstring with invalid key
33-
with pytest.raises(RuntimeError, match="Error documenting"):
33+
with pytest.raises(KeyError, match="Error documenting"):
3434

3535
@fill_doc
3636
def foo(verbose):

0 commit comments

Comments
 (0)