Bug in Dynamic Enum options??

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Bug in Dynamic Enum options??

Craig McDonnell

Hi all,

 

I’m trying to define a HasTraits class with a dynamic Enum following the example here:

https://mail.enthought.com/pipermail/enthought-dev/attachments/20090113/0f56d54b/attachment.pl

 

The following code raises an error when running the line: spike.root_model = 'model4'

 

import unittest

from enthought.traits.api import Enum, HasTraits, List, Property

 

 

class Spike(HasTraits):

   

    models = Property(List)

    root_model = Enum(values='models')

   

    def _get_models(self):

        return ['model1', 'model2', 'model3']

 

 

class TestRunner(unittest.TestCase):

 

    def test_enum(self):

        spike = Spike(root_model='model1')

        spike.root_model = 'model2'

        # The following line should fail, but does so for the wrong reason:

        spike.root_model = 'model4'

 

 

if __name__ == "__main__":

    unittest.main()

 

The exception raised is:

 

Traceback (most recent call last):

  File "C:\Workspaces\PYTHON\######\test_spike.py", line 24, in test_enum

    spike.root_model = 'model4'

  File "C:\Python25\lib\site-packages\traits-3.1.0n1-py2.5-win32.egg\enthought\traits\trait_types.py", line 1974, in _set

    self.error( object, name, value )

  File "c:\python25\lib\site-packages\traits-3.1.0n1-py2.5-win32.egg\enthought\traits\trait_handlers.py", line 174, in error

    raise TraitError( object, name, self.full_info( object, name, value ),

  File "C:\Python25\lib\site-packages\traits-3.1.0n1-py2.5-win32.egg\enthought\traits\trait_types.py", line 1939, in full_info

    return ' or '.join( [ repr( x ) for x in values ] )

UnboundLocalError: local variable 'values' referenced before assignment

 

This seems to be because full_info references values without setting it when name is not an empty string:

 

    def full_info ( self, object, name, value ):

        """ Returns a description of the trait.

        """

        if self.name == '':

            values = self.values

        else:

            value = eval( self.values )

           

        return ' or '.join( [ repr( x ) for x in values ] )

 

Is this a bug or is there something I am doing wrong here? Should the line value = eval( self.values ) actually read values = eval( self.values )?

 

 

Thanks.

 

Regards,
Craig McDonnell

 

Office:              0117 906 2142
Email:               [hidden email]
Web:                www.xype.com
Address:           Xype Ltd, Unit 1, Brabazon Office Park, Golf Course Lane, Filton, Bristol, BS34 7PZ

 

This e-mail and any attachment may contain confidential and/or privileged information. If you have received this e-mail and/or attachment in error, please notify the sender immediately and delete the e-mail and any attachment from your system. If you are not the intended recipient you must not copy, distribute, disclose or use the contents of the e-mail or any attachment. 
All e-mail sent to or from this address may be accessed by someone other than the recipient for system management and security reasons or for other lawful purposes. 

Xype Limited is registered in England and Wales under company number 04516192. The company's registered office is Unit 1 Brabazon Office Park, Golf Course Lane, Filton, Bristol, BS34 7PZ

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev