Skip to content
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

AdaptsTo and Supports don't store default values correctly #1647

Open
corranwebster opened this issue May 20, 2022 · 0 comments
Open

AdaptsTo and Supports don't store default values correctly #1647

corranwebster opened this issue May 20, 2022 · 0 comments

Comments

@corranwebster
Copy link
Contributor

corranwebster commented May 20, 2022

When running the following code:

from traits.api import AdaptsTo, HasTraits, Instance, Interface, Supports, register_factory           

class ITestInterface(Interface): 
    pass 

class MyClass(HasTraits): 
    pass 

class MyClassTestInterfaceAdapter(HasTraits): 
    object = Instance(MyClass) 
    def __init__(self, object): 
        super().__init__(self, object=object) 

class MyClassTestInterfaceAdapter(HasTraits): 
    object = Instance(MyClass) 
    def __init__(self, object): 
        super().__init__(object=object) 

register_factory(MyClassTestInterfaceAdapter, MyClass, ITestInterface)  

class TestAdaptTraits(HasTraits): 
    supports = Supports(ITestInterface, MyClass, (), {}) 
    adapts_to = AdaptsTo(ITestInterface, MyClass, (), {}) 

t = TestAdaptTraits()                                                  

print('default t.supports:', t.supports)
print('default t.supports_:', t.supports_)

t.supports = MyClass()                                                 

print('set t.supports:', t.supports)
print('set t.supports_:', t.supports_)

print('default t.adapts_to:', t.adapts_to)
print('default t.adapts_to_:', t.adapts_to_)

t.adapts_to = MyClass()                                                 

print('set t.adapts_to:', t.adapts_to)
print('set t.adapts_to_:', t.adapts_to_)

we get output

default t.supports: <__main__.MyClass object at 0x7fd61a97e620>
default t.supports_: <__main__.MyClass object at 0x7fd61a97e620>
set t.supports: <__main__.MyClassTestInterfaceAdapter object at 0x7fd61a97e6d0>
set t.supports_: <__main__.MyClass object at 0x7fd61a97e678>
default t.adapts_to: <__main__.MyClass object at 0x7fd61a97e620>
default t.adapts_to_: <__main__.MyClass object at 0x7fd61a97e620>
set t.adapts_to: <__main__.MyClass object at 0x7fd61a97e728>
set t.adapts_to_: <__main__.MyClassTestInterfaceAdapter object at 0x7fd61a97e780

Notice that the default values of supports and adapts_to are not of the expected types - they have not had adaptation applied to them.

This can probably be fixed by refactoring them to be simpler mapped traits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant