Skip to content

Commit

Permalink
Remove the deprecated class creation listeners (#1730)
Browse files Browse the repository at this point in the history
This PR removes the deprecated `MetaHasTraits.add_listener` and
`MetaHasTraits.remove_listener` methods.

**Checklist**
- [x] Tests
- [x] Update API reference (`docs/source/traits_api_reference`) - N/A:
feature was not documented
- [x] Update User manual (`docs/source/traits_user_manual`) - N/A:
feature was not documented
- [x] Update type annotation hints in stub files

For the rationale for the deprecation, see #1549.
  • Loading branch information
mdickinson committed Apr 17, 2023
1 parent bef7857 commit 8851999
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 65 deletions.
48 changes: 3 additions & 45 deletions traits/has_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,54 +390,12 @@ class MetaHasTraits(type):
"""

# All registered class creation listeners.
#
# { Str class_name : Callable listener }
_listeners = {}

def __new__(cls, class_name, bases, class_dict):
# Convert entries in the class dictionary into traits, as appropriate.
update_traits_class_dict(class_name, bases, class_dict)

# Finish building the class using the updated class dictionary:
klass = type.__new__(cls, class_name, bases, class_dict)

# Call all listeners that registered for this specific class:
name = "%s.%s" % (klass.__module__, klass.__name__)
for listener in MetaHasTraits._listeners.get(name, []):
listener(klass)

# Call all listeners that registered for ANY class:
for listener in MetaHasTraits._listeners.get("", []):
listener(klass)

return klass

@classmethod
def add_listener(cls, listener, class_name=""):
""" Adds a class creation listener.
If the class name is the empty string then the listener will be called
when *any* class is created.
.. deprecated:: 6.3.0
"""
warnings.warn(
"add_listener is deprecated", DeprecationWarning, stacklevel=2
)

MetaHasTraits._listeners.setdefault(class_name, []).append(listener)

@classmethod
def remove_listener(cls, listener, class_name=""):
""" Removes a class creation listener.
.. deprecated:: 6.3.0
"""
warnings.warn(
"remove_listener is deprecated", DeprecationWarning, stacklevel=2
)

MetaHasTraits._listeners[class_name].remove(listener)
# Finish building the class using the updated class dictionary.
return type.__new__(cls, class_name, bases, class_dict)


def update_traits_class_dict(class_name, bases, class_dict):
Expand Down
4 changes: 0 additions & 4 deletions traits/has_traits.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ class _SimpleTest:

class MetaHasTraits(type):
def __new__(cls, class_name: _Any, bases: _Any, class_dict: _Any): ...
@classmethod
def add_listener(cls, listener: _Any, class_name: str = ...) -> None: ...
@classmethod
def remove_listener(cls, listener: _Any, class_name: str = ...) -> None: ...

def update_traits_class_dict(class_name: _Any, bases: _Any, class_dict: _Any): ...
def migrate_property(name: _Any, property: _Any, property_info: _Any, class_dict: _Any): ...
Expand Down
16 changes: 0 additions & 16 deletions traits/tests/test_has_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
SingletonHasTraits,
SingletonHasStrictTraits,
SingletonHasPrivateTraits,
MetaHasTraits,
)
from traits.ctrait import CTrait
from traits.observation.api import (
Expand Down Expand Up @@ -56,21 +55,6 @@ def _dummy_validator(self, value):
pass


class TestMetaHasTraits(unittest.TestCase):
def test_add_listener_and_remove_listener_deprecated(self):
def listener(cls):
pass

with self.assertWarnsRegex(
DeprecationWarning, "add_listener is deprecated"
):
MetaHasTraits.add_listener(listener)
with self.assertWarnsRegex(
DeprecationWarning, "remove_listener is deprecated"
):
MetaHasTraits.remove_listener(listener)


class TestCreateTraitsMetaDict(unittest.TestCase):
def test_class_attributes(self):
# Given
Expand Down

0 comments on commit 8851999

Please sign in to comment.