Skip to content

Commit b5dc0ad

Browse files
committed
more ExceptionExpected
1 parent ab76c67 commit b5dc0ad

File tree

7 files changed

+86
-29
lines changed

7 files changed

+86
-29
lines changed

fastcore/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.1.2"
1+
__version__ = "1.1.3"

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"TEST_IMAGE_BW": "00_test.ipynb",
1919
"test_fig_exists": "00_test.ipynb",
2020
"ExceptionExpected": "00_test.ipynb",
21+
"exception": "00_test.ipynb",
2122
"defaults": "01_foundation.ipynb",
2223
"copy_func": "01_foundation.ipynb",
2324
"patch_to": "01_foundation.ipynb",

fastcore/test.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
__all__ = ['test_fail', 'test', 'nequals', 'test_eq', 'test_eq_type', 'test_ne', 'is_close', 'test_close', 'test_is',
44
'test_shuffled', 'test_stdout', 'test_warns', 'TEST_IMAGE', 'TEST_IMAGE_BW', 'test_fig_exists',
5-
'ExceptionExpected']
5+
'ExceptionExpected', 'exception']
66

77
# Cell
88
from .imports import *
@@ -101,12 +101,13 @@ def test_fig_exists(ax):
101101

102102
# Cell
103103
class ExceptionExpected:
104-
"A context manager to allow you to test if an exception (ex) is raised. Optionally, search the exception's error message with a regex."
105-
def __init__(self, ex:Exception, regex:str=None):store_attr()
104+
"Context manager that tests if an exception is raised"
105+
def __init__(self, ex=Exception, regex=''): store_attr()
106106
def __enter__(self): pass
107107
def __exit__(self, type, value, traceback):
108-
if isinstance(value, self.ex):
109-
if self.regex:
110-
assert re.search(self.regex, ' '.join(value.args)) is not None, f"Did not find regex:{self.regex} in Exception message."
111-
return True
112-
else: raise TypeError(f"An error of {self.ex} was not raised.")
108+
if not isinstance(value, self.ex) or (self.regex and not re.search(self.regex, f'{value.args}')):
109+
raise TypeError(f"Expected {self.ex.__name__}({self.regex}) not raised.")
110+
return True
111+
112+
# Cell
113+
exception = ExceptionExpected()

fastcore/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,9 @@ def __init__(self, mgrs): self.default,self.stack = L(mgrs),ExitStack()
675675
def __enter__(self): self.default.map(self.stack.enter_context)
676676
def __exit__(self, *args, **kwargs): self.stack.__exit__(*args, **kwargs)
677677

678+
# Cell
679+
def _typeerr(arg, val, typ): return TypeError(f"{arg}=={val} not {typ}")
680+
678681
# Cell
679682
def typed(f):
680683
"Decorator to check param and return types at runtime"
@@ -686,9 +689,9 @@ def _f(*args,**kwargs):
686689
if len(anno) > 0:
687690
for i,arg in enumerate(args): kw[names[i]] = arg
688691
for k,v in kw.items():
689-
if not isinstance(v,anno[k]): raise TypeError(f"{k}=={v} not {anno[k]}")
692+
if not isinstance(v,anno[k]): raise _typeerr(k, v, anno[k])
690693
res = f(*args,**kwargs)
691-
if ret is not None and not isinstance(res,ret): raise TypeError(f"return=={res} not {ret}")
694+
if ret is not None and not isinstance(res,ret): raise _typeerr("return", res, ret)
692695
return res
693696
return functools.update_wrapper(_f, f)
694697

nbs/00_test.ipynb

Lines changed: 56 additions & 14 deletions
Large diffs are not rendered by default.

nbs/02_utils.ipynb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@
21712171
{
21722172
"data": {
21732173
"text/plain": [
2174-
"['e', 'a', 'h', 'f', 'g', 'b', 'c', 'd']"
2174+
"['h', 'f', 'd', 'g', 'a', 'b', 'c', 'e']"
21752175
]
21762176
},
21772177
"execution_count": null,
@@ -3621,6 +3621,16 @@
36213621
"show_doc(ContextManagers, title_level=4)"
36223622
]
36233623
},
3624+
{
3625+
"cell_type": "code",
3626+
"execution_count": null,
3627+
"metadata": {},
3628+
"outputs": [],
3629+
"source": [
3630+
"#export\n",
3631+
"def _typeerr(arg, val, typ): return TypeError(f\"{arg}=={val} not {typ}\")"
3632+
]
3633+
},
36243634
{
36253635
"cell_type": "code",
36263636
"execution_count": null,
@@ -3638,9 +3648,9 @@
36383648
" if len(anno) > 0:\n",
36393649
" for i,arg in enumerate(args): kw[names[i]] = arg\n",
36403650
" for k,v in kw.items():\n",
3641-
" if not isinstance(v,anno[k]): raise TypeError(f\"{k}=={v} not {anno[k]}\")\n",
3651+
" if not isinstance(v,anno[k]): raise _typeerr(k, v, anno[k])\n",
36423652
" res = f(*args,**kwargs)\n",
3643-
" if ret is not None and not isinstance(res,ret): raise TypeError(f\"return=={res} not {ret}\")\n",
3653+
" if ret is not None and not isinstance(res,ret): raise _typeerr(\"return\", res, ret)\n",
36443654
" return res\n",
36453655
" return functools.update_wrapper(_f, f)"
36463656
]

settings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author = Jeremy Howard and Sylvain Gugger
77
author_email = [email protected]
88
copyright = fast.ai
99
branch = master
10-
version = 1.1.2
10+
version = 1.1.3
1111
min_python = 3.6
1212
audience = Developers
1313
language = English

0 commit comments

Comments
 (0)