Skip to content

Hypothesis integration raises internal error when collecting RuleBasedStateMachine #758

Closed
@Zac-HD

Description

@Zac-HD

Initially reported in HypothesisWorks/hypothesis#3850

The bug can be reproduced by installing pytest_asyncio and hypothesis, then trying to run the following test:

from hypothesis.stateful import RuleBasedStateMachine
class StatefulTest(RuleBasedStateMachine):
    def __init__(self):
        super().__init__()
 
TestStorage = StatefulTest.TestCase
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   ...
INTERNALERROR>   File ".../python3.11/site-packages/pytest_asyncio/plugin.py", line 371, in pytest_collection_modifyitems
INTERNALERROR>     and _hypothesis_test_wraps_coroutine(function)
INTERNALERROR>         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
INTERNALERROR>   File ".../python3.11/site-packages/pytest_asyncio/plugin.py", line 377, in _hypothesis_test_wraps_coroutine
INTERNALERROR>     return _is_coroutine(function.hypothesis.inner_test)
INTERNALERROR>                          ^^^^^^^^^^^^^^^^^^^
INTERNALERROR> AttributeError: 'function' object has no attribute 'hypothesis'

To fix this, we should avoid assuming that the .hypothesis attribute exists in

@staticmethod
def _can_substitute(item: Function) -> bool:
func = item.obj
return getattr(
func, "is_hypothesis_test", False
) and asyncio.iscoroutinefunction(func.hypothesis.inner_test)

As a fix, I'd suggest:

        return (
            getattr(func, "is_hypothesis_test", False)
            and getattr(func, "hypothesis", None)
            and asyncio.iscoroutinefunction(func.hypothesis.inner_test)
        )

Note that there's no simple to support async stateful testing; the hypothesis-trio project is essentially a fork of our upstream stateful testing module with async handling injected in many places. My suggestion therefore continues to treat asyncio stateful tests as out of scope, since I don't think there's sufficient demand to be worth the maintainence burden.

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