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

Add exceptions for invalid trace callback #717

Merged
merged 6 commits into from
Jun 12, 2023

Conversation

karpetrosyan
Copy link
Member

Examples

import httpcore

async def log(a, b):
    print(a, b)

httpcore.request("GET", "https://google.com", extensions={"trace": log})

Old output

RuntimeWarning: coroutine 'log' was never awaited
  self.trace_extension(prefix_and_name, info)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

New output

TypeError: If you are using a synchronous interface, the callback of the `trace` extension should be a normal function instead of an asynchronous function.
import trio

def log(a, b):
    print(a,b)

async def req():
    cl = httpcore.AsyncConnectionPool()
    await cl.request("GET", "https://google.com", extensions={"trace": log})

trio.run(req)

Old output

TypeError: object NoneType can't be used in 'await' expression

New output

TypeError: If you're using an asynchronous interface, the callback of the `trace` extension should be an asynchronous function rather than a normal function.

@karpetrosyan
Copy link
Member Author

When migrating from synchronous to asynchronous interface, it's common to forget to change the synchronous trace callback to an asynchronous function, so let's add a user-friendly behavior for those cases.

@karpetrosyan karpetrosyan requested a review from a team June 10, 2023 08:18
httpcore/_trace.py Outdated Show resolved Hide resolved
Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

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

Sure.

@karpetrosyan karpetrosyan merged commit 7548dd5 into encode:master Jun 12, 2023
5 checks passed
@karpetrosyan karpetrosyan mentioned this pull request Jul 3, 2023
1 task
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.

None yet

3 participants