Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncio_mode=auto does not mark static methods #292

Closed
kriek opened this issue Feb 15, 2022 · 5 comments · Fixed by #295
Closed

asyncio_mode=auto does not mark static methods #292

kriek opened this issue Feb 15, 2022 · 5 comments · Fixed by #295

Comments

@kriek
Copy link
Contributor

kriek commented Feb 15, 2022

Hi there,

I've migrated from 0.17.2 to 0.18.1 and activated auto asyncio_mode. It seems the mode is not able to apply asyncio marker to static methods.

The following minimized example:

class TestClass:
    @staticmethod
    async def test_static_method():
        await asyncio.sleep(0)

Fails as follows:

$ pytest test_static_method.py
============================================================================ test session starts =============================================================================
platform win32 -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\git\pytest-asyncio, configfile: pytest.ini
plugins: asyncio-0.18.1
asyncio: mode=auto
collected 1 item

test_static_method.py s                                                                                                                                                 [100%]

============================================================================== warnings summary ==============================================================================
test_static_method.py::TestClass::test_static_method
  c:\users\kasyc\appdata\local\programs\python\python38\lib\site-packages\_pytest\python.py:173: PytestUnhandledCoroutineWarning: async def functions are not natively supporte
d and have been skipped.
  You need to install a suitable plugin for your async framework, for example:
    - anyio
    - pytest-asyncio
    - pytest-tornasync
    - pytest-trio
    - pytest-twisted
    warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))

-- Docs: https://docs.pytest.org/en/stable/warnings.html
======================================================================= 1 skipped, 1 warning in 0.09s ========================================================================

test_auto_mode.py could easily be extended to cover that extra use case but I am not sure how to adapt pytest_pycollect_makeitem to fix the problem.

As a side note, with 0.17.2, we were doing our own auto mode with the following code and it was working for functions, methods and static methods:

def pytest_collection_modifyitems(items):
    for item in items:
        if isinstance(item, pytest.Function) and inspect.iscoroutinefunction(
            item.function
        ):
            item.add_marker("asyncio")
@seifertm
Copy link
Contributor

Thanks for letting us know! In fact, this has already been reported in #265. We decided to close that issue, because we thought the majority of users can opt to not use static test methods.

Is there any benefit of decorating tests with @staticmethod?

@kriek
Copy link
Contributor Author

kriek commented Feb 15, 2022

We have expensive pytest fixtures and we are using test classes to make use of "class" fixture scope. With it, we can control precisely when the teardown code is executed inside a test module.
We don't have attributes defined for our classes and using staticmethod is a way to mute the no-self-use pylint warning. We could alternatively have disabled it.

I wouldn't mind if the problem is not fixed, we added an explicit asyncio mark to affected staticmethod tests.

From what I investigated when reporting the issue, while obj is of type <class 'staticmethod'> (as you pointed out in #265), once the item is resolved via item = pytest.Function.from_parent(collector, name=name), inspect.iscoroutinefunction(item.function) == True.

Edit: fixed code sample, inspect.iscoroutinefunction() is to be called on item.function.

@asvetlov
Copy link
Contributor

@kriek would you propose a patch?

@kriek
Copy link
Contributor Author

kriek commented Feb 17, 2022

I will give it a try.

@kriek
Copy link
Contributor Author

kriek commented Feb 17, 2022

PR 295 created. I found the staticmethod unwrapping code by inspecting pytest sources.

clrpackages pushed a commit to clearlinux-pkgs/pypi-pytest_asyncio that referenced this issue Mar 7, 2022
… to version 0.18.2

Andrew Svetlov (1):
      Update changelog

Michael Seifert (2):
      fix: Add positional arguments to the synchronous test function wrapper. (#303)
      docs: Prepare release 0.18.2. (#304)

dependabot[bot] (2):
      Bump actions/setup-python from 2 to 3 (#300)
      Bump actions/checkout from 2 to 3 (#301)

kriek (1):
      Fix asyncio auto mode not marking static methods (closes pytest-dev/pytest-asyncio#265, closes pytest-dev/pytest-asyncio#292).  (#295)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants