Fixed import error when exceptiongroup isn't available#2231
Fixed import error when exceptiongroup isn't available#2231Kludex merged 7 commits intoKludex:masterfrom
Conversation
|
I also went ahead and updated the pinned AnyIO version as v4.0.0rc1 is out now. |
|
This would also need something like the following patch to fix the tests for anyio < 4 which #2228 raises. From 917d1c135635d6d9e5aec83633673ec21a529933 Mon Sep 17 00:00:00 2001
From: Steven Guikal <void@fluix.one>
Date: Sun, 30 Jul 2023 17:18:40 -0400
Subject: [PATCH] Fix wsgi test for AnyIO versions before 4.0.0
---
tests/middleware/test_wsgi.py | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/tests/middleware/test_wsgi.py b/tests/middleware/test_wsgi.py
index ad39754..5b2508c 100644
--- a/tests/middleware/test_wsgi.py
+++ b/tests/middleware/test_wsgi.py
@@ -2,6 +2,7 @@ import sys
import pytest
+from importlib.metadata import version
from starlette.middleware.wsgi import WSGIMiddleware, build_environ
if sys.version_info < (3, 11): # pragma: no cover
@@ -69,11 +70,15 @@ def test_wsgi_exception(test_client_factory):
# The HTTP protocol implementations would catch this error and return 500.
app = WSGIMiddleware(raise_exception)
client = test_client_factory(app)
- with pytest.raises(ExceptionGroup) as exc:
- client.get("/")
-
- assert len(exc.value.exceptions) == 1
- assert isinstance(exc.value.exceptions[0], RuntimeError)
+ if version("anyio") < "4":
+ with pytest.raises(RuntimeError):
+ client.get("/")
+ else:
+ with pytest.raises(ExceptionGroup) as exc:
+ client.get("/")
+
+ assert len(exc.value.exceptions) == 1
+ assert isinstance(exc.value.exceptions[0], RuntimeError)
def test_wsgi_exc_info(test_client_factory):
--
2.41.0 |
|
So do you want me to keep 4.0.0rc1 as the pinned AnyIO version? If so, how do I prove that it also passes on 3.x? |
|
I'm not a contributor so I'm not sure what compatibility guarantees are made or how tests are run. Applying my patch means distributions that have AnyIO < 4 in their repositories (aka everyone since 4.0.0 is only a pre-release) don't need to modify tests. |
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
|
Curious – the coverage fails even on AnyIO 3.7.1. Maybe the test changes themselves caused this? |
|
Closed by mistake. |
|
The only reason coverage fails here is that the code base is only tested against AnyIO 3 now, and tripping the missing line requires AnyIO 4. Should I add a Ideally we would test against both but that may complicate the test suite too much. |
|
Yes, add the pragma, please. |
Fixes #2227. Closes #2229. This is an alternate version of the linked PR.
Summary
Last minute changes to the AnyIO 4.0 compatibility PR inadvertently caused an
ImportErroron Pythons earlier than 3.11 when theexceptiongroupback-port wasn't installed. This makes the import optional and thus restores compatibility with AnyIO 3.x.Checklist