Skip to content

Commit

Permalink
fix(flake8): Replace type check with isinstance (#2464)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkLark86 committed Aug 2, 2023
1 parent e3241da commit 2731b02
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion apps/archive/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ def delete_by_article_ids(self, ids):
super().delete_action({config.ID_FIELD: {"$in": ids}})

def _set_association_timestamps(self, assoc_item, updates, new=True):
if type(assoc_item) == dict:
if isinstance(assoc_item, dict):
assoc_item[config.LAST_UPDATED] = updates.get(config.LAST_UPDATED, datetime.datetime.now())
if new:
assoc_item[config.DATE_CREATED] = datetime.datetime.now()
Expand Down
2 changes: 1 addition & 1 deletion apps/archive/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def transtype_metadata(doc, original=None):
continue

if value_type == "date":
if value and type(value) != datetime:
if value and not isinstance(value, datetime):
try:
extra[key] = date_parse(value)
except Exception as e:
Expand Down
8 changes: 4 additions & 4 deletions apps/content_types/content_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,15 @@ def set_enabled_for_custom(editor, allowed, fields_map):

def set_required_for_custom(editor, schema, mandatory, fields_map):
# old notation where `value` is string
for field, value in tuple((k, v) for k, v in mandatory.items() if type(v) == str):
for field, value in tuple((k, v) for k, v in mandatory.items() if isinstance(v, str)):
if field == value or field == "subject":
try:
editor[fields_map.get(field, field)]["required"] = value is not None
schema[fields_map.get(field, field)]["required"] = value is not None
except KeyError:
continue
# new notation where `value` is dict
for field, value in tuple((k, v) for k, v in mandatory.items() if type(v) == dict):
for field, value in tuple((k, v) for k, v in mandatory.items() if isinstance(v, dict)):
if (field is not None and value.get("required", False)) or field == "subject":
try:
editor[fields_map.get(field, field)]["required"] = value.get("required", False)
Expand All @@ -406,14 +406,14 @@ def set_required_for_custom(editor, schema, mandatory, fields_map):

def set_readonly_for_custom(editor, schema, mandatory, fields_map):
# old notation where `value` is string
for field, value in tuple((k, v) for k, v in mandatory.items() if type(v) == str):
for field, value in tuple((k, v) for k, v in mandatory.items() if isinstance(v, str)):
try:
editor[fields_map.get(field, field)]["readonly"] = False
schema[fields_map.get(field, field)]["readonly"] = False
except KeyError:
continue
# new notation where `value` is dict
for field, value in tuple((k, v) for k, v in mandatory.items() if type(v) == dict):
for field, value in tuple((k, v) for k, v in mandatory.items() if isinstance(v, dict)):
if (field is not None and value.get("readonly", False)) or field == "subject":
try:
editor[fields_map.get(field, field)]["readonly"] = value.get("readonly", False)
Expand Down
8 changes: 4 additions & 4 deletions apps/publish/content/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def _validate_associated_items(self, original_item, updates=None, validation_err

for item in items:
orig = None
if type(item) == dict and item.get(config.ID_FIELD):
if isinstance(item, dict) and item.get(config.ID_FIELD):
orig = super().find_one(req=None, _id=item[config.ID_FIELD]) or {}
doc = copy(orig)
doc.update(item)
Expand Down Expand Up @@ -676,7 +676,7 @@ def _validate_associated_items(self, original_item, updates=None, validation_err
# don't validate items that already have published
if doc_item_state not in [CONTENT_STATE.PUBLISHED, CONTENT_STATE.CORRECTED]:
validate_item = {"act": self.publish_type, "type": doc[ITEM_TYPE], "validate": doc}
if type(item) == dict:
if isinstance(item, dict):
validate_item["embedded"] = True
errors = get_resource_service("validate").post([validate_item], headline=True, fields=True)[0]
if errors[0]:
Expand Down Expand Up @@ -732,7 +732,7 @@ def _refresh_associated_items(self, original, skip_related=False):
"""
associations = original.get(ASSOCIATIONS) or {}
for name, item in associations.items():
if type(item) == dict and item.get(config.ID_FIELD) and (not skip_related or len(item.keys()) > 2):
if isinstance(item, dict) and item.get(config.ID_FIELD) and (not skip_related or len(item.keys()) > 2):
keys = [key for key in DEFAULT_SCHEMA.keys() if key not in PRESERVED_FIELDS]

if app.settings.get("COPY_METADATA_FROM_PARENT") and item.get(ITEM_TYPE) in MEDIA_TYPES:
Expand Down Expand Up @@ -789,7 +789,7 @@ def _publish_associated_items(self, original, updates=None):
for associations_key, associated_item in associations.items():
if associated_item is None:
continue
if type(associated_item) == dict and associated_item.get(config.ID_FIELD):
if isinstance(associated_item, dict) and associated_item.get(config.ID_FIELD):
if not config.PUBLISH_ASSOCIATED_ITEMS or not publish_service:
if original.get(ASSOCIATIONS, {}).get(associations_key):
# Not allowed to publish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def forwards(self, mongodb_collection, mongodb_database):
if "schema" in vocabulary:
schema = vocabulary["schema"]
for field in self.update_fields:
if field in vocabulary["schema"] and type(vocabulary["schema"]) == dict:
if field in vocabulary["schema"] and isinstance(vocabulary["schema"], dict):
schema[field]["required"] = True
mongodb_collection.update({"_id": vocabulary.get(config.ID_FIELD)}, {"$set": {"schema": schema}})

Expand Down
2 changes: 1 addition & 1 deletion superdesk/io/feeding_services/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def _update(self, provider, update, test=False):
statuses = api.GetUserTimeline(screen_name=screen_name, count=status_count)
except twitter.error.TwitterError as exc:
# in some case python twitter error will return dict
if type(exc.args[0]) == dict:
if isinstance(exc.args[0], dict):
raise IngestTwitterError.TwitterAPIGeneralError(exc, provider)
if exc.message[0].get("code") == 34:
# that page does not exist
Expand Down
2 changes: 1 addition & 1 deletion superdesk/publish/formatters/ninjs_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def _transform_to_ninjs(self, article, subscriber, recursive=True):
if article.get("extra"):
ninjs["extra"] = article["extra"]
for key, value in ninjs["extra"].items():
if type(value) == dict and "embed" in value:
if isinstance(value, dict) and "embed" in value:
value.setdefault("description", "")

return ninjs
Expand Down
2 changes: 1 addition & 1 deletion superdesk/publish/transmitters/http_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _copy_published_media_files(self, item, destination):

assets_url = self._get_assets_url(destination)

if not (type(assets_url) == str and assets_url.strip()):
if not (isinstance(assets_url, str) and assets_url.strip()):
return

media = {}
Expand Down
2 changes: 1 addition & 1 deletion superdesk/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def upload_config_file():
logger.error("Invalid JSON file {0}: {1}".format(file_name, str(ex)))
raise SuperdeskApiError.internalError("Invalid JSON file: {}.".format(file_name))

if type(file_data) == dict:
if isinstance(file_data, dict):
file_data = [file_data]
_items += file_data

Expand Down

0 comments on commit 2731b02

Please sign in to comment.