pythonpython-3.xgoogle-ads-api

isinstance failing on same class types


Can anyone help make sense of this?

Using google python sdk as an example, according to Google retry policy (https://cloud.google.com/python/docs/reference/storage/latest/retry_timeout):

from google.api_core import exceptions
from google.api_core.retry import Retry

_MY_RETRIABLE_TYPES = (
   exceptions.TooManyRequests,  # 429
   exceptions.InternalServerError,  # 500
   exceptions.BadGateway,  # 502
   exceptions.ServiceUnavailable,  # 503
)

def is_retryable(exc):
    return isinstance(exc, _MY_RETRIABLE_TYPES)

my_retry_policy = Retry(predicate=is_retryable)

Why does the following occur when testing is_retryable?

exceptions.TooManyRequests==exceptions.TooManyRequests -> True
is_retryable(exceptions.TooManyRequests) -> False 
is_retryable(429) -> False 
is_retryable(exceptions.TooManyRequests.code) -> False
is_retryable(exceptions.TooManyRequests.code.value) -> False 

Solution

  • Your tuple consists of a number of types. A type is not an instance of itself, nor is it an "alias" of any kind for an integer value. You need to pass an instance of one of the types to is_retryable.

    >>> is_retryable(exceptions.TooManyRequests())
    True