'anyio' has no attribute 'start_blocking_portal'

1.7k views Asked by At

One of my fast api endpoints is supposed to return a csv response. I try to check this endpoint but I get this error

self = <starlette.testclient.TestClient object at 0x7f834a5f6ec0>

    @contextlib.contextmanager
    def _portal_factory(
        self,
    ) -> typing.Generator[anyio.abc.BlockingPortal, None, None]:
        if self.portal is not None:
            yield self.portal
        else:
>           with anyio.start_blocking_portal(**self.async_backend) as portal:
E           AttributeError: module 'anyio' has no attribute 'start_blocking_portal'

This is how the test look like

from starlette.testclient import TestClient


def test_endpoint(client: TestClient, file_regression):

    response = client.get("/bla")

    file_regression.check(response.content, extension=".csv", binary=True)

my requirements.txt

black
fastapi==0.85.0
isort
pytest
pytest-regressions
requests
uvicorn
1

There are 1 answers

0
Grigoriev Semyon On BEST ANSWER

I see that start_blocking_portal is in anyio.from_thread package in stable version.

https://anyio.readthedocs.io/en/stable/migration.html#blocking-portal-changes

https://anyio.readthedocs.io/en/stable/api.html#anyio.from_thread.BlockingPortal

start_blocking_portal is in anyio package in v2.x

https://anyio.readthedocs.io/en/2.x/api.html

And I recommend you to upgrade FastAPI version to the current release