Skip to content

Commit

Permalink
Fix #128
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquebastos committed Feb 2, 2022
2 parents 0137606 + f802447 commit a627eef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
18 changes: 16 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,22 @@

DEFAULT_ENCODING = 'UTF-8'


# Python 3.10 don't have strtobool anymore. So we move it here.
TRUE_VALUES = {"y", "yes", "t", "true", "on", "1"}
FALSE_VALUES = {"n", "no", "f", "false", "off", "0"}

def strtobool(value):
value = value.lower()

if value in TRUE_VALUES:
return True
elif value in FALSE_VALUES:
return False

raise ValueError("Invalid truth value: " + value)


class UndefinedValueError(Exception):
pass

Expand Down Expand Up @@ -283,7 +298,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
18 changes: 18 additions & 0 deletions tests/test_strtobool.py
@@ -0,0 +1,18 @@
import pytest
from decouple import strtobool


@pytest.mark.parametrize("value", ("Y", "YES", "T", "TRUE", "ON", "1"))
def test_true_values(value):
assert strtobool(value)


@pytest.mark.parametrize("value", ("N", "NO", "F", "FALSE", "OFF", "0"))
def test_false_values(value):
assert strtobool(value) is False


def test_invalid():
with pytest.raises(ValueError, match="Invalid truth value"):
strtobool("MAYBE")

0 comments on commit a627eef

Please sign in to comment.