Skip to content

Commit d674840

Browse files
authored
Merge pull request #746 from nipype/typing-bugfixes
fixes issues with super->sub-class auto-cast and handles MultiInputObj coercion
2 parents ad47c1e + ad28ae5 commit d674840

File tree

7 files changed

+381
-207
lines changed

7 files changed

+381
-207
lines changed

pydra/engine/specs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ def __getattr__(self, name):
694694
raise AttributeError(f"{name} hasn't been set yet")
695695
if name not in self._field_names:
696696
raise AttributeError(
697-
f"Task {self._task.name} has no {self._attr_type} attribute {name}"
697+
f"Task '{self._task.name}' has no {self._attr_type} attribute '{name}', "
698+
"available: '" + "', '".join(self._field_names) + "'"
698699
)
699700
type_ = self._get_type(name)
700701
splits = self._get_task_splits()

pydra/engine/tests/test_specs.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ def test_lazy_getvale():
124124
lf = LazyIn(task=tn)
125125
with pytest.raises(Exception) as excinfo:
126126
lf.inp_c
127-
assert str(excinfo.value) == "Task tn has no input attribute inp_c"
127+
assert (
128+
str(excinfo.value)
129+
== "Task 'tn' has no input attribute 'inp_c', available: 'inp_a', 'inp_b'"
130+
)
128131

129132

130133
def test_input_file_hash_1(tmp_path):

pydra/engine/tests/test_workflow.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from ..core import Workflow
3838
from ... import mark
3939
from ..specs import SpecInfo, BaseSpec, ShellSpec
40+
from pydra.utils import exc_info_matches
4041

4142

4243
def test_wf_no_input_spec():
@@ -102,13 +103,15 @@ def test_wf_dict_input_and_output_spec():
102103
wf.inputs.a = "any-string"
103104
wf.inputs.b = {"foo": 1, "bar": False}
104105

105-
with pytest.raises(TypeError, match="Cannot coerce 1.0 into <class 'str'>"):
106+
with pytest.raises(TypeError) as exc_info:
106107
wf.inputs.a = 1.0
107-
with pytest.raises(
108-
TypeError,
109-
match=("Could not coerce object, 'bad-value', to any of the union types "),
110-
):
108+
assert exc_info_matches(exc_info, "Cannot coerce 1.0 into <class 'str'>")
109+
110+
with pytest.raises(TypeError) as exc_info:
111111
wf.inputs.b = {"foo": 1, "bar": "bad-value"}
112+
assert exc_info_matches(
113+
exc_info, "Could not coerce object, 'bad-value', to any of the union types"
114+
)
112115

113116
result = wf()
114117
assert result.output.a == "any-string"
@@ -5002,14 +5005,13 @@ def test_wf_input_output_typing():
50025005
output_spec={"alpha": int, "beta": ty.List[int]},
50035006
)
50045007

5005-
with pytest.raises(
5006-
TypeError, match="Cannot coerce <class 'list'> into <class 'int'>"
5007-
):
5008+
with pytest.raises(TypeError) as exc_info:
50085009
list_mult_sum(
50095010
scalar=wf.lzin.y,
50105011
in_list=wf.lzin.y,
50115012
name="A",
50125013
)
5014+
exc_info_matches(exc_info, "Cannot coerce <class 'list'> into <class 'int'>")
50135015

50145016
wf.add( # Split over workflow input "x" on "scalar" input
50155017
list_mult_sum(

pydra/utils/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .misc import user_cache_dir, add_exc_note # noqa: F401
1+
from .misc import user_cache_dir, add_exc_note, exc_info_matches # noqa: F401

pydra/utils/misc.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
import re
23
import platformdirs
34
from pydra._version import __version__
45

@@ -31,3 +32,14 @@ def add_exc_note(e: Exception, note: str) -> Exception:
3132
else:
3233
e.args = (e.args[0] + "\n" + note,)
3334
return e
35+
36+
37+
def exc_info_matches(exc_info, match, regex=False):
38+
if exc_info.value.__cause__ is not None:
39+
msg = str(exc_info.value.__cause__)
40+
else:
41+
msg = str(exc_info.value)
42+
if regex:
43+
return re.match(".*" + match, msg)
44+
else:
45+
return match in msg

0 commit comments

Comments
 (0)