Skip to content

Commit

Permalink
🎨 Change the way add_listener deprecation warning is display
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Apr 30, 2024
1 parent 01721a4 commit 415efb4
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions src/ipyaladin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,21 @@ def set_listener(self, listener_type: str, callback: callable) -> None:
listener_type is detected
"""
self.add_listener(listener_type, callback, False)
if listener_type in {"objectHovered", "object_hovered"}:
self.listener_callback["object_hovered"] = callback
elif listener_type in {"objectClicked", "object_clicked"}:
self.listener_callback["object_clicked"] = callback
elif listener_type == "click":
self.listener_callback["click"] = callback
elif listener_type == "select":
self.listener_callback["select"] = callback
else:
raise ValueError(
"listener_type must be 'object_hovered', "
"'object_clicked', 'click' or 'select'"
)

def add_listener(
self, listener_type: str, callback: callable, _dWarning: bool = True
) -> None:
def add_listener(self, listener_type: str, callback: callable) -> None:
"""Add a listener to the widget. Use set_listener instead.
Parameters
Expand All @@ -422,30 +432,15 @@ def add_listener(
callback: callable
A python function to be called when the event corresponding to the
listener_type is detected
_dWarning: bool
If True, a deprecation warning is raised
Note
----
This method is deprecated, use set_listener instead
"""
if _dWarning:
warnings.warn(
"add_listener is deprecated, use set_listener instead",
DeprecationWarning,
stacklevel=2,
)
if listener_type in {"objectHovered", "object_hovered"}:
self.listener_callback["object_hovered"] = callback
elif listener_type in {"objectClicked", "object_clicked"}:
self.listener_callback["object_clicked"] = callback
elif listener_type == "click":
self.listener_callback["click"] = callback
elif listener_type == "select":
self.listener_callback["select"] = callback
else:
raise ValueError(
"listener_type must be 'object_hovered', "
"'object_clicked', 'click' or 'select'"
)
warnings.warn(
"add_listener is deprecated, use set_listener instead",
DeprecationWarning,
stacklevel=2,
)
self.set_listener(listener_type, callback)

0 comments on commit 415efb4

Please sign in to comment.