Fix of TypeError when returning None in AuthBase.__call__ from async code #1386 #1387
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The root cause
The function
_run_authentication
is made to runAuthBase.__call__
and execute what if returned. But for an example, the HttpBearer returns None in cases when a token is misplaced in the request:Even though None is returned, the function
_run_authentication
tries to callasync_to_sync
without checking if the coroutine is None.Because of the call to async_to_sync with None, instead of a coroutine, this TypeError is raised:
Fix
Because we know that the coroutine is async:
We can assume that calling it without putting the coroutine into the eventloop won't trigger the coroutine body. So we can safely call the coroutine without putting it into the eventloop, and check if the result is a coroutine, not a function:
That way we won't call
async_to_sync
with None, thus triggering TypeError.