-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make strtobool() return a boolean value, not an integer (0, 1) (#1565)
- Loading branch information
Showing
4 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
def strtobool(val): | ||
"""Convert a string representation of truth to true (1) or false (0). | ||
def strtobool(val: str) -> bool: | ||
"""Convert a string representation of truth to true or false. | ||
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values | ||
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if | ||
'val' is anything else. | ||
""" | ||
val = val.lower() | ||
if val in ("y", "yes", "t", "true", "on", "1"): | ||
return 1 | ||
return True | ||
elif val in ("n", "no", "f", "false", "off", "0"): | ||
return 0 | ||
return False | ||
else: | ||
raise ValueError("invalid truth value %r" % (val,)) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# This file lives here rather than in the config package, because the test runner | ||
# does not seem able to find it there. Also the test.py setting file is being | ||
# discovered as a test. | ||
import django.test | ||
|
||
from config.settings.util import strtobool | ||
|
||
|
||
class TestStrToBool(django.test.SimpleTestCase): | ||
TRUTHY_VALUES = ( | ||
"y", | ||
"yes", | ||
"t", | ||
"true", | ||
"on", | ||
"1", | ||
"Y", | ||
"YES", | ||
"T", | ||
"TRUE", | ||
"ON", | ||
"oN", | ||
"tRuE", | ||
"YeS", | ||
"Yes", | ||
) | ||
|
||
def test_truthy_values(self) -> None: | ||
for value in self.TRUTHY_VALUES: | ||
with self.subTest(value=value): | ||
self.assertIs(strtobool(value), True) | ||
|
||
FALSY_VALUES = ( | ||
"n", | ||
"no", | ||
"f", | ||
"false", | ||
"off", | ||
"0", | ||
"N", | ||
"NO", | ||
"F", | ||
"FALSE", | ||
"OFF", | ||
"fALsE", | ||
"nO", | ||
"No", | ||
"ofF", | ||
) | ||
|
||
def test_falsy_values(self) -> None: | ||
for value in self.FALSY_VALUES: | ||
with self.subTest(value=value): | ||
self.assertIs(strtobool(value), False) | ||
|
||
INCORRECT_VALUES = ( | ||
"", | ||
" ", | ||
"\t", | ||
" n", | ||
" no ", | ||
" f ", | ||
" false ", | ||
" off ", | ||
"random value", | ||
) | ||
|
||
def test_incorrect_values(self): | ||
for value in self.INCORRECT_VALUES: | ||
exception_message = f"invalid truth value {value!r}" | ||
with self.subTest(value=value, exception_message=exception_message): | ||
with self.assertRaises(ValueError) as cm: | ||
strtobool(value) | ||
# We are using assertRaises instead of assertRaisesRegex | ||
# because we want to test values like "\t" which won't work | ||
# with assertRaisesRegex. Therefore we need to test the message | ||
# with assertEqual separately. | ||
self.assertEqual(str(cm.exception), exception_message) |