Skip to content

Conversation

@uSpike
Copy link
Contributor

@uSpike uSpike commented Jun 23, 2021

As implemented in #1157, TestClient.async_backend was a class-level attribute. This made it easy to monkeypatch for use in starlette tests, but makes it difficult/impossible to run different test client backends concurrently (in threads).

This PR changes TestClient to accept a backend and backend_options arguments which allow a user to specify the backend options for only that TestClient instance. The class attribute will remain as the default for these arguments.

Fixes #1209

Copy link
Contributor

@graingert graingert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, I think it's worth removing the classvar and using a functools.partial object for the tests


#: These options are passed to `anyio.start_blocking_portal()`
#: These are the default options for the constructor arguments
async_backend: typing.Dict[str, typing.Any] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this class-level declaration still? Class vars always confuse me, but this means somebody can do:

TestClient.async_backend["backend"] = "trio"

to default all future TestClient instances to use the Trio backend, right?

I know removing this would be slightly changing the API, however in the code examples we gave we only modified instance attributes and that usage should stay the same. What's your thinking around this?

Copy link
Contributor

@graingert graingert Jun 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed it in my followup PR: #1211

@graingert
Copy link
Contributor

graingert commented Jun 28, 2021

merged in #1211

@graingert graingert closed this Jun 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

allow passing anyio backend options via TestClient constructor

3 participants