Skip to content

Commit

Permalink
Make strtobool() return a boolean value, not an integer (0, 1) (#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
tm-kn authored Mar 15, 2024
1 parent 5b18a50 commit bf5f9f9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"debug_toolbar.middleware.DebugToolbarMiddleware",
]

def show_toolbar(request):
def show_toolbar(request) -> bool:
return strtobool(os.getenv("DEBUG_TOOLBAR", "False"))

DEBUG_TOOLBAR_CONFIG = {
Expand Down
8 changes: 4 additions & 4 deletions config/settings/util.py
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 added etna/tests/__init__.py
Empty file.
78 changes: 78 additions & 0 deletions etna/tests/test_config.py
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)

0 comments on commit bf5f9f9

Please sign in to comment.