Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 8 additions & 1 deletion nipype/interfaces/base/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ def __repr__(self):
""" Return a well-formatted representation of the traits """
outstr = []
for name, value in sorted(self.trait_get().items()):
outstr.append('%s = %s' % (name, value))
if not name == '__all__':
outstr.append('%s = %s' % (name, value))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still necessary, with __all__ becoming a property?

return '\n{}\n'.format('\n'.join(outstr))


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove this line.

def _generate_handlers(self):
"""Find all traits with the 'xor' metadata and attach an event
handler to them.
Expand Down Expand Up @@ -309,6 +311,11 @@ def _get_sorteddict(self,
out = objekt
return out

@property
def __all__(self):
return [k for k,v in self.items() if not k == "__all__"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that items() provides a dict keyed by self.copyable_trait_names(), and you only need keys, I think you can just make this:

@property
def __all__(self):
    return self.copyable_trait_names()

You'll need to change the BET tests to either sort or check set equality, but this should work.



Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many blank lines.


class TraitedSpec(BaseTraitedSpec):
""" Create a subclass with strict traits.
Expand Down
48 changes: 48 additions & 0 deletions nipype/interfaces/base/tests/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
from ....utils.filemanip import split_filename
from ... import base as nib
from ...base import traits, Undefined
from ....interfaces import fsl
from ...utility.wrappers import Function
from ....pipeline import Node


standard_library.install_aliases()

Expand Down Expand Up @@ -47,6 +51,20 @@ class spec(nib.TraitedSpec):
assert infields.__repr__() == '\nfoo = 1\ngoo = 0.0\n'


def test_TraitedSpec_tab_completion():
bet_nd = Node(fsl.BET(), name = 'bet')
bet_interface = fsl.BET()
bet_inputs = bet_nd.inputs.class_editable_traits()
bet_outputs = bet_nd.outputs.class_editable_traits()

# Check __all__ for bet node and interface inputs
assert bet_nd.inputs.__all__ == bet_inputs
assert bet_interface.inputs.__all__ == bet_inputs

# Check __all__ for bet node outputs
assert bet_nd.outputs.__all__ == bet_outputs


@pytest.mark.skip
def test_TraitedSpec_dynamic():
from pickle import dumps, loads
Expand All @@ -63,6 +81,36 @@ def test_TraitedSpec_dynamic():
assign_a_again


def test_DynamicTraitedSpec_tab_completion():
def extract_func(list_out):
return list_out[0]

# Define interface
func_interface = Function(input_names=["list_out"],
output_names=["out_file","another_file"],
function=extract_func)
# Define node
list_extract = Node(Function(
input_names=["list_out"],output_names=["out_file"],
function=extract_func), name="list_extract")

# Check __all__ for interface inputs
expected_input = sorted(list_extract.inputs.editable_traits())
assert(sorted(func_interface.inputs.__all__) == expected_input)

# Check __all__ for node inputs
assert(sorted(list_extract.inputs.__all__) == expected_input)

# Check __all__ for node outputs
expected_output = sorted(list_extract.outputs.editable_traits())
assert(sorted(list_extract.outputs.__all__) == expected_output)

# Add trait and retest
list_extract._interface._output_names.append('added_out_trait')
expected_output = sorted(['added_out_trait',*expected_output])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 2.7, 3.4 don't like the [*x] syntax. How about making expected_output a set above, moving to set comparisons, and changing this to:

expected_output.add('added_out_trait')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice. Thanks for the tip. done

assert(sorted(list_extract.outputs.__all__) == expected_output)


def test_TraitedSpec_logic():
class spec3(nib.TraitedSpec):
_xor_inputs = ('foo', 'bar')
Expand Down