Skip to content

Commit

Permalink
fix tests for strict exception groups (#42)
Browse files Browse the repository at this point in the history
* fix tests to work with strict and non-strict exception groups
* add python 3.12 to build, drop python 3.7 (end of life)
* upgrade dev dependencies where needed for newer python versions

---------

Co-authored-by: John Belmonte <[email protected]>
  • Loading branch information
VincentVanlaer and belm0 committed Apr 7, 2024
1 parent 36bab0d commit eeecd24
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 30 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ jobs:
build_and_test_pinned:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ['3.7', '3.8', '3.9', '3.10'] # 'pypy-3.7'
python-version: ['3.8', '3.9', '3.10', '3.11'] # 'pypy-3.7'
steps:
- uses: actions/checkout@v3
- name: Setup Python
Expand All @@ -31,7 +32,7 @@ jobs:
# macos-latest disabled due to unexplained timeout
# https://github.com/python-trio/purerpc/issues/39
os: [ubuntu-latest] # TODO: windows-latest
python-version: ['3.11']
python-version: ['3.12']
steps:
- uses: actions/checkout@v3
- name: Setup Python
Expand Down
48 changes: 23 additions & 25 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is autogenerated by pip-compile with python 3.7
# To update, run:
# This file is autogenerated by pip-compile with Python 3.8
# by the following command:
#
# pip-compile --extra=dev --output-file=requirements_test.txt setup.py
#
Expand All @@ -15,17 +15,21 @@ attrs==21.4.0
# outcome
# pytest
# trio
build==1.2.1
# via pip-tools
cffi==1.15.0
# via cryptography
click==8.1.2
# via pip-tools
cryptography==36.0.2
# via trustme
grpcio==1.44.0
exceptiongroup==1.2.0 ; python_version < "3.11"
# via purerpc (setup.py)
grpcio==1.62.1
# via
# grpcio-tools
# purerpc (setup.py)
grpcio-tools==1.44.0
grpcio-tools==1.62.1
# via purerpc (setup.py)
h2==3.2.0
# via purerpc (setup.py)
Expand All @@ -39,37 +43,35 @@ idna==3.3
# trio
# trustme
importlib-metadata==4.11.3
# via
# click
# pep517
# pluggy
# pytest
# via build
iniconfig==1.1.1
# via pytest
outcome==1.1.0
# via trio
packaging==21.3
# via pytest
pep517==0.12.0
# via pip-tools
pip-tools==6.6.0
# via
# build
# pytest
pip-tools==7.4.1
# via purerpc (setup.py)
pluggy==1.0.0
# via pytest
protobuf==3.20.0
protobuf==4.25.3
# via grpcio-tools
py==1.11.0
# via pytest
pycparser==2.21
# via cffi
pyparsing==3.0.8
# via packaging
pyproject-hooks==1.0.0
# via
# build
# pip-tools
pytest==7.1.1
# via purerpc (setup.py)
python-forge==18.6.0
# via purerpc (setup.py)
six==1.16.0
# via grpcio
sniffio==1.2.0
# via
# anyio
Expand All @@ -80,24 +82,20 @@ tblib==1.7.0
# via purerpc (setup.py)
tomli==2.0.1
# via
# pep517
# build
# pip-tools
# pyproject-hooks
# pytest
trio==0.20.0
# via purerpc (setup.py)
trustme==0.9.0
# via purerpc (setup.py)
typing-extensions==4.1.1
# via
# anyio
# importlib-metadata
uvloop==0.16.0 ; platform_system != "Windows"
uvloop==0.19.0 ; platform_system != "Windows"
# via purerpc (setup.py)
wheel==0.37.1
# via pip-tools
zipp==3.8.0
# via
# importlib-metadata
# pep517
# via importlib-metadata

# The following packages are considered to be unsafe in a requirements file:
# pip
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def main():
"Operating System :: POSIX :: Linux",
"Programming Language :: Python",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Framework :: AsyncIO",
Expand Down Expand Up @@ -77,6 +77,7 @@ def main():
"grpcio-tools<=1.26",
],
'dev': [
"exceptiongroup; python_version<'3.11'",
"pytest",
"grpcio",
"grpcio-tools",
Expand Down
31 changes: 31 additions & 0 deletions tests/exceptiongroups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Iterable
from contextlib import contextmanager
import sys

if sys.version_info < (3, 11):
from exceptiongroup import ExceptionGroup


def _unroll_exceptions(
exceptions: Iterable[Exception]
) -> Iterable[Exception]:
res: list[Exception] = []
for exc in exceptions:
if isinstance(exc, ExceptionGroup):
res.extend(_unroll_exceptions(exc.exceptions))

else:
res.append(exc)
return res


@contextmanager
def unwrap_exceptiongroups_single():
try:
yield
except ExceptionGroup as e:
exceptions = _unroll_exceptions(e.exceptions)

assert len(exceptions) == 1, "Exception group contains multiple exceptions"

raise exceptions[0]
3 changes: 2 additions & 1 deletion tests/test_echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import purerpc
from purerpc.test_utils import run_purerpc_service_in_process, run_grpc_service_in_process, \
async_iterable_to_list, random_payload, grpc_client_parallelize, purerpc_channel, purerpc_client_parallelize, grpc_channel
from .exceptiongroups import unwrap_exceptiongroups_single

pytestmark = pytest.mark.anyio

Expand Down Expand Up @@ -200,7 +201,7 @@ async def test_purerpc_client_disconnect(echo_pb2, echo_grpc):
port = await tg.start(server.serve_async)

# client
with pytest.raises(anyio.ClosedResourceError):
with pytest.raises(anyio.ClosedResourceError), unwrap_exceptiongroups_single():
async with purerpc.insecure_channel("localhost", port) as channel:
stub = echo_grpc.EchoStub(channel)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import purerpc
from purerpc.test_utils import run_purerpc_service_in_process, run_grpc_service_in_process, grpc_channel, \
grpc_client_parallelize, purerpc_channel
from .exceptiongroups import unwrap_exceptiongroups_single

pytestmark = pytest.mark.anyio

Expand Down Expand Up @@ -88,7 +89,7 @@ async def generator():

stub = greeter_grpc.GreeterStub(channel)

with pytest.raises(ValueError, match="oops"):
with pytest.raises(ValueError, match="oops"), unwrap_exceptiongroups_single():
async with stub.SayHelloToMany(generator()) as aiter:
async for resp in aiter:
if resp.message == "2":
Expand Down

0 comments on commit eeecd24

Please sign in to comment.