Skip to content

Commit e84f8b6

Browse files
authored
Merge pull request #996 from tsalo/raise-on-nonstandard-modifiers
Raise exception on nonstandard spaces with modifiers
2 parents eb9d4dd + a8fe5a8 commit e84f8b6

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

niworkflows/utils/spaces.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,11 +708,17 @@ def __call__(self, parser, namespace, values, option_string=None):
708708
if not values:
709709
# option was called without any output spaces, so user does not want outputs
710710
spaces.checkpoint()
711+
712+
invalid_spaces = []
711713
for val in values:
712714
val = val.rstrip(':')
715+
space = val.split(':', 1)[0]
716+
if space in NONSTANDARD_REFERENCES and ':' in val:
717+
invalid_spaces.append(val)
718+
713719
if (
714-
val not in NONSTANDARD_REFERENCES
715-
and not val.split(':')[0].startswith('fs')
720+
space not in NONSTANDARD_REFERENCES
721+
and not space.startswith('fs')
716722
and ':res-' not in val
717723
and ':resolution-' not in val
718724
):
@@ -721,8 +727,16 @@ def __call__(self, parser, namespace, values, option_string=None):
721727
# https://github.com/nipreps/niworkflows/pull/457#discussion_r375510227
722728
# https://github.com/nipreps/niworkflows/pull/494
723729
val = f'{val}:res-native'
730+
724731
for sp in Reference.from_string(val):
725732
spaces.add(sp)
733+
734+
if invalid_spaces:
735+
raise ValueError(
736+
'Modifiers are not allowed for nonstandard spaces. '
737+
f'Invalid space(s): {", ".join(invalid_spaces)}'
738+
)
739+
726740
setattr(namespace, self.dest, spaces)
727741

728742

niworkflows/utils/tests/test_spaces.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,16 @@ def test_space_action_edgecases(parser, flag, expected):
104104
pargs = parser.parse_known_args(flag)[0]
105105
spaces = pargs.spaces
106106
assert spaces.is_cached() is expected
107+
108+
109+
@pytest.mark.parametrize(
110+
('spaces', 'expected'),
111+
[
112+
(('func:res-01',), 'Modifiers are not allowed for nonstandard spaces.'),
113+
(('anat:den-91k',), 'Modifiers are not allowed for nonstandard spaces.'),
114+
(('shouldraise',), 'space identifier "shouldraise" is invalid.'),
115+
],
116+
)
117+
def test_space_action_invalid_spaces(parser, spaces, expected):
118+
with pytest.raises(ValueError, match=expected):
119+
parser.parse_known_args(args=('--spaces',) + spaces)[0]

0 commit comments

Comments
 (0)