Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace strtobool for local function #128

Merged
merged 2 commits into from Feb 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions decouple.py
Expand Up @@ -5,7 +5,6 @@
from shlex import shlex
from io import open
from collections import OrderedDict
from distutils.util import strtobool

# Useful for very coarse version differentiation.
PYVERSION = sys.version_info
Expand All @@ -26,6 +25,18 @@

DEFAULT_ENCODING = 'UTF-8'


def strtobool(value):
_value = value.lower()
if _value in {"y", "yes", "t", "true", "on", "1"}:
henriquebastos marked this conversation as resolved.
Show resolved Hide resolved
result = True
elif _value in {"n", "no", "f", "false", "off", "0"}:
henriquebastos marked this conversation as resolved.
Show resolved Hide resolved
result = False
else:
raise ValueError(" ".join(("invalid truth value", value)))
henriquebastos marked this conversation as resolved.
Show resolved Hide resolved
return result


class UndefinedValueError(Exception):
pass

Expand Down Expand Up @@ -261,7 +272,6 @@ def __init__(self, flat=None, cast=text_type, choices=None):
self._valid_values.extend(self.flat)
self._valid_values.extend([value for value, _ in self.choices])


def __call__(self, value):
transform = self.cast(value)
if transform not in self._valid_values:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_strtobool.py
@@ -0,0 +1,28 @@
import pytest
henriquebastos marked this conversation as resolved.
Show resolved Hide resolved
from decouple import strtobool


def test_true_values():
true_list = ["y", "yes", "t", "true", "on", "1"]
henriquebastos marked this conversation as resolved.
Show resolved Hide resolved
for item in true_list:
assert strtobool(item) == 1


def test_false_values():
false_list = ["n", "no", "f", "false", "off", "0"]
henriquebastos marked this conversation as resolved.
Show resolved Hide resolved
for item in false_list:
assert strtobool(item) == 0


@pytest.mark.parametrize(
"test_input,expected",
[
("Invalid_Value_1", "invalid truth value Invalid_Value_1"),
henriquebastos marked this conversation as resolved.
Show resolved Hide resolved
("1nv4l1d_V4lu3_2", "invalid truth value 1nv4l1d_V4lu3_2"),
("invalid_value_3", "invalid truth value invalid_value_3"),
],
)
def test_eval(test_input, expected):
with pytest.raises(ValueError) as execinfo:
strtobool(test_input)
assert str(execinfo.value) == expected