GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 0.19.0: Fixture misses attribute · Issue #390 · pytest-dev/pytest-asyncio

0.19.0: Fixture misses attribute #390

New issue
Copy link
New issue
Copy link
Closed
Closed
0.19.0: Fixture misses attribute#390
Copy link

Description

@viralmutant
viralmutant
opened on Jul 22, 2022
Issue body actions

I am working with python3.10 and pytest=7.1.2 version

It seems with the latest version of pytest-asyncio=0.19.0 a regression has been introduced and this bug is back.

Using the same example testcase to reproduce the issue, I observe the same failure

AttributeError: 'async_generator' object has no attribute 'put'

Downgrading pytest-asyncio to version 0.18.3 and the error is gone.

Reacted by Houman

Activity

  1. davidandreoletti commented on Jul 22, 2022

    @davidandreoletti
    davidandreoletti
    on Jul 22, 2022
    More actions

    @viralmutant

    pytest-asyncio=0.19.0 enforces strict mode. Projects must update their fixture markers as described.

    Env: py38 + purest=7.1.2 +pytest-asyncio=0.19.0

    Reacted by Michael Seifert
  2. graingert commented on Jul 22, 2022

    @graingert
    graingert
    on Jul 22, 2022
    Member
    More actions

    @viralmutant please show the output of the code when you run on python3.10 and pytest-asyncio==0.18.3

    Reacted by Michael Seifert
  3. viralmutant commented on Jul 22, 2022

    @viralmutant
    viralmutant
    on Jul 22, 2022
    Author
    More actions

    @viralmutant please show the output of the code when you run on python3.10 and pytest-asyncio==0.18.3

    Here is the output with -s

    plugins: forked-1.4.0, cov-3.0.0, asyncio-0.18.3, env-0.6.2, sugar-0.9.5, requests-mock-1.9.3, pyfakefs-4.5.0, xdist-2.5.0
    asyncio: mode=legacy
    collecting ...
     test_bug.py ✓
    
    /CodeBox/turbo-test/lib/python3.10/site-packages/pytest_asyncio/plugin.py:191: DeprecationWarning: The 'asyncio_mode' default value will change to 'strict' in future, please explicitly use 'asyncio_mode=strict' or 'asyncio_mode=auto' in pytest configuration file.
        config.issue_config_time_warning(LEGACY_MODE, stacklevel=2)
    
    test_bug.py:12
      
    /CodeBox/turbo/test_bug.py:12: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
        def release(self):
    
    ../turbo-test/lib/python3.10/site-packages/pytest_asyncio/plugin.py:230
      .../CodeBox/turbo-test/lib/python3.10/site-packages/pytest_asyncio/plugin.py:230: DeprecationWarning: '@pytest.fixture' is applied to <fixture fake_session, file=/CodeBox/turbo/test_bug.py, line=16> in 'legacy' mode, please replace it with '@pytest_asyncio.fixture' as a preparation for switching to 'strict' mode (or use 'auto' mode to seamlessly handle all these fixtures as asyncio-driven).
        warnings.warn(
    
    test_bug.py::test_bug
      /CodeBox/turbo/test_bug.py:20: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
        def _fake_request(method, url, *args, **kwargs):
    
    -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
    
    Results (0.84s):
           1 passed
  4. seifertm commented on Jul 23, 2022

    @seifertm
    seifertm
    on Jul 23, 2022
    Contributor
    More actions

    Your test output shows a deprecation warning about asyncio-mode. Starting with pytest-asyncio v0.19 asyncio mode defaults to strict. The error you're seeing is likely caused by wrong fixture decorators. The reproducible example you linked suffers from that exact problem.

    As @davidandreoletti already mentioned you need to update your fixture definitions if you intend to use strict mode. Strict mode will no longer evaluate async fixtures decorated witih @pytest.fixture. You need to use @pytest_asyncio.fixture.

    @viralmutant Please check your fixture definitions and let us know if this fixes your issue.

    Reacted by André Carvalho and Igor Chesnykh
  5. viralmutant commented on Jul 26, 2022

    @viralmutant
    viralmutant
    on Jul 26, 2022
    Author
    More actions

    Yes, after replacing the fixture as @pytest_asyncio.fixture I didn't observe the issue with v0.19
    Thanks

    The confusing part is that once the version has been upgraded, it just fails and won't tell why. And while being at the earlier version, when a couple of thousand tests are running in CI pipeline, the warnings are ignored

    Reacted by David Andreoletti, Houman and Takos
  6. kiddten commented on Aug 17, 2022

    @kiddten
    kiddten
    on Aug 17, 2022
    More actions

    Hi @seifertm

    I am trying to use async_generator with fixture in factory mode:

    @pytest_asyncio.fixture
    async def async_client_factory():
        async def _factory(x):
            print(x) # just to simplify case, we need x as parameter for our factory
            async with AsyncClient() as ac:
                yield ac
    
        return _factory
    
    @pytest.mark.asyncio
    async def test_async_generator_factory(async_client_factory):
        async_client = async_client_factory(1)
        response = await async_client.get("/test")
        assert response.status_code == status.HTTP_200_OK

    But I get same error:

    >       response = await async_client.get("/test")
    E       AttributeError: 'async_generator' object has no attribute 'get'
    

    package versions:

    pytest-asyncio = "0.19.0"
    httpx = "^0.23.0"
    pytest = "^7.1.2"
    
  7. kiddten commented on Aug 17, 2022

    @kiddten
    kiddten
    on Aug 17, 2022
    More actions

    just a fixture w/o factory works well

    @pytest_asyncio.fixture
    async def async_client():
        async with AsyncClient() as ac:
            yield ac
    
    @pytest.mark.asyncio
    async def test_async_generator(async_client):
        response = await async_client.get("/test")
        assert response.status_code == status.HTTP_200_OK
  8. seifertm commented on Aug 17, 2022

    @seifertm
    seifertm
    on Aug 17, 2022
    Contributor
    More actions

    Interesting, thanks for the report. I'll look into it tomorrow.

  9. graingert commented on Aug 17, 2022

    @graingert
    graingert
    on Aug 17, 2022
    Member
    More actions

    You can't use a yield inside an httpx.AsyncClient like that, you need the @contextlib.asynccontextmanager

    See https://discuss.python.org/t/preventing-yield-inside-certain-context-managers/1091

  10. kiddten commented on Aug 18, 2022

    @kiddten
    kiddten
    on Aug 18, 2022
    More actions

    @graingert it does not help in case with factory

    @pytest_asyncio.fixture
    async def async_client_factory():
        @asynccontextmanager
        async def _factory(x):
            print(x) # just to simplify case, we need x as parameter for our factory
            async with AsyncClient() as ac:
                yield ac
    
        return _factory
    
    @pytest.mark.asyncio
    async def test_async_generator_factory(async_client_factory):
        async_client = async_client_factory(1)
        response = await async_client.get("/test")
        assert response.status_code == status.HTTP_200_OK

    similar error

    >       response = await async_client.get("/test")
    E       AttributeError: '_AsyncGeneratorContextManager' object has no attribute 'get'
    

    although it works with fixture w/o factory

    @asynccontextmanager
    @pytest_asyncio.fixture
    async def async_client():
        async with AsyncClient() as ac:
            yield ac
    
    @pytest.mark.asyncio
    async def test_async_generator(async_client):
        response = await async_client.get("/test")
        assert response.status_code == status.HTTP_200_OK

    seems something related to pytest internals

  11. graingert commented on Aug 18, 2022

    @graingert
    graingert
    on Aug 18, 2022
    Member
    More actions

    You need to use

    async with async_client_factory(1) as async_client:
        ...
    Reacted by kiddten and Michael Seifert
  12. seifertm commented on Aug 25, 2022

    @seifertm
    seifertm
    on Aug 25, 2022
    Contributor
    More actions

    @kiddick I cannot see anything wrong with the behaviour of pytest-asyncio in the example you provided here. Your fixture returns the _factory async context manager. As Thomas mentioned you need to open the context manager using asnyc with …, in order to access the yielded AsyncClient.

    From your reaction on the previous comment I assume the issue is resolved for you.

    I initially left this issue open, because I intended to add a warning when people forgot to update their fixtures. I don't think there's a reliable way to do it, though, so I'll close this issue.

  13. seifertm
    closed this as completedon Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.