From ae173f0f5126dcd8db9f43d6fdb37439873e9233 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 13 Apr 2020 11:49:39 -0700 Subject: [PATCH] Enable BytesWarning during test and fix discovered case (#1322) The Python command line argument -b causes Python to emit a warning when bytes and str usage is mixed. This is generally considered bad practice as either one or the other is required. Enabling this feature during tests helps catch them before reaching production. The warning appeared as: tests/test_scripting.py::TestScripting::test_eval_msgpack_pipeline_error_in_lua .../redis-py/redis/client.py:3967: BytesWarning: str() on a bytes instance cmd = ' '.join(imap(safe_unicode, command)) --- redis/_compat.py | 6 +++++- tox.ini | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/redis/_compat.py b/redis/_compat.py index e4cc34c950..146e37dd56 100644 --- a/redis/_compat.py +++ b/redis/_compat.py @@ -166,6 +166,11 @@ def itervalues(x): def nativestr(x): return x if isinstance(x, str) else x.decode('utf-8', 'replace') + def safe_unicode(value): + if isinstance(value, bytes): + value = value.decode('utf-8', 'replace') + return str(value) + next = next unichr = chr imap = map @@ -173,7 +178,6 @@ def nativestr(x): xrange = range basestring = str unicode = str - safe_unicode = str long = int BlockingIOError = BlockingIOError diff --git a/tox.ini b/tox.ini index 6ead28246c..35a4cbb655 100644 --- a/tox.ini +++ b/tox.ini @@ -9,7 +9,7 @@ deps = pytest >= 2.7.0 extras = hiredis: hiredis -commands = {envpython} -m coverage run -m pytest -W always {posargs} +commands = {envpython} -b -m coverage run -m pytest -W always {posargs} [testenv:pycodestyle] basepython = python3.6