From 99e8129ba361fe81e72f4fb2fb127de0f0447d05 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sat, 3 Feb 2024 18:37:49 +0200 Subject: [PATCH] compat: get rid of STRING_TYPES I think it only obfuscates the code, also calling `bytes` a string type is pretty misleading in Python 3. --- src/_pytest/compat.py | 3 --- src/_pytest/python.py | 3 +-- src/_pytest/python_api.py | 10 ++-------- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/_pytest/compat.py b/src/_pytest/compat.py index dbe22e4aa..26a45349a 100644 --- a/src/_pytest/compat.py +++ b/src/_pytest/compat.py @@ -181,9 +181,6 @@ def _translate_non_printable(s: str) -> str: return s.translate(_non_printable_ascii_translate_table) -STRING_TYPES = bytes, str - - def _bytes_to_ascii(val: bytes) -> str: return val.decode("ascii", "backslashreplace") diff --git a/src/_pytest/python.py b/src/_pytest/python.py index a5ae48134..10ca00890 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -50,7 +50,6 @@ from _pytest.compat import is_generator from _pytest.compat import NOTSET from _pytest.compat import safe_getattr from _pytest.compat import safe_isclass -from _pytest.compat import STRING_TYPES from _pytest.config import Config from _pytest.config import ExitCode from _pytest.config import hookimpl @@ -998,7 +997,7 @@ class IdMaker: def _idval_from_value(self, val: object) -> Optional[str]: """Try to make an ID for a parameter in a ParameterSet from its value, if the value type is supported.""" - if isinstance(val, STRING_TYPES): + if isinstance(val, (str, bytes)): return _ascii_escaped_by_config(val, self.config) elif val is None or isinstance(val, (float, int, bool, complex)): return str(val) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 336b052fc..7e51da319 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -24,7 +24,6 @@ from typing import TypeVar from typing import Union import _pytest._code -from _pytest.compat import STRING_TYPES from _pytest.outcomes import fail @@ -721,15 +720,10 @@ def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase: elif ( hasattr(expected, "__getitem__") and isinstance(expected, Sized) - # Type ignored because the error is wrong -- not unreachable. - and not isinstance(expected, STRING_TYPES) # type: ignore[unreachable] + and not isinstance(expected, (str, bytes)) ): cls = ApproxSequenceLike - elif ( - isinstance(expected, Collection) - # Type ignored because the error is wrong -- not unreachable. - and not isinstance(expected, STRING_TYPES) # type: ignore[unreachable] - ): + elif isinstance(expected, Collection) and not isinstance(expected, (str, bytes)): msg = f"pytest.approx() only supports ordered sequences, but got: {expected!r}" raise TypeError(msg) else: