Skip to content

Commit

Permalink
Add repr() calls to "invalid value" exception message in StrConverter…
Browse files Browse the repository at this point in the history
….to_bool (#2666)

* Use !r and repr to make exception messages clearer.

* Use !r and repr to make exception messages clearer. Add changelog file

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Reverted repr() on VALID_BOOL list; updated relevant test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fixed test-breaking typo

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ptmcg and pre-commit-ci[bot] committed Dec 9, 2022
1 parent 1e32d0c commit 791b4aa
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/changelog/2665.bugfix.rst
@@ -0,0 +1 @@
Use ``!r`` and ``repr()`` to better display erroneous values in exception from ``StrConverter.to_bool()`` - by :user:`ptmcg`.
6 changes: 4 additions & 2 deletions src/tox/config/loader/str_convert.py
Expand Up @@ -81,13 +81,15 @@ def to_env_list(value: str) -> EnvList:

@staticmethod
def to_bool(value: str) -> bool:
norm = value.strip().lower()
norm = str(value).strip().lower()
if norm in StrConvert.TRUTHFUL_VALUES:
return True
elif norm in StrConvert.FALSE_VALUES:
return False
else:
raise TypeError(f"value {value} cannot be transformed to bool, valid: {', '.join(StrConvert.VALID_BOOL)}")
raise TypeError(
f"value {value!r} cannot be transformed to bool, valid: {', '.join(StrConvert.VALID_BOOL)}",
)


__all__ = ("StrConvert",)
2 changes: 1 addition & 1 deletion tests/config/test_sets.py
Expand Up @@ -89,7 +89,7 @@ def test_config_bad_bool(conf_builder: ConfBuilder) -> None:
config_set.add_config(keys="bad_bool", of_type=bool, default=False, desc="bad_bool")
with pytest.raises(TypeError) as context:
assert config_set["bad_bool"]
error = "value whatever cannot be transformed to bool, valid: , 0, 1, false, no, off, on, true, yes"
error = "value 'whatever' cannot be transformed to bool, valid: , 0, 1, false, no, off, on, true, yes"
assert str(context.value) == error


Expand Down

0 comments on commit 791b4aa

Please sign in to comment.