Skip to content

Commit 99e8129

Browse files
committed
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.
1 parent 20b18f0 commit 99e8129

File tree

3 files changed

+3
-13
lines changed

3 files changed

+3
-13
lines changed

src/_pytest/compat.py

-3
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@ def _translate_non_printable(s: str) -> str:
181181
return s.translate(_non_printable_ascii_translate_table)
182182

183183

184-
STRING_TYPES = bytes, str
185-
186-
187184
def _bytes_to_ascii(val: bytes) -> str:
188185
return val.decode("ascii", "backslashreplace")
189186

src/_pytest/python.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
from _pytest.compat import NOTSET
5151
from _pytest.compat import safe_getattr
5252
from _pytest.compat import safe_isclass
53-
from _pytest.compat import STRING_TYPES
5453
from _pytest.config import Config
5554
from _pytest.config import ExitCode
5655
from _pytest.config import hookimpl
@@ -998,7 +997,7 @@ def _idval_from_hook(self, val: object, argname: str) -> Optional[str]:
998997
def _idval_from_value(self, val: object) -> Optional[str]:
999998
"""Try to make an ID for a parameter in a ParameterSet from its value,
1000999
if the value type is supported."""
1001-
if isinstance(val, STRING_TYPES):
1000+
if isinstance(val, (str, bytes)):
10021001
return _ascii_escaped_by_config(val, self.config)
10031002
elif val is None or isinstance(val, (float, int, bool, complex)):
10041003
return str(val)

src/_pytest/python_api.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from typing import Union
2525

2626
import _pytest._code
27-
from _pytest.compat import STRING_TYPES
2827
from _pytest.outcomes import fail
2928

3029

@@ -721,15 +720,10 @@ def approx(expected, rel=None, abs=None, nan_ok: bool = False) -> ApproxBase:
721720
elif (
722721
hasattr(expected, "__getitem__")
723722
and isinstance(expected, Sized)
724-
# Type ignored because the error is wrong -- not unreachable.
725-
and not isinstance(expected, STRING_TYPES) # type: ignore[unreachable]
723+
and not isinstance(expected, (str, bytes))
726724
):
727725
cls = ApproxSequenceLike
728-
elif (
729-
isinstance(expected, Collection)
730-
# Type ignored because the error is wrong -- not unreachable.
731-
and not isinstance(expected, STRING_TYPES) # type: ignore[unreachable]
732-
):
726+
elif isinstance(expected, Collection) and not isinstance(expected, (str, bytes)):
733727
msg = f"pytest.approx() only supports ordered sequences, but got: {expected!r}"
734728
raise TypeError(msg)
735729
else:

0 commit comments

Comments
 (0)