Skip to content

Remove manual contextvar copy logic#1421

Merged
Kludex merged 8 commits intomasterfrom
refactor/remove-contextvars-logic
Jan 31, 2022
Merged

Remove manual contextvar copy logic#1421
Kludex merged 8 commits intomasterfrom
refactor/remove-contextvars-logic

Conversation

@Kludex
Copy link
Copy Markdown
Owner

@Kludex Kludex commented Jan 20, 2022

anyio 3.4.0 copies the context already: agronholm/anyio#390

There's no need for limiting the range just for that logic, but that's something we can clean up (now or in the future).

@Kludex Kludex added this to the Version 1.0 milestone Jan 23, 2022
@Kludex Kludex added the clean up Refinement to existing functionality label Jan 23, 2022
@adriangb
Copy link
Copy Markdown
Contributor

I ran this change on anyio==3.0.0 and nothing broke. So it seems like we don't have a test for this (probably the code is covered, but not the logic).

So maybe it's worth adding a quick test before merging this to be sure things work as expected?

def test_accessing_context_from_threaded_sync_endpoint() -> None:

    ctxvar: ContextVar[bytes]  = ContextVar("ctxvar")
    ctxvar.set(b"data")

    def endpoint(request: Request) -> Response:
        return Response(ctxvar.get())
    
    app = Starlette(routes=[Route("/", endpoint)])
    client = TestClient(app)
    
    resp = client.get("/")
    assert resp.content == b"data"

@lovelydinosaur
Copy link
Copy Markdown

Ooohhh.... nice bit of tidying.
I do like tidying.

@Kludex
Copy link
Copy Markdown
Owner Author

Kludex commented Jan 26, 2022

I've just noticed that this doesn't work for Python 3.6, so I can expect a pipeline failure on the following commit.

EDIT: To be fair, it didn't work for Python 3.6 before... So not a relevant comment.

@Kludex
Copy link
Copy Markdown
Owner Author

Kludex commented Jan 26, 2022

Well... That was unexpected...

@adriangb
Copy link
Copy Markdown
Contributor

Maybe import contextvars within the test and otherwise skip the test?

def test_accessing_context_from_threaded_sync_endpoint() -> None:
    try:
        from contextvars import ContextVar
    except ImportError:
        pytest.skip('test requires contextvars package to be available')

    ctxvar: ContextVar[bytes]  = ContextVar("ctxvar")
    ctxvar.set(b"data")

    def endpoint(request: Request) -> Response:
        return Response(ctxvar.get())
    
    app = Starlette(routes=[Route("/", endpoint)])
    client = TestClient(app)
    
    resp = client.get("/")
    assert resp.content == b"data"

@adriangb
Copy link
Copy Markdown
Contributor

@Kludex
Copy link
Copy Markdown
Owner Author

Kludex commented Jan 26, 2022

Well... That was unexpected...

Maybe because we require the back port on < 3.7?

https://github.com/encode/starlette/blob/5a5a5c367f2bac0cb526d417adb59cd2174c3329/setup.py#L43

That's for contextlib, not for contextvars 🤔

@Kludex
Copy link
Copy Markdown
Owner Author

Kludex commented Jan 26, 2022

@adriangb
Copy link
Copy Markdown
Contributor

adriangb commented Jan 26, 2022

That's for contextlib, not for contextvars 🤔

🤦 my bad

AnyIO installs https://pypi.org/project/contextvars/ for Python 3.6 :P

So I guess we're good then? The test demonstrates that things work, and we know why they work, so I don't see any issue right?

@Kludex
Copy link
Copy Markdown
Owner Author

Kludex commented Jan 26, 2022

Yep.

The only "issue" is restricting anyio's possible versions. It's also important to say that anyio is working on the 4.0 release.

Besides that, everything is cool.

@Kludex
Copy link
Copy Markdown
Owner Author

Kludex commented Jan 31, 2022

Since the range of anyio will be >=3.4.0,<5.0, we can merge this 👍

@Kludex Kludex merged commit 565898b into Kludex:master Jan 31, 2022
@Kludex Kludex deleted the refactor/remove-contextvars-logic branch January 31, 2022 23:26
@adriangb
Copy link
Copy Markdown
Contributor

adriangb commented Feb 1, 2022

Awesome, thank you @Kludex !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clean up Refinement to existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants