Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove manual contextvar copy logic #1421

Merged
merged 8 commits into from
Jan 31, 2022

Conversation

Kludex
Copy link
Member

@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
Member

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"

@tomchristie
Copy link
Member

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

@Kludex
Copy link
Member 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
Member Author

Kludex commented Jan 26, 2022

Well... That was unexpected...

@adriangb
Copy link
Member

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
Member

Well... That was unexpected...

Maybe because we require the back port on < 3.7?

"contextlib2 >= 21.6.0; python_version < '3.7'",

@Kludex
Copy link
Member Author

Kludex commented Jan 26, 2022

Well... That was unexpected...

Maybe because we require the back port on < 3.7?

"contextlib2 >= 21.6.0; python_version < '3.7'",

That's for contextlib, not for contextvars 🤔

@Kludex
Copy link
Member Author

Kludex commented Jan 26, 2022

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

@adriangb
Copy link
Member

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
Member 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
Member 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 encode:master Jan 31, 2022
@Kludex Kludex deleted the refactor/remove-contextvars-logic branch January 31, 2022 23:26
@adriangb
Copy link
Member

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