Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions reframe/utility/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,31 @@ class Bool(metaclass=_BuiltinType):
This type represents a boolean value but allows implicit conversions from
:class:`str`. More specifically, the following conversions are supported:

- The strings ``'yes'``, ``'true'`` and ``'1'`` are converted to ``True``.
- The strings ``'no'``, ``'false'`` and ``'0'`` are converted to
- The strings ``'yes'``, ``'y'``, 'true'`` and ``'1'`` are converted to
``True``.
- The strings ``'no'``, ``'n'``, ``'false'`` and ``'0'`` are converted to
``False``.

The built-in :class:`bool` type is registered as a subclass of this type.

Boolean test variables that are meant to be set properly from the command
line must be declared of this type and not :class:`bool`.

.. versionchanged:: 4.3.3

The strings ``'y'`` and ``'n'`` are also recognized as valid boolean
values and string comparison is now case-insensitive.

'''

_type = bool

@classmethod
def __rfm_cast_str__(cls, s):
if s in ('true', 'yes', '1'):
s = s.lower()
if s in ('true', 'yes', 'y', '1'):
return True
elif s in ('false', 'no', '0'):
elif s in ('false', 'no', 'n', '0'):
return False

raise TypeError(f'cannot convert {s!r} to bool')
Expand Down
12 changes: 6 additions & 6 deletions unittests/test_typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ def test_bool_type():
with pytest.raises(TypeError):
typ.Bool('foo')

with pytest.raises(TypeError):
typ.Bool('True')

with pytest.raises(TypeError):
typ.Bool('False')

# Test for boolean conversion
assert typ.Bool('true') is True
assert typ.Bool('True') is True
assert typ.Bool('yes') is True
assert typ.Bool('y') is True
assert typ.Bool('YeS') is True
assert typ.Bool('false') is False
assert typ.Bool('False') is False
assert typ.Bool('no') is False
assert typ.Bool('n') is False
assert typ.Bool('nO') is False


def test_duration_type():
Expand Down