Skip to content

Commit

Permalink
Merge pull request #11251 from amaltaro/fix-11250
Browse files Browse the repository at this point in the history
Fix strToBool validate function to fail on integer values
  • Loading branch information
amaltaro authored Aug 24, 2022
2 parents e07416f + 070d7e6 commit 10f6c1e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/python/Utils/Utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ def makeNonEmptyList(stringList):

def strToBool(string):
"""
_strToBool_
Try to convert a string to boolean. i.e. "True" to python True
Try to convert different variations of True or False (including a string
type object) to a boolean value.
In short:
* True gets mapped from: True, "True", "true", "TRUE".
* False gets mapped from: False, "False", "false", "FALSE"
* anything else will fail
:param string: expects a boolean or a string, but it could be anything else
:return: a boolean value, or raise an exception if value passed in is not supported
"""
if string in [False, True]:
if string is False or string is True:
return string
elif string in ["True", "true", "TRUE"]:
return True
Expand Down
2 changes: 1 addition & 1 deletion test/python/Utils_t/Utilities_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def testStrToBool(self):
for v in [False, "False", "FALSE", "false"]:
self.assertFalse(strToBool(v))

for v in ["", "alan", [], [''], {'a': 123}]:
for v in [1, 0, "", "alan", [], [''], {'a': 123}]:
self.assertRaises(ValueError, strToBool, v)

def testSafeStr(self):
Expand Down

0 comments on commit 10f6c1e

Please sign in to comment.