enthought / traits

Observable typed attributes for Python classes
Other
437 stars 85 forks source link

Bad behavior of inherited traits with _ = Disallow #30

Open sccolbert opened 12 years ago

sccolbert commented 12 years ago

This works as expected:

In [11]: class Foo(HasTraits):
   ....:     pass
   ....: 

In [12]: class Bar(Foo):
   ....:     _ = Disallow
   ....:     

In [13]: f = Foo()

In [14]: f.a = 12

In [15]: b = Bar()

In [16]: b.a = 12
---------------------------------------------------------------------------
TraitError                                Traceback (most recent call last)
/Users/chris/Development/git_repos/enaml/<ipython-input-16-a15ba401819f> in <module>()
----> 1 b.a = 12

TraitError: Cannot set the undefined 'a' attribute of a 'Bar' object.

But this is bad behavior with an instance defining behavior of the entire class and subclasses. It seems that adding an instance trait also gets added to the class traits dict. Not sure how deep down the rabbit hole fixing this would take us.

In [17]: class Foo(HasTraits):
   ....:     pass
   ....: 

In [18]: f = Foo()

In [19]: f.a = 12

In [20]: class Bar(Foo):
   ....:     _ = Disallow
   ....:     

In [21]: b = Bar()

In [22]: b.a = 12
sccolbert commented 12 years ago

Assigning to Robert just to get his opinion; not necessarily expecting a fix.