Skip to content

Commit

Permalink
Monkeypatch repr_callable in hypothesis plugin. Actually use
Browse files Browse the repository at this point in the history
repr_callable in __repr__s.
  • Loading branch information
jakkdl committed Jan 22, 2025
1 parent 685fe03 commit 8544351
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/trio/_core/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ def _hypothesis_plugin_setup() -> None: # pragma: no cover
_ALLOW_DETERMINISTIC_SCHEDULING = True # type: ignore
register_random(_r)

# monkeypatch repr_callable to make repr's way better
# requires importing hypothesis (in the test file or in conftest.py)
try:
from hypothesis.internal.reflection import get_pretty_function_description

import trio.testing._raises_group

def repr_callable(fun: Callable[[BaseException], bool]) -> str:
return "'" + get_pretty_function_description(fun) + "'"

Check failure on line 134 in src/trio/_core/_run.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.13, check formatting)

Mypy-Linux+Mac+Windows

src/trio/_core/_run.py:(134:13 - 134:67): Returning Any from function declared to return "str" [no-any-return]

trio.testing._raises_group.repr_callable = repr_callable
except ImportError:
pass


def _count_context_run_tb_frames() -> int:
"""Count implementation dependent traceback frames from Context.run()
Expand Down
13 changes: 9 additions & 4 deletions src/trio/testing/_raises_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ def _match_pattern(match: Pattern[str]) -> str | Pattern[str]:
return match.pattern if match.flags == _REGEX_NO_FLAGS else match


def repr_callable(fun: Callable[[BaseExcT_1], bool]) -> str:
def repr_callable(fun: Callable[[BaseException], bool]) -> str:
"""Get the repr of a ``check`` parameter.
Split out so it can be monkeypatched (e.g. by hypothesis)
Split out so it can be monkeypatched (e.g. by our hypothesis plugin)
"""
return repr(fun)

Expand Down Expand Up @@ -275,6 +275,8 @@ class Matcher(AbstractMatcher[MatchE]):
with RaisesGroups(Matcher(check=lambda x: type(x) is ValueError)):
...
Tip: if you install ``hypothesis`` and import it in ``conftest.py`` you will get
readable ``repr``s of ``check`` callables in the output.
"""

# At least one of the three parameters must be passed.
Expand Down Expand Up @@ -353,7 +355,7 @@ def __repr__(self) -> str:
f"match={_match_pattern(self.match)!r}",
)
if self.check is not None:
parameters.append(f"check={self.check!r}")
parameters.append(f"check={repr_callable(self.check)}")

Check failure on line 358 in src/trio/testing/_raises_group.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.13, check formatting)

Mypy-Linux+Mac+Windows

src/trio/testing/_raises_group.py:(358:54 - 358:63): Argument 1 to "repr_callable" has incompatible type "Callable[[MatchE], bool]"; expected "Callable[[BaseException], bool]" [arg-type]
return f'Matcher({", ".join(parameters)})'

def _check_type(self, exception: BaseException) -> TypeGuard[MatchE]:
Expand Down Expand Up @@ -413,6 +415,9 @@ class RaisesGroup(AbstractMatcher[BaseExceptionGroup[BaseExcT_co]]):
even though it generally does not care about the order of the exceptions in the group.
To avoid the above you should specify the first ValueError with a Matcher as well.
Tip: if you install ``hypothesis`` and import it in ``conftest.py`` you will get
readable ``repr``s of ``check`` callables in the output.
"""

# allow_unwrapped=True requires: singular exception, exception not being
Expand Down Expand Up @@ -623,7 +628,7 @@ def __repr__(self) -> str:
# If no flags were specified, discard the redundant re.compile() here.
reqs.append(f"match={_match_pattern(self.match)!r}")
if self.check is not None:
reqs.append(f"check={self.check!r}")
reqs.append(f"check={repr_callable(self.check)}")

Check failure on line 631 in src/trio/testing/_raises_group.py

View workflow job for this annotation

GitHub Actions / Ubuntu (3.13, check formatting)

Mypy-Linux+Mac+Windows

src/trio/testing/_raises_group.py:(631:48 - 631:57): Argument 1 to "repr_callable" has incompatible type "Callable[[BaseExceptionGroup[BaseExcT_co]], bool]"; expected "Callable[[BaseException], bool]" [arg-type]
return f"RaisesGroup({', '.join(reqs)})"

def _unroll_exceptions(
Expand Down

0 comments on commit 8544351

Please sign in to comment.