summaryrefslogtreecommitdiff
path: root/alot/ui.py
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2016-12-13 11:53:47 -0800
committerDylan Baker <dylan@pnwbakers.com>2016-12-13 12:34:15 -0800
commit02431efd92060ea5d1f64bfc700ae9b1e5f4907d (patch)
tree5fc98c8c4011bc84c2c87cec26eafd5f3bfbfefe /alot/ui.py
parent42504da6791fb12527c708759715b7a79d5a1ddb (diff)
Replace mutable keyword arguments
There are a number of cases of mutable keyword arguments (list and dict in this case). Mutable keyword arguments are rather dangerous, since any mutation of the default value is persistent, which will inevitably lead to bugs. For example, imagine this code: def func(def=[]): def.append('foo') return def >>> func() ['foo'] >>> func() ['foo', 'foo'] This is almost certainly not what was intended. This code generally uses the idiom of setting the default value to None, and then assigning with or `value = value or []` which will replace value with the empty list (or dict) when value is falsey, like None or another empty list.
Diffstat (limited to 'alot/ui.py')
-rw-r--r--alot/ui.py9
1 files changed, 6 insertions, 3 deletions
diff --git a/alot/ui.py b/alot/ui.py
index 105560d6..aa9d89c8 100644
--- a/alot/ui.py
+++ b/alot/ui.py
@@ -241,7 +241,7 @@ class UI(object):
self._unlock_callback = afterwards
self._locked = True
- def prompt(self, prefix, text=u'', completer=None, tab=0, history=[]):
+ def prompt(self, prefix, text=u'', completer=None, tab=0, history=None):
"""
prompt for text input.
This returns a :class:`~twisted.defer.Deferred` that calls back with
@@ -260,6 +260,8 @@ class UI(object):
:type history: list of str
:rtype: :class:`twisted.defer.Deferred`
"""
+ history = history or []
+
d = defer.Deferred() # create return deferred
oldroot = self.mainloop.widget
@@ -442,8 +444,8 @@ class UI(object):
self._notificationbar = None
self.update()
- def choice(self, message, choices={'y': 'yes', 'n': 'no'},
- select=None, cancel=None, msg_position='above'):
+ def choice(self, message, choices=None, select=None, cancel=None,
+ msg_position='above'):
"""
prompt user to make a choice.
@@ -462,6 +464,7 @@ class UI(object):
:type msg_position: str
:rtype: :class:`twisted.defer.Deferred`
"""
+ choices = choices or {'y': 'yes', 'n': 'no'}
assert select in choices.values() + [None]
assert cancel in choices.values() + [None]
assert msg_position in ['left', 'above']