Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
30a7a40
Add RaisesGroup, a helper for catching ExceptionGroups in tests
jakkdl Dec 6, 2023
0d08e19
fix some CI test errors, now only some grumpy export tests left
jakkdl Dec 6, 2023
8f1e221
clarify mixed loose test
jakkdl Dec 6, 2023
f7ec079
rename variable to fit new class name
jakkdl Dec 6, 2023
c7c6264
ignore RaisesGroup in test_exports for now
jakkdl Dec 9, 2023
49af03d
fix test_export fail
jakkdl Dec 9, 2023
3f63030
fix pyright --verifytypes errors
jakkdl Dec 9, 2023
3adc49a
un-unittest the tests
jakkdl Dec 9, 2023
cd9d3a5
add test for _ExceptionInfo (and fix it)
jakkdl Dec 9, 2023
e9688be
rewrite not to use any in assert, since codecov doesn't like it
jakkdl Dec 9, 2023
97fb79b
* Split out type tests
jakkdl Dec 10, 2023
5f0298b
Merge remote-tracking branch 'origin/master' into raisesgroup
jakkdl Dec 16, 2023
faaebf5
rewrite another test to use RaisesGroup
jakkdl Dec 16, 2023
22d8b5a
add new classes to docs
jakkdl Dec 22, 2023
32d079a
Merge remote-tracking branch 'origin/master' into raisesgroup
jakkdl Dec 22, 2023
43a51b6
Fix ruff issues
CoolCat467 Dec 22, 2023
3cc15e6
add fix for TracebackType
jakkdl Dec 22, 2023
efbccbc
cover __str__ of Matcher with no type specified
jakkdl Dec 22, 2023
1261dc1
properly "fix" sphinx+TracebackType
jakkdl Dec 22, 2023
ccdb79d
add newsfragment
jakkdl Dec 22, 2023
7956848
fix url in newsfragment
jakkdl Dec 23, 2023
d6d7595
Merge branch 'master' into raisesgroup
CoolCat467 Dec 28, 2023
4990a7d
Add overloads to Matcher() to precisely check init parameters
TeamSpen210 Dec 29, 2023
86287a5
Pre-compile strings passed to Matcher(), but unwrap in __str__
TeamSpen210 Dec 29, 2023
663c12b
update comment, add pyright: ignore
jakkdl Dec 30, 2023
148df67
fix formatting, move matcher_tostring test to test_testing_raisesgroup
jakkdl Dec 30, 2023
c8f7983
add docstrings
jakkdl Jan 3, 2024
9b66187
Apply suggestions from code review
jakkdl Jan 5, 2024
d020655
add comments
jakkdl Jan 5, 2024
957b4fd
switch to tell type checkers that we always use _ExceptionInfo. Add n…
jakkdl Jan 5, 2024
614f85a
fix broken test
jakkdl Jan 5, 2024
80b7031
fix newsfragment quoting of pytest
jakkdl Jan 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/trio/_tests/type_tests/raisesgroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""
from __future__ import annotations

import re
import sys
from typing import Union

Expand Down Expand Up @@ -92,6 +93,19 @@ def check_matcher_transparent() -> None:
assert_type(e.value, BaseExceptionGroup[ValueError])


def check_matcher_tostring() -> None:
assert str(Matcher(ValueError)) == "Matcher(ValueError)"
assert str(Matcher(match="[a-z]")) == "Matcher(match='[a-z]')"
pattern_no_flags = re.compile("noflag", 0)
assert str(Matcher(match=pattern_no_flags)) == "Matcher(match='noflag')"
pattern_flags = re.compile("noflag", re.IGNORECASE)
assert str(Matcher(match=pattern_flags)) == f"Matcher(match={pattern_flags!r})"
assert (
str(Matcher(ValueError, match="re", check=bool))
== f"Matcher(ValueError, match='re', check={bool!r})"
)


def check_nested_raisesgroups_contextmanager() -> None:
with RaisesGroup(RaisesGroup(ValueError)) as excinfo:
raise ExceptionGroup("foo", (ValueError(),))
Expand Down
20 changes: 16 additions & 4 deletions src/trio/testing/_raises_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def _stringify_exception(exc: BaseException) -> str:
)


# String patterns default to including the unicode flag.
_regex_no_flags = re.compile("").flags


@final
class Matcher(Generic[E]):
# At least one of the three parameters must be passed.
Expand Down Expand Up @@ -134,7 +138,11 @@ def __init__(
f"exception_type {exception_type} must be a subclass of BaseException"
)
self.exception_type = exception_type
self.match = match
self.match: Pattern[str] | None
if isinstance(match, str):
self.match = re.compile(match)
else:
self.match = match
self.check = check

def matches(self, exception: BaseException) -> TypeGuard[E]:
Expand All @@ -156,9 +164,13 @@ def __str__(self) -> str:
reqs = []
if self.exception_type is not None:
reqs.append(self.exception_type.__name__)
for req, attr in (("match", self.match), ("check", self.check)):
if attr is not None:
reqs.append(f"{req}={attr!r}")
if (match := self.match) is not None:
# If no flags were specified, discard the redundant re.compile() here.
reqs.append(
f"match={match.pattern if match.flags == _regex_no_flags else match!r}"
)
if self.check is not None:
reqs.append(f"check={self.check!r}")
return f'Matcher({", ".join(reqs)})'


Expand Down