Improve the warning message for the implicitly str conversion

Signed-off-by: William Jamir Silva <williamjamir@gmail.com>
This commit is contained in:
William Jamir Silva 2018-10-31 14:22:42 -03:00 committed by William Jamir Silva
parent 0fea71a4f5
commit 5404246e64
2 changed files with 10 additions and 6 deletions

View File

@ -230,10 +230,12 @@ class MonkeyPatch(object):
if not isinstance(value, str): if not isinstance(value, str):
warnings.warn( warnings.warn(
pytest.PytestWarning( pytest.PytestWarning(
"Environment variable value {!r} should be str, converted to str implicitly".format( "Value of environment variable {name} type should be str, but got "
value "{value!r} (type: {type}); converted to str implicitly".format(
) name=name, value=value, type=type(value).__name__
) )
),
stacklevel=2,
) )
value = str(value) value = str(value)
if prepend and name in os.environ: if prepend and name in os.environ:

View File

@ -3,6 +3,7 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
import os import os
import re
import sys import sys
import textwrap import textwrap
@ -226,9 +227,10 @@ class TestEnvironWarnings(object):
def test_setenv_non_str_warning(self, monkeypatch): def test_setenv_non_str_warning(self, monkeypatch):
value = 2 value = 2
msg = ( msg = (
"Environment variable value {!r} should be str, converted to str implicitly" "Value of environment variable PYTEST_INTERNAL_MY_VAR type should be str, "
"but got 2 (type: int); converted to str implicitly"
) )
with pytest.warns(pytest.PytestWarning, match=msg.format(value)): with pytest.warns(pytest.PytestWarning, match=re.escape(msg)):
monkeypatch.setenv(str(self.VAR_NAME), value) monkeypatch.setenv(str(self.VAR_NAME), value)
@ -337,7 +339,7 @@ def test_importerror(testdir):
) )
testdir.tmpdir.join("test_importerror.py").write( testdir.tmpdir.join("test_importerror.py").write(
textwrap.dedent( textwrap.dedent(
"""\ r"""\
def test_importerror(monkeypatch): def test_importerror(monkeypatch):
monkeypatch.setattr('package.a.x', 2) monkeypatch.setattr('package.a.x', 2)
""" """