Skip to content

Commit e788059

Browse files
authored
Merge pull request #2987 from oesteban/fix/2984
FIX: Incorrect extension identified when checking ``File`` traits
2 parents 865adc9 + be28002 commit e788059

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

nipype/interfaces/base/traits_extension.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,30 @@ class File(BasePath):
267267
>>> a.foo # doctest: +ELLIPSIS
268268
'.../idoexist.txt'
269269
270+
>>> class A(TraitedSpec):
271+
... foo = File(extensions=['.nii', '.nii.gz'])
272+
>>> a = A()
273+
>>> a.foo = 'badext.txt' # doctest: +IGNORE_EXCEPTION_DETAIL
274+
Traceback (most recent call last):
275+
TraitError:
276+
277+
>>> class A(TraitedSpec):
278+
... foo = File(extensions=['.nii', '.nii.gz'])
279+
>>> a = A()
280+
>>> a.foo = 'goodext.nii'
281+
>>> a.foo
282+
'goodext.nii'
283+
284+
>>> a = A()
285+
>>> a.foo = 'idontexist.000.nii'
286+
>>> a.foo # doctest: +ELLIPSIS
287+
'idontexist.000.nii'
288+
289+
>>> a = A()
290+
>>> a.foo = 'idontexist.000.nii.gz'
291+
>>> a.foo # doctest: +ELLIPSIS
292+
'idontexist.000.nii.gz'
293+
270294
"""
271295

272296
_is_file = True
@@ -292,8 +316,8 @@ def validate(self, objekt, name, value, return_pathlike=False):
292316
"""Validate a value change."""
293317
value = super(File, self).validate(objekt, name, value, return_pathlike=True)
294318
if self._exts:
295-
ext = ''.join(value.suffixes)
296-
if ext not in self._exts:
319+
fname = value.name
320+
if not any((fname.endswith(e) for e in self._exts)):
297321
self.error(objekt, name, str(value))
298322

299323
if not return_pathlike:

0 commit comments

Comments
 (0)