Skip to content

Commit 791b4aa

Browse files
Add repr() calls to "invalid value" exception message in StrConverter.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>
1 parent 1e32d0c commit 791b4aa

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

docs/changelog/2665.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use ``!r`` and ``repr()`` to better display erroneous values in exception from ``StrConverter.to_bool()`` - by :user:`ptmcg`.

src/tox/config/loader/str_convert.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,15 @@ def to_env_list(value: str) -> EnvList:
8181

8282
@staticmethod
8383
def to_bool(value: str) -> bool:
84-
norm = value.strip().lower()
84+
norm = str(value).strip().lower()
8585
if norm in StrConvert.TRUTHFUL_VALUES:
8686
return True
8787
elif norm in StrConvert.FALSE_VALUES:
8888
return False
8989
else:
90-
raise TypeError(f"value {value} cannot be transformed to bool, valid: {', '.join(StrConvert.VALID_BOOL)}")
90+
raise TypeError(
91+
f"value {value!r} cannot be transformed to bool, valid: {', '.join(StrConvert.VALID_BOOL)}",
92+
)
9193

9294

9395
__all__ = ("StrConvert",)

tests/config/test_sets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_config_bad_bool(conf_builder: ConfBuilder) -> None:
8989
config_set.add_config(keys="bad_bool", of_type=bool, default=False, desc="bad_bool")
9090
with pytest.raises(TypeError) as context:
9191
assert config_set["bad_bool"]
92-
error = "value whatever cannot be transformed to bool, valid: , 0, 1, false, no, off, on, true, yes"
92+
error = "value 'whatever' cannot be transformed to bool, valid: , 0, 1, false, no, off, on, true, yes"
9393
assert str(context.value) == error
9494

9595

0 commit comments

Comments
 (0)