schemathesis / schemathesis

Supercharge your API testing, catch bugs, and ensure compliance

Home Page:https://schemathesis.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] Does not work with anyio 4

chbndrhnns opened this issue · comments

Checklist

  • I checked the FAQ section of the documentation
  • I looked for similar issues in the issue tracker
  • I am using the latest version of Schemathesis

Describe the bug

Schemathesis does not start when anyio 4 is installed.
It seems there is a dependency on starlette-testclient which is the cause of the problem.

To Reproduce

from schemathesis import from_asgi, experimental

from starlette.applications import Starlette

schema = from_asgi("/openapi.json", Starlette())


@schema.parametrize()
def test_api(case):
    response = case.call_asgi()
    case.validate_response(response)

Expected behavior

Works

Environment

- OS: [e.g. Linux or Windows]
- Python version: [e.g. 3.11]
- Schemathesis version: [e.g. 3.20.0]
- Spec version: [e.g. Open API 3.0.2]

Additional context

../../../../../Library/Caches/pypoetry/virtualenvs/bla-Ms7_xa4C-py3.9/lib/python3.9/site-packages/starlette_testclient/_testclient.py:450: in _portal_factory
    with anyio.start_blocking_portal(**self.async_backend) as portal:
E   AttributeError: module 'anyio' has no attribute 'start_blocking_portal'

Indeed, Schemathesis pins anyio to <4, exactly because of starlette-testclient

There is a plan to switch from starlette-testclient completely, but I didn't get to it just yet.

@Stranger6667 Is supporting anyio>=4 currently still planned (be it through switching away from starlette-testclient or some other way) and if so, can you estimate when that would be? Can I support you somehow in the migration?

Hi @wolfskaempf !

thank you for pinging. At this point I refactored the transport logic and now it should be easier to migrate that logic to httpx.

There are two main ways to get this through. The first one is to migrate ASGI transport to httpx, including sync-async bridge (the ASGI adapter in httpx is async). The other option is to add support for newer anyio versions to starlette-testclient and update it in Schemathesis.

the latter could be simpler, but I’d prefer the former one as it is cleaner and reduce our dependency on legacy tools (in a sense that starlette moved to httpx and starlette-testclient is a backport of its old code).

In terms of support I am thinking about a few ways:

  • work on the implementation, I’d be happy to assist
  • Test pre-release version when it’s ready
  • If your company uses Schemathesis, they might take a look at sponsorship options.

Other than that I am currently looking more into reworking all the jsonschema usage by integrating the next version of my ‘jsonschema’ Rust crate which aims to resolve long standing performance and compatibility issues in this area. It will take a few months, so I am not sure if I’d have the bandwidth to dive deeper into transports.

Tagging @Kludex for visibility. Maybe adjusting things on the starlette-testclient could be the easiest way to go for now.
@Kludex - would you be open to accepting anyio 4 support if I or somebody else will open a PR in the starlette-testclient repo?

@Stranger6667 I gave you maintainer rights on starlette-testclient. Feel free to do what you need. 👍

@Stranger6667 I gave you maintainer rights on starlette-testclient. Feel free to do what you need. 👍

Thanks!

The fix is available in 3.27.1