The Client.request method failed to refresh the authentication token when self.oauth2_token was a dictionary (or any truthy value that wasn't an OAuth2Token instance). This resulted in API requests being sent without a valid Authorization header when the client was initialized with a dictionary token.
The logic to trigger a token refresh was:
if not self.oauth2_token or (
isinstance(self.oauth2_token, OAuth2Token) and self.oauth2_token.expired
):When self.oauth2_token is a dictionary:
not self.oauth2_tokenisFalse.isinstance(self.oauth2_token, OAuth2Token)isFalse.
Thus, the entire condition evaluated to False, skipping the refresh_oauth2() call.
The updated condition is:
if not isinstance(self.oauth2_token, OAuth2Token) or self.oauth2_token.expired:Now, if self.oauth2_token is not an instance of OAuth2Token (e.g., it's a dictionary or None), the first part of the or condition is True, correctly triggering a refresh.
The tests do not cover the scenario where refresh_oauth2() fails (e.g., raises an exception due to network issues). In a real application, we would need error handling around the refresh call to prevent the application from crashing or proceeding with an invalid state.