Skip to content

Commit a4b104d

Browse files
Refactor PR #128
1 parent 7e90cd3 commit a4b104d

2 files changed

Lines changed: 20 additions & 26 deletions

File tree

decouple.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@
2626
DEFAULT_ENCODING = 'UTF-8'
2727

2828

29+
# Python 3.10 don't have strtobool anymore. So we move it here.
30+
TRUE_VALUES = {"y", "yes", "t", "true", "on", "1"}
31+
FALSE_VALUES = {"n", "no", "f", "false", "off", "0"}
32+
2933
def strtobool(value):
30-
_value = value.lower()
31-
if _value in {"y", "yes", "t", "true", "on", "1"}:
32-
result = True
33-
elif _value in {"n", "no", "f", "false", "off", "0"}:
34-
result = False
35-
else:
36-
raise ValueError(" ".join(("invalid truth value", value)))
37-
return result
34+
value = value.lower()
35+
36+
if value in TRUE_VALUES:
37+
return True
38+
elif value in FALSE_VALUES:
39+
return False
40+
41+
raise ValueError("Invalid truth value: " + value)
3842

3943

4044
class UndefinedValueError(Exception):

tests/test_strtobool.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,16 @@
33

44

55
def test_true_values():
6-
true_list = ["y", "yes", "t", "true", "on", "1"]
7-
for item in true_list:
8-
assert strtobool(item) == 1
6+
for item in ("Y", "YES", "T", "TRUE", "ON", "1"):
7+
assert strtobool(item)
98

109

1110
def test_false_values():
12-
false_list = ["n", "no", "f", "false", "off", "0"]
13-
for item in false_list:
14-
assert strtobool(item) == 0
11+
for item in ("N", "NO", "F", "FALSE", "OFF", "0"):
12+
assert strtobool(item) is False
1513

1614

17-
@pytest.mark.parametrize(
18-
"test_input,expected",
19-
[
20-
("Invalid_Value_1", "invalid truth value Invalid_Value_1"),
21-
("1nv4l1d_V4lu3_2", "invalid truth value 1nv4l1d_V4lu3_2"),
22-
("invalid_value_3", "invalid truth value invalid_value_3"),
23-
],
24-
)
25-
def test_eval(test_input, expected):
26-
with pytest.raises(ValueError) as execinfo:
27-
strtobool(test_input)
28-
assert str(execinfo.value) == expected
15+
def test_invalid():
16+
with pytest.raises(ValueError, match="Invalid truth value"):
17+
strtobool("MAYBE")
18+

0 commit comments

Comments
 (0)