Skip to content

Commit

Permalink
Merge pull request pytest-dev#11915 from bluetech/compat-string-types
Browse files Browse the repository at this point in the history
compat: a couple of minor cleanups
  • Loading branch information
bluetech authored Feb 4, 2024
2 parents 20b18f0 + 3ba4095 commit e28f35c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 25 deletions.
18 changes: 3 additions & 15 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,25 +177,13 @@ def get_default_arg_names(function: Callable[..., Any]) -> tuple[str, ...]:
)


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")


def ascii_escaped(val: bytes | str) -> str:
r"""If val is pure ASCII, return it as an str, otherwise, escape
bytes objects into a sequence of escaped bytes:
b'\xc3\xb4\xc5\xd6' -> r'\xc3\xb4\xc5\xd6'
and escapes unicode objects into a sequence of escaped unicode
ids, e.g.:
and escapes strings into a sequence of escaped unicode ids, e.g.:
r'4\nV\U00043efa\x0eMXWB\x1e\u3028\u15fd\xcd\U0007d944'
Expand All @@ -206,10 +194,10 @@ def ascii_escaped(val: bytes | str) -> str:
a UTF-8 string.
"""
if isinstance(val, bytes):
ret = _bytes_to_ascii(val)
ret = val.decode("ascii", "backslashreplace")
else:
ret = val.encode("unicode_escape").decode("ascii")
return _translate_non_printable(ret)
return ret.translate(_non_printable_ascii_translate_table)


@dataclasses.dataclass
Expand Down
3 changes: 1 addition & 2 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
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
Expand Down Expand Up @@ -998,7 +997,7 @@ def _idval_from_hook(self, val: object, argname: str) -> Optional[str]:
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)
Expand Down
10 changes: 2 additions & 8 deletions src/_pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from typing import Union

import _pytest._code
from _pytest.compat import STRING_TYPES
from _pytest.outcomes import fail


Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e28f35c

Please sign in to comment.