bizarre trait error

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

bizarre trait error

Roan LaPlante
I was trying to create an Instance of threading.Condition like so:

from traits.api import *
from threading import Condition

class Test(HasTraits):
    cond_var=Instance(Condition)

    def __init__(self,cond):
        self.cond_var=cond

Test(Condition())



This code crashes with

Traceback (most recent call last):
  File "cond_var.py", line 10, in <module>
    Test(Condition())
  File "cond_var.py", line 8, in __init__
    self.cond_var=cond
  File "/local_mount/space/truffles/1/users/rlaplant/epd/lib/python2.7/site-packages/traits/trait_handlers.py", line 170, in error
    value )
traits.trait_errors.TraitError: The 'cond_var' trait of a Test instance must be a function or None, but a value of <Condition(<_RLock owner=None count=0>, 0)> <class 'threading._Condition'> was specified.


If the type of cond_var is changed to Any, it works without complaint.


What could be going on here?


thanks,
--
Roan LaPlante
Athinoula A. Martinos Center for Biomedical Imaging


_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: bizarre trait error

Robert Kern
On Wed, Dec 4, 2013 at 5:14 PM, Roan LaPlante
<[hidden email]> wrote:

> I was trying to create an Instance of threading.Condition like so:
>
> from traits.api import *
> from threading import Condition
>
> class Test(HasTraits):
>     cond_var=Instance(Condition)
>
>     def __init__(self,cond):
>         self.cond_var=cond
>
> Test(Condition())
>
>
>
> This code crashes with
>
> Traceback (most recent call last):
>   File "cond_var.py", line 10, in <module>
>     Test(Condition())
>   File "cond_var.py", line 8, in __init__
>     self.cond_var=cond
>   File
> "/local_mount/space/truffles/1/users/rlaplant/epd/lib/python2.7/site-packages/traits/trait_handlers.py",
> line 170, in error
>     value )
> traits.trait_errors.TraitError: The 'cond_var' trait of a Test instance must
> be a function or None, but a value of <Condition(<_RLock owner=None
> count=0>, 0)> <class 'threading._Condition'> was specified.
>
>
> If the type of cond_var is changed to Any, it works without complaint.
>
>
> What could be going on here?

threading.Condition is a factory function that returns a
threading._Condition object. It is not a type itself. Most of the
"classes" in threading are similarly arranged to discourage naive
subclassing. You can use `Instance(threading._Condition)` if you
really want, but a plain `Any()` is perfectly fine.

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: bizarre trait error

Roan LaPlante
gotcha

thanks,
R


On Wed, Dec 4, 2013 at 12:24 PM, Robert Kern <[hidden email]> wrote:
On Wed, Dec 4, 2013 at 5:14 PM, Roan LaPlante
<[hidden email]> wrote:
> I was trying to create an Instance of threading.Condition like so:
>
> from traits.api import *
> from threading import Condition
>
> class Test(HasTraits):
>     cond_var=Instance(Condition)
>
>     def __init__(self,cond):
>         self.cond_var=cond
>
> Test(Condition())
>
>
>
> This code crashes with
>
> Traceback (most recent call last):
>   File "cond_var.py", line 10, in <module>
>     Test(Condition())
>   File "cond_var.py", line 8, in __init__
>     self.cond_var=cond
>   File
> "/local_mount/space/truffles/1/users/rlaplant/epd/lib/python2.7/site-packages/traits/trait_handlers.py",
> line 170, in error
>     value )
> traits.trait_errors.TraitError: The 'cond_var' trait of a Test instance must
> be a function or None, but a value of <Condition(<_RLock owner=None
> count=0>, 0)> <class 'threading._Condition'> was specified.
>
>
> If the type of cond_var is changed to Any, it works without complaint.
>
>
> What could be going on here?

threading.Condition is a factory function that returns a
threading._Condition object. It is not a type itself. Most of the
"classes" in threading are similarly arranged to discourage naive
subclassing. You can use `Instance(threading._Condition)` if you
really want, but a plain `Any()` is perfectly fine.

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


The information in this e-mail is intended only for the person to whom it is
addressed. If you believe this e-mail was sent to you in error and the e-mail
contains patient information, please contact the Partners Compliance HelpLine at
http://www.partners.org/complianceline . If the e-mail was sent to you in error
but does not contain patient information, please contact the sender and properly
dispose of the e-mail.




--
Roan LaPlante
Athinoula A. Martinos Center for Biomedical Imaging


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