suggestion (code-quality): Merge nested if conditions (merge-nested-ifs)
if (not creds or not creds.valid) and (creds and creds.expired and creds.refresh_token):
self.logger.info("Refreshing expired credentials from %s...", token_path)
try:
creds.refresh(Request())
except Exception as e:
self.logger.error("Error refreshing credentials: %s", e)
# Potentially delete token.json and force re-authentication
if os.path.exists(token_path):
try:
os.remove(token_path)
self.logger.info("Removed invalid token file: %s", token_path)
except OSError as oe:
self.logger.error("Error removing token file %s: %s", token_path, oe)
creds = None # Force re-authentication
Explanation
Too much nesting can make code difficult to understand, and this is especially
true in Python, where there are no brackets to help out with the delineation of
different nesting levels.
Reading deeply nested code is confusing, since you have to keep track of which
conditions relate to which levels. We therefore strive to reduce nesting where
possible, and the situation where two if conditions can be combined using
and is an easy win.
Originally posted by @sourcery-ai[bot] in #111 (comment)