mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
New Param: Flag now fill-in default=False and also forces default to be a bool
This commit is contained in:
@@ -638,11 +638,34 @@ class Flag(Bool):
|
||||
"""
|
||||
A boolean parameter that always gets filled in with a default value.
|
||||
|
||||
This `Bool` subclass forces ``autofill=True`` in `Flag.__init__()`.
|
||||
This `Bool` subclass forces ``autofill=True`` in `Flag.__init__()`. If no
|
||||
default is provided, it also fills in a default value of ``False``.
|
||||
Lastly, unlike the `Bool` class, the default must be either ``True`` or
|
||||
``False`` and cannot be ``None``.
|
||||
|
||||
For example:
|
||||
|
||||
>>> Flag('my_flag')
|
||||
Flag('my_flag', autofill=True, default=False)
|
||||
>>> Flag('my_flag', default=True) # Do this for default of True
|
||||
Flag('my_flag', autofill=True, default=True)
|
||||
|
||||
Also note that creating a `Flag` instance with ``autofill=False`` will have
|
||||
no effect. For example:
|
||||
|
||||
>>> Flag('my_flag', autofill=False) # autofill will still be True
|
||||
Flag('my_flag', autofill=True, default=False)
|
||||
"""
|
||||
|
||||
def __init__(self, name, *rules, **kw):
|
||||
kw['autofill'] = True
|
||||
if 'default' not in kw:
|
||||
kw['default'] = False
|
||||
if type(kw['default']) is not bool:
|
||||
default = kw['default']
|
||||
raise TypeError(
|
||||
TYPE_ERROR % ('default', bool, default, type(default))
|
||||
)
|
||||
super(Flag, self).__init__(name, *rules, **kw)
|
||||
|
||||
|
||||
@@ -751,7 +774,7 @@ class Bytes(Param):
|
||||
|
||||
class Str(Bytes):
|
||||
"""
|
||||
A parameter for Unicode text (stored in the``unicode`` type).
|
||||
A parameter for Unicode text (stored in the ``unicode`` type).
|
||||
|
||||
This class is named *Str* instead of *Unicode* so it's aligned with the
|
||||
Python v3 ``(str, unicode) => (bytes, str)`` clean-up. See:
|
||||
|
||||
@@ -554,22 +554,38 @@ class test_Flag(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.parameter.Flag.__init__` method.
|
||||
"""
|
||||
# Test with no kwargs:
|
||||
o = self.cls('my_flag')
|
||||
assert o.type is bool
|
||||
assert isinstance(o, parameter.Bool)
|
||||
assert o.autofill is True
|
||||
assert o.default is False
|
||||
|
||||
# Test with autofill=False
|
||||
o = self.cls('my_flag', autofill=False)
|
||||
# Test that TypeError is raise if default is not a bool:
|
||||
e = raises(TypeError, self.cls, 'my_flag', default=None)
|
||||
assert str(e) == TYPE_ERROR % ('default', bool, None, NoneType)
|
||||
|
||||
# Test with autofill=False, default=True
|
||||
o = self.cls('my_flag', autofill=False, default=True)
|
||||
assert o.autofill is True
|
||||
assert o.default is True
|
||||
|
||||
# Test when cloning:
|
||||
orig = self.cls('my_flag')
|
||||
for clone in [orig.clone(), orig.clone(autofill=False)]:
|
||||
assert clone.autofill is True
|
||||
assert clone.default is False
|
||||
assert clone is not orig
|
||||
assert type(clone) is self.cls
|
||||
|
||||
# Test when cloning with default=True/False
|
||||
orig = self.cls('my_flag')
|
||||
assert orig.clone().default is False
|
||||
assert orig.clone(default=True).default is True
|
||||
orig = self.cls('my_flag', default=True)
|
||||
assert orig.clone().default is True
|
||||
assert orig.clone(default=False).default is False
|
||||
|
||||
|
||||
class test_Bytes(ClassChecker):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user