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

Hypothesis tests that are class members are passed self twice #700

Open
2 tasks done
jdpage opened this issue Mar 14, 2024 · 1 comment
Open
2 tasks done

Hypothesis tests that are class members are passed self twice #700

jdpage opened this issue Mar 14, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@jdpage
Copy link

jdpage commented Mar 14, 2024

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

AnyIO version

4.3.0

Python version

3.12.2

What happened?

When running a hypothesis test that is a class member, the test runner fails with a TypeError, citing multiple values for self being passed.

How can we reproduce the bug?

The following script reproduces the issue, using pytest 8.1.1 and hypothesis 6.99.5:

from hypothesis import strategies as st, given
import pytest


class TestSomething:
    @given(x=st.just(1))
    @pytest.mark.anyio
    async def test_foo(self, x):
        assert x == 1

The following output is produced under pytest:

================================================= test session starts ==================================================
platform linux -- Python 3.12.2, pytest-8.1.1, pluggy-1.4.0
rootdir: /home/thyme/src/scratch/repro
plugins: hypothesis-6.99.5, anyio-4.3.0
collected 2 items                                                                                                      

ex.py FF                                                                                                         [100%]

======================================================= FAILURES =======================================================
___________________________________________ TestSomething.test_foo[asyncio] ____________________________________________

self = <ex.TestSomething object at 0x7f3b1a13a180>

    @given(x=st.just(1))
>   @pytest.mark.anyio

ex.py:7: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv/lib64/python3.12/site-packages/anyio/pytest_plugin.py:105: in run_with_hypothesis
    runner.run_test(original_func, kwargs)
.venv/lib64/python3.12/site-packages/anyio/_backends/_asyncio.py:1970: in run_test
    self._raise_async_exceptions()
.venv/lib64/python3.12/site-packages/anyio/_backends/_asyncio.py:1884: in _raise_async_exceptions
    raise exceptions[0]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <anyio._backends._asyncio.TestRunner object at 0x7f3b193721e0>
test_func = <function TestSomething.test_foo at 0x7f3b194ade40>
kwargs = {'self': <ex.TestSomething object at 0x7f3b1a13a180>, 'x': 1}

    def run_test(
        self, test_func: Callable[..., Coroutine[Any, Any, Any]], kwargs: dict[str, Any]
    ) -> None:
        try:
            self.get_loop().run_until_complete(
>               self._call_in_runner_task(test_func, **kwargs)
            )
E           TypeError: TestRunner._call_in_runner_task() got multiple values for argument 'self'
E           Falsifying example: run_with_hypothesis(
E               self=<ex.TestSomething object at 0x7f3b1a13a180>,
E               x=1,
E           )

.venv/lib64/python3.12/site-packages/anyio/_backends/_asyncio.py:1965: TypeError
_____________________________________________ TestSomething.test_foo[trio] _____________________________________________

self = <ex.TestSomething object at 0x7f3b1949e570>

    @given(x=st.just(1))
>   @pytest.mark.anyio

ex.py:7: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv/lib64/python3.12/site-packages/anyio/pytest_plugin.py:105: in run_with_hypothesis
    runner.run_test(original_func, kwargs)
.venv/lib64/python3.12/site-packages/anyio/_backends/_asyncio.py:1970: in run_test
    self._raise_async_exceptions()
.venv/lib64/python3.12/site-packages/anyio/_backends/_asyncio.py:1884: in _raise_async_exceptions
    raise exceptions[0]
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <anyio._backends._asyncio.TestRunner object at 0x7f3b19373b90>
test_func = <function TestSomething.test_foo at 0x7f3b194ade40>
kwargs = {'self': <ex.TestSomething object at 0x7f3b1949e570>, 'x': 1}

    def run_test(
        self, test_func: Callable[..., Coroutine[Any, Any, Any]], kwargs: dict[str, Any]
    ) -> None:
        try:
            self.get_loop().run_until_complete(
>               self._call_in_runner_task(test_func, **kwargs)
            )
E           TypeError: TestRunner._call_in_runner_task() got multiple values for argument 'self'
E           Falsifying example: run_with_hypothesis(
E               self=<ex.TestSomething object at 0x7f3b1949e570>,
E               x=1,
E           )

.venv/lib64/python3.12/site-packages/anyio/_backends/_asyncio.py:1965: TypeError
=============================================== short test summary info ================================================
FAILED ex.py::TestSomething::test_foo[asyncio] - TypeError: TestRunner._call_in_runner_task() got multiple values for argument 'self'
FAILED ex.py::TestSomething::test_foo[trio] - TypeError: TestRunner._call_in_runner_task() got multiple values for argument 'self'
================================================== 2 failed in 0.30s ===================================================
@jdpage jdpage added the bug Something isn't working label Mar 14, 2024
@jdpage
Copy link
Author

jdpage commented Mar 14, 2024

As a workaround, changing the name of the method receiver parameter from self to something else solves the problem, e.g. the following behaves correctly:

from hypothesis import strategies as st, given
import pytest


class TestSomething:
    @given(x=st.just(1))
    @pytest.mark.anyio
    async def test_foo(_, x):  # self is now _
        assert x == 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant