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

chore: use isinstance() instead of type comparison #1792

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 3 additions & 9 deletions googleapiclient/discovery.py
Expand Up @@ -698,21 +698,15 @@ def _cast(value, schema_type):
A string representation of 'value' based on the schema_type.
"""
if schema_type == "string":
if type(value) == type("") or type(value) == type(""):
Copy link

Choose a reason for hiding this comment

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

This looks weird, but also potentially unsafe to remove.

Copy link

Choose a reason for hiding this comment

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

This is indeed weird, but is also safe.

Look at the last time it was changed (you will have to expand discover.py and scroll to line 701): 3bbefc1#diff-b039ba2bb64e79cf4c77e4fe5a4b21a826172e8b7ae9be872fd2d46047c05cbbL695-R701

Originally, it was

if type(value) == type("") or type(value) == type(u""):

But was changed automatically to be

if type(value) == type("") or type(value) == type(""):

because type("") == type(u"") == "str".

This if is redundant and pretty silly, and should be changed, no?

return value
else:
return str(value)
return str(value)
elif schema_type == "integer":
return str(int(value))
elif schema_type == "number":
return str(float(value))
elif schema_type == "boolean":
return str(bool(value)).lower()
else:
if type(value) == type("") or type(value) == type(""):
return value
else:
return str(value)
return str(value)


def _media_size_to_long(maxSize):
Expand Down Expand Up @@ -1075,7 +1069,7 @@ def method(self, **kwargs):
for key, value in kwargs.items():
to_type = parameters.param_types.get(key, "string")
# For repeated parameters we cast each member of the list.
if key in parameters.repeated_params and type(value) == type([]):
if key in parameters.repeated_params and isinstance(value, list):
cast_value = [_cast(x, to_type) for x in value]
else:
cast_value = _cast(value, to_type)
Expand Down
4 changes: 2 additions & 2 deletions googleapiclient/model.py
Expand Up @@ -174,7 +174,7 @@ def _build_query(self, params):
params.update({"alt": self.alt_param})
astuples = []
for key, value in params.items():
if type(value) == type([]):
if isinstance(value, list):
for x in value:
x = x.encode("utf-8")
astuples.append((key, x))
Expand Down Expand Up @@ -393,7 +393,7 @@ def makepatch(original, modified):
# Use None to signal that the element is deleted
patch[key] = None
elif original_value != modified_value:
if type(original_value) == type({}):
if isinstance(original_value, dict):
# Recursively descend objects
patch[key] = makepatch(original_value, modified_value)
else:
Expand Down