Skip to content

Commit e28f35c

Browse files
authored
Merge pull request #11915 from bluetech/compat-string-types
compat: a couple of minor cleanups
2 parents 20b18f0 + 3ba4095 commit e28f35c

File tree

3 files changed

+6
-25
lines changed

3 files changed

+6
-25
lines changed

src/_pytest/compat.py

+3-15
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,13 @@ def get_default_arg_names(function: Callable[..., Any]) -> tuple[str, ...]:
177177
)
178178

179179

180-
def _translate_non_printable(s: str) -> str:
181-
return s.translate(_non_printable_ascii_translate_table)
182-
183-
184-
STRING_TYPES = bytes, str
185-
186-
187-
def _bytes_to_ascii(val: bytes) -> str:
188-
return val.decode("ascii", "backslashreplace")
189-
190-
191180
def ascii_escaped(val: bytes | str) -> str:
192181
r"""If val is pure ASCII, return it as an str, otherwise, escape
193182
bytes objects into a sequence of escaped bytes:
194183
195184
b'\xc3\xb4\xc5\xd6' -> r'\xc3\xb4\xc5\xd6'
196185
197-
and escapes unicode objects into a sequence of escaped unicode
198-
ids, e.g.:
186+
and escapes strings into a sequence of escaped unicode ids, e.g.:
199187
200188
r'4\nV\U00043efa\x0eMXWB\x1e\u3028\u15fd\xcd\U0007d944'
201189
@@ -206,10 +194,10 @@ def ascii_escaped(val: bytes | str) -> str:
206194
a UTF-8 string.
207195
"""
208196
if isinstance(val, bytes):
209-
ret = _bytes_to_ascii(val)
197+
ret = val.decode("ascii", "backslashreplace")
210198
else:
211199
ret = val.encode("unicode_escape").decode("ascii")
212-
return _translate_non_printable(ret)
200+
return ret.translate(_non_printable_ascii_translate_table)
213201

214202

215203
@dataclasses.dataclass

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)