Skip to content

FIX: Incorrect extension identified when checking File traits #2987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions nipype/interfaces/base/traits_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,30 @@ class File(BasePath):
>>> a.foo # doctest: +ELLIPSIS
'.../idoexist.txt'

>>> class A(TraitedSpec):
... foo = File(extensions=['.nii', '.nii.gz'])
>>> a = A()
>>> a.foo = 'badext.txt' # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
TraitError:

>>> class A(TraitedSpec):
... foo = File(extensions=['.nii', '.nii.gz'])
>>> a = A()
>>> a.foo = 'goodext.nii'
>>> a.foo
'goodext.nii'

>>> a = A()
>>> a.foo = 'idontexist.000.nii'
>>> a.foo # doctest: +ELLIPSIS
'idontexist.000.nii'

>>> a = A()
>>> a.foo = 'idontexist.000.nii.gz'
>>> a.foo # doctest: +ELLIPSIS
'idontexist.000.nii.gz'

"""

_is_file = True
Expand All @@ -292,8 +316,8 @@ def validate(self, objekt, name, value, return_pathlike=False):
"""Validate a value change."""
value = super(File, self).validate(objekt, name, value, return_pathlike=True)
if self._exts:
ext = ''.join(value.suffixes)
if ext not in self._exts:
fname = value.name
if not any((fname.endswith(e) for e in self._exts)):
self.error(objekt, name, str(value))

if not return_pathlike:
Expand Down