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

Remove the deprecated adapt-by-calling ability of Interfaces #1719

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 3 additions & 19 deletions traits/has_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from types import FunctionType

from . import __version__ as TraitsVersion
from .adaptation.adaptation_error import AdaptationError
from .constants import DefaultValue, TraitKind
from .ctrait import CTrait, __newobj__
from .ctraits import CHasTraits
Expand Down Expand Up @@ -3679,26 +3678,11 @@ def _veto_changed(self, state):
class MetaInterface(ABCMetaHasTraits):
""" Meta class for interfaces.

Interfaces are simple ABCs with the following features:-

1) They cannot be instantiated (they are interfaces, not implementations!).
2) Calling them is equivalent to calling 'adapt'.

Historically, there were some differences between interfaces
and ABCs in Traits, but now Interface is a near synonym for
ABCHasTraits.
"""

@deprecated('use "adapt(adaptee, protocol)" instead.')
def __call__(self, adaptee, default=AdaptationError):
""" Attempt to adapt the adaptee to this interface.

Note that this means that (intentionally ;^) that interfaces
cannot be instantiated!

"""

from traits.adaptation.api import adapt

return adapt(adaptee, self, default=default)


class Interface(HasTraits, metaclass=MetaInterface):
""" The base class for all interfaces.
Expand Down
29 changes: 5 additions & 24 deletions traits/tests/test_interface_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

# Standard library imports.
import unittest
import warnings

# Enthought library imports.
from traits.adaptation.api import reset_global_adaptation_manager
from traits.api import (
adapt,
Adapter,
HasTraits,
Instance,
Expand Down Expand Up @@ -386,17 +386,7 @@ class Foo(HasTraits):

f = Foo()

# Adaptation via direct instantiation of interfaces is deprecated, so
# catch the warning to keep the test run output clean.
with warnings.catch_warnings(record=True) as warn_msgs:
warnings.simplefilter("always", DeprecationWarning)
self.assertEqual(f, IFoo(f))

self.assertEqual(len(warn_msgs), 1)
warn_msg = warn_msgs[0]
self.assertIn(
'use "adapt(adaptee, protocol)" instead', str(warn_msg.message))
self.assertIn("test_interface_checker", warn_msg.filename)
self.assertEqual(f, adapt(f, IFoo))

def test_adaptation(self):
""" adaptation """
Expand All @@ -415,17 +405,8 @@ class FooToIFooAdapter(Adapter):

f = Foo()

# Make sure adaptation works. Adaptation via direct instantiation of
# Interface classes is deprecated, so suppress the warning.
with warnings.catch_warnings(record=True) as warn_msgs:
warnings.simplefilter("always", DeprecationWarning)
i_foo = IFoo(f)
# Make sure adaptation works.
i_foo = adapt(f, IFoo)

self.assertNotEqual(None, i_foo)
self.assertIsNotNone(i_foo)
self.assertEqual(FooToIFooAdapter, type(i_foo))

self.assertEqual(len(warn_msgs), 1)
warn_msg = warn_msgs[0]
self.assertIn(
'use "adapt(adaptee, protocol)" instead', str(warn_msg.message))
self.assertIn("test_interface_checker", warn_msg.filename)