From 0e96f88704a940ed8eda7299a27f2c9dbb6fcf09 Mon Sep 17 00:00:00 2001 From: Vytautas Liuolia Date: Tue, 18 Jul 2023 13:41:57 +0200 Subject: [PATCH] chore(tests): adapt ASGI tests to Cython 3.0 (#2160) --- tests/asgi/_cythonized.pyx | 9 ++------ tests/asgi/test_cythonized_asgi.py | 36 +++++++++++++++++------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/tests/asgi/_cythonized.pyx b/tests/asgi/_cythonized.pyx index 5624ba611..091e07358 100644 --- a/tests/asgi/_cythonized.pyx +++ b/tests/asgi/_cythonized.pyx @@ -105,13 +105,8 @@ class TestResourceWithHooks: pass -class TestResourceWithHooksNoHintBefore: +class TestResourceWithHooksNoHint: @falcon.before(my_before_hook) - async def on_get(self, req, resp): - pass - - -class TestResourceWithHooksNoHintBefore: - @falcon.after(my_before_hook) + @falcon.after(my_after_hook) async def on_get(self, req, resp): pass diff --git a/tests/asgi/test_cythonized_asgi.py b/tests/asgi/test_cythonized_asgi.py index 710940359..744dcd952 100644 --- a/tests/asgi/test_cythonized_asgi.py +++ b/tests/asgi/test_cythonized_asgi.py @@ -34,6 +34,12 @@ from _util import disable_asgi_non_coroutine_wrapping # NOQA +# NOTE(vytas): Cython 3.0+ now correctly marks cythonized coroutines as such, +# however, the relevant protocol is only available in Python 3.10+. +# See also: https://github.com/cython/cython/pull/3427 +CYTHON_COROUTINE_HINT = sys.version_info >= (3, 10) + + @pytest.fixture def client(): return testing.TestClient(falcon.asgi.App()) @@ -79,12 +85,15 @@ def test_not_cython_func(func): @pytest.mark.skipif(not pyximport, reason='Cython not installed') def test_jsonchema_validator(client): with disable_asgi_non_coroutine_wrapping(): - client.app.add_route('/', _cythonized.TestResourceWithValidation()) + if CYTHON_COROUTINE_HINT: + client.app.add_route('/', _cythonized.TestResourceWithValidationNoHint()) + else: + with pytest.raises(TypeError): + client.app.add_route( + '/wowsuchfail', _cythonized.TestResourceWithValidationNoHint() + ) - with pytest.raises(TypeError): - client.app.add_route( - '/wowsuchfail', _cythonized.TestResourceWithValidationNoHint() - ) + client.app.add_route('/', _cythonized.TestResourceWithValidation()) client.simulate_get() @@ -101,13 +110,6 @@ def test_scheduled_jobs(client): @pytest.mark.skipif(not pyximport, reason='Cython not installed') -@pytest.mark.skipif( - sys.version_info < (3, 7), - reason=( - 'CPython 3.6 does not complain when you try to call loop.create_task() ' - 'with the wrong type.' - ), -) def test_scheduled_jobs_type_error(client): client.app.add_route( '/wowsuchfail', _cythonized.TestResourceWithScheduledJobsAsyncRequired() @@ -126,11 +128,13 @@ def test_scheduled_jobs_type_error(client): @pytest.mark.skipif(not pyximport, reason='Cython not installed') def test_hooks(client): with disable_asgi_non_coroutine_wrapping(): - with pytest.raises(TypeError): - client.app.add_route('/', _cythonized.TestResourceWithHooksNoHintBefore()) - client.app.add_route('/', _cythonized.TestResourceWithHooksNoHintAfter()) + if CYTHON_COROUTINE_HINT: + client.app.add_route('/', _cythonized.TestResourceWithHooksNoHint()) + else: + with pytest.raises(TypeError): + client.app.add_route('/', _cythonized.TestResourceWithHooksNoHint()) - client.app.add_route('/', _cythonized.TestResourceWithHooks()) + client.app.add_route('/', _cythonized.TestResourceWithHooks()) result = client.simulate_get() assert result.headers['x-answer'] == '42'