Skip to content

Commit 34d79ce

Browse files
jakkdlnicoddemus
authored andcommitted
fix type failures in raises.py and add tests
Fix typing failures after mypy 1.18 update.
1 parent de32d0f commit 34d79ce

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/_pytest/raises.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def _parse_exc(
452452
issubclass(origin_exc, BaseExceptionGroup)
453453
and exc_type in (BaseException, Any)
454454
):
455-
if not isinstance(exc, Exception):
455+
if not issubclass(origin_exc, ExceptionGroup):
456456
self.is_baseexception = True
457457
return cast(type[BaseExcT_1], origin_exc)
458458
else:
@@ -465,9 +465,9 @@ def _parse_exc(
465465
)
466466
# unclear if the Type/ValueError distinction is even helpful here
467467
msg = f"expected exception must be {expected}, not "
468-
if isinstance(exc, type):
468+
if isinstance(exc, type): # type: ignore[unreachable]
469469
raise ValueError(msg + f"{exc.__name__!r}")
470-
if isinstance(exc, BaseException):
470+
if isinstance(exc, BaseException): # type: ignore[unreachable]
471471
raise TypeError(msg + f"an exception instance ({type(exc).__name__})")
472472
raise TypeError(msg + repr(type(exc).__name__))
473473

testing/python/raises_group.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,16 @@ def test_annotated_group() -> None:
13251325
with RaisesExc(BaseExceptionGroup[BaseException]):
13261326
raise BaseExceptionGroup("", [KeyboardInterrupt()])
13271327

1328+
# assure AbstractRaises.is_baseexception is set properly
1329+
assert (
1330+
RaisesGroup(ExceptionGroup[Exception]).expected_type()
1331+
== "ExceptionGroup(ExceptionGroup)"
1332+
)
1333+
assert (
1334+
RaisesGroup(BaseExceptionGroup[BaseException]).expected_type()
1335+
== "BaseExceptionGroup(BaseExceptionGroup)"
1336+
)
1337+
13281338

13291339
def test_tuples() -> None:
13301340
# raises has historically supported one of several exceptions being raised

testing/test_monkeypatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def test_context() -> None:
417417
with monkeypatch.context() as m:
418418
m.setattr(functools, "partial", 3)
419419
assert not inspect.isclass(functools.partial)
420-
assert inspect.isclass(functools.partial) # type:ignore[unreachable]
420+
assert inspect.isclass(functools.partial)
421421

422422

423423
def test_context_classmethod() -> None:

0 commit comments

Comments
 (0)