aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
15.13k stars 2.02k forks source link

pytest example in testing.rst throws TypeError: 'async_generator' object is not callable #8709

Closed EdwardBetts closed 2 months ago

EdwardBetts commented 2 months ago

Describe the bug

I tried running the example client = await aiohttp_client(app) pytest example from testing.rst, but it doesn't work.

To Reproduce

Run pytest client example from testing.rst

Expected behavior

Test passes

Logs/tracebacks

$ python3 -mpytest --asyncio-mode=auto test_foo.py 
============================================================================================== test session starts ===============================================================================================
platform linux -- Python 3.12.5, pytest-8.3.2, pluggy-1.5.0
benchmark: 4.0.0 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /home/edward/scratch/aiohttp_tests
plugins: remotedata-0.4.1, astropy-header-0.2.2, cov-5.0.0, console-scripts-1.4.1, time-machine-2.13.0, requests_mock-1.12.1, anyio-4.4.0, openfiles-0.6.0, forked-1.6.0, typeguard-4.3.0, mock-3.14.0, astropy-0.11.0, hypothesis-6.105.1, kgb-7.1.1, asyncio-0.20.3, arraydiff-0.6.1, twisted-1.14.1, django-4.5.2, syrupy-4.6.1, flaky-3.8.1, timeout-2.3.1, xdist-3.6.1, tornasync-0.6.0.post2, trio-0.8.0, aiohttp-1.0.5, doctestplus-1.2.1, Faker-26.0.0, filter-subpackage-0.2.0, benchmark-4.0.0, pylama-8.4.1, repeat-0.9.3, httpx-0.30.0
asyncio: mode=Mode.AUTO
collected 1 item                                                                                                                                                                                                 

test_foo.py F                                                                                                                                                                                              [100%]

==================================================================================================== FAILURES ====================================================================================================
___________________________________________________________________________________________________ test_hello ___________________________________________________________________________________________________

aiohttp_client = <async_generator object aiohttp_client at 0x7ff007969a40>, loop = <_UnixSelectorEventLoop running=False closed=False debug=False>

    async def test_hello(aiohttp_client, loop):
        app = web.Application()
        app.router.add_get('/', hello)
>       client = await aiohttp_client(app)
E       TypeError: 'async_generator' object is not callable

test_foo.py:9: TypeError
================================================================================================ warnings summary ================================================================================================
test_foo.py::test_hello
  /usr/lib/python3/dist-packages/pytest_aiohttp/plugin.py:33: DeprecationWarning: 'loop' fixture is deprecated and scheduled for removal, please use 'event_loop' instead
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============================================================================================ short test summary info =============================================================================================
FAILED test_foo.py::test_hello - TypeError: 'async_generator' object is not callable
========================================================================================== 1 failed, 1 warning in 0.07s ==========================================================================================
$

Python Version

Python 3.12.5

aiohttp Version

Name: aiohttp
Version: 3.9.5
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: 
Author-email: 
License: Apache 2
Location: /usr/lib/python3/dist-packages
Requires: aiosignal, attrs, frozenlist, multidict, yarl
Required-by: aioairzone, aiohttp-retry, airgradient, blinkpy, millheater, tplink_omada_client, yalexs, yolink-api

multidict Version

Name: multidict
Version: 6.0.4
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /usr/lib/python3/dist-packages
Requires: 
Required-by: aiohttp, yarl

yarl Version

Name: yarl
Version: 1.9.4
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache-2.0
Location: /usr/lib/python3/dist-packages
Requires: idna, multidict
Required-by: aiohttp, airgradient

OS

Debian GNU/Linux

Related component

Client

Additional context

No response

Code of Conduct

Dreamsorcerer commented 2 months ago

This works fine for me (I need to remove the loop fixture from the docs, which triggers a DeprecationWarning):

from aiohttp import web

async def hello(request):
    return web.Response(text='Hello, world')

async def test_hello(aiohttp_client):
    app = web.Application()
    app.router.add_get('/', hello)
    client = await aiohttp_client(app)
    resp = await client.get('/')
    assert resp.status == 200
    text = await resp.text()
    assert 'Hello, world' in text
> pytest --asyncio-mode=auto test_server.py
Test session starts (platform: linux, Python 3.10.12, pytest 8.3.2, pytest-sugar 0.9.7)
rootdir: /home/ubuntu/desktop
plugins: timeout-2.1.0, forked-1.4.0, anyio-3.6.2, sugar-0.9.7, mock-3.11.1, cov-4.1.0, toolbox-0.4, metadata-3.0.0, jest-0.3.0, aresponses-3.0.0, asyncio-0.23.8, aiohttp-1.0.5, web3-6.15.1
asyncio: mode=auto
collected 1 item                                                                                                                                                                                                    

 test_server.py ✓                                                                                                                                                                                     100% ██████████

Results (0.16s):
       1 passed

Note that like your other bug report, I have significantly less plugins than you. Maybe you should start by uninstalling a load of old, broken plugins?

EdwardBetts commented 2 months ago

Thanks for your help. Sorry for the bad bug report.