Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55897f7

Browse files
committed
Deprecate considering *args, **kwargs in Timer.remove_callback.
1 parent 51c77d6 commit 55897f7

2 files changed

Lines changed: 58 additions & 20 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Timer.remove_callback won't consider ``*args, **kwargs`` in a future version
2+
````````````````````````````````````````````````````````````````````````````
3+
4+
Currently, ``Timer.remove_callback(func, *args, **kwargs)`` removes a callback
5+
previously added by ``Timer.add_callback(func, *args, **kwargs)``, but if
6+
``*args, **kwargs`` is not passed in (``Timer.remove_callback(func)``), then
7+
the first callback with a matching ``func`` is removed, regardless of whether
8+
it was added with or without ``*args, **kwargs``.
9+
10+
In a future version, ``Timer.remove_callback`` will always use the latter
11+
behavior (not consider ``*args, **kwargs``); to specifically consider them, add
12+
the callback as a `functools.partial` object ::
13+
14+
cb = timer.add_callback(functools.partial(func, *args, **kwargs))
15+
# ...
16+
# later
17+
timer.remove_callback(cb)
18+
19+
``Timer.add_callback`` was modified to return *func* to simplify the above
20+
usage (previously it returned None); this also allows using it as a decorator.
21+
22+
The new API is modelled after `atexit.register` / `atexit.unregister`.

lib/matplotlib/backend_bases.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ def set_sketch_params(self, scale=None, length=None, randomness=None):
10941094

10951095

10961096
class TimerBase(object):
1097-
'''
1097+
"""
10981098
A base class for providing timer events, useful for things animations.
10991099
Backends need to implement a few specific methods in order to use their
11001100
own timing mechanisms so that the timer events are integrated into their
@@ -1137,8 +1137,7 @@ class TimerBase(object):
11371137
Stores list of (func, args, kwargs) tuples that will be called upon
11381138
timer events. This list can be manipulated directly, or the
11391139
functions `add_callback` and `remove_callback` can be used.
1140-
1141-
'''
1140+
"""
11421141
def __init__(self, interval=None, callbacks=None):
11431142
#Initialize empty callbacks list and setup default settings if necssary
11441143
if callbacks is None:
@@ -1157,22 +1156,25 @@ def __init__(self, interval=None, callbacks=None):
11571156
self._timer = None
11581157

11591158
def __del__(self):
1160-
'Need to stop timer and possibly disconnect timer.'
1159+
"""Need to stop timer and possibly disconnect timer."""
11611160
self._timer_stop()
11621161

11631162
def start(self, interval=None):
1164-
'''
1165-
Start the timer object. `interval` is optional and will be used
1166-
to reset the timer interval first if provided.
1167-
'''
1163+
"""
1164+
Start the timer object.
1165+
1166+
Parameters
1167+
----------
1168+
interval : int, optional
1169+
Timer interval in milliseconds; overrides a previously set interval
1170+
if provided.
1171+
"""
11681172
if interval is not None:
11691173
self._set_interval(interval)
11701174
self._timer_start()
11711175

11721176
def stop(self):
1173-
'''
1174-
Stop the timer.
1175-
'''
1177+
"""Stop the timer."""
11761178
self._timer_stop()
11771179

11781180
def _timer_start(self):
@@ -1203,19 +1205,33 @@ def single_shot(self, ss):
12031205
self._timer_set_single_shot()
12041206

12051207
def add_callback(self, func, *args, **kwargs):
1206-
'''
1208+
"""
12071209
Register *func* to be called by timer when the event fires. Any
12081210
additional arguments provided will be passed to *func*.
1209-
'''
1211+
1212+
This function returns *func*, which makes it possible to use it as a
1213+
decorator.
1214+
"""
12101215
self.callbacks.append((func, args, kwargs))
1216+
return func
12111217

12121218
def remove_callback(self, func, *args, **kwargs):
1213-
'''
1214-
Remove *func* from list of callbacks. *args* and *kwargs* are optional
1215-
and used to distinguish between copies of the same function registered
1216-
to be called with different arguments.
1217-
'''
1219+
"""
1220+
Remove *func* from list of callbacks.
1221+
1222+
*args* and *kwargs* are optional and used to distinguish between copies
1223+
of the same function registered to be called with different arguments.
1224+
This behavior is deprecated. In the future, `*args, **kwargs` won't be
1225+
considered anymore; to keep a specific callback removable by itself,
1226+
pass it to `add_callback` as a `functools.partial` object.
1227+
"""
12181228
if args or kwargs:
1229+
cbook.warn_deprecated(
1230+
"3.1", "In a future version, Timer.remove_callback will not "
1231+
"take *args, **kwargs anymore, but remove all callbacks where "
1232+
"the callable matches; to keep a specific callback removable "
1233+
"by itself, pass it to add_callback as a functools.partial "
1234+
"object.")
12191235
self.callbacks.remove((func, args, kwargs))
12201236
else:
12211237
funcs = [c[0] for c in self.callbacks]
@@ -1229,11 +1245,11 @@ def _timer_set_single_shot(self):
12291245
"""Used to set single shot on underlying timer object."""
12301246

12311247
def _on_timer(self):
1232-
'''
1248+
"""
12331249
Runs all function that have been registered as callbacks. Functions
12341250
can return False (or 0) if they should not be called any more. If there
12351251
are no callbacks, the timer is automatically stopped.
1236-
'''
1252+
"""
12371253
for func, args, kwargs in self.callbacks:
12381254
ret = func(*args, **kwargs)
12391255
# docstring above explains why we use `if ret == 0` here,

0 commit comments

Comments
 (0)