From 2731b02496dc8b45c555c7295763b1ae19c295a8 Mon Sep 17 00:00:00 2001 From: MarkLark86 Date: Wed, 2 Aug 2023 14:49:53 +1000 Subject: [PATCH] fix(flake8): Replace type check with isinstance (#2464) --- apps/archive/archive.py | 2 +- apps/archive/common.py | 2 +- apps/content_types/content_types.py | 8 ++++---- apps/publish/content/common.py | 8 ++++---- .../data_updates/00009_20180425-010702_vocabularies.py | 2 +- superdesk/io/feeding_services/twitter.py | 2 +- superdesk/publish/formatters/ninjs_formatter.py | 2 +- superdesk/publish/transmitters/http_push.py | 2 +- superdesk/upload.py | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/apps/archive/archive.py b/apps/archive/archive.py index 618ab890c9..9bab997751 100644 --- a/apps/archive/archive.py +++ b/apps/archive/archive.py @@ -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() diff --git a/apps/archive/common.py b/apps/archive/common.py index 8baf2b8c3c..6416a02b83 100644 --- a/apps/archive/common.py +++ b/apps/archive/common.py @@ -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: diff --git a/apps/content_types/content_types.py b/apps/content_types/content_types.py index 65083ef001..938dfde094 100644 --- a/apps/content_types/content_types.py +++ b/apps/content_types/content_types.py @@ -387,7 +387,7 @@ 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 @@ -395,7 +395,7 @@ def set_required_for_custom(editor, schema, mandatory, fields_map): 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) @@ -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) diff --git a/apps/publish/content/common.py b/apps/publish/content/common.py index 4d0b2aafe0..9ad1fdf389 100644 --- a/apps/publish/content/common.py +++ b/apps/publish/content/common.py @@ -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) @@ -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]: @@ -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: @@ -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 diff --git a/superdesk/data_updates/00009_20180425-010702_vocabularies.py b/superdesk/data_updates/00009_20180425-010702_vocabularies.py index 9df37662d9..b7c25fa0c2 100644 --- a/superdesk/data_updates/00009_20180425-010702_vocabularies.py +++ b/superdesk/data_updates/00009_20180425-010702_vocabularies.py @@ -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}}) diff --git a/superdesk/io/feeding_services/twitter.py b/superdesk/io/feeding_services/twitter.py index 6f7436d6de..354f122a84 100644 --- a/superdesk/io/feeding_services/twitter.py +++ b/superdesk/io/feeding_services/twitter.py @@ -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 diff --git a/superdesk/publish/formatters/ninjs_formatter.py b/superdesk/publish/formatters/ninjs_formatter.py index bebe8ab4b7..4ab4e7fff3 100644 --- a/superdesk/publish/formatters/ninjs_formatter.py +++ b/superdesk/publish/formatters/ninjs_formatter.py @@ -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 diff --git a/superdesk/publish/transmitters/http_push.py b/superdesk/publish/transmitters/http_push.py index 5b1275b7c4..4c1140a53f 100644 --- a/superdesk/publish/transmitters/http_push.py +++ b/superdesk/publish/transmitters/http_push.py @@ -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 = {} diff --git a/superdesk/upload.py b/superdesk/upload.py index 99f13eb1f4..30aed62c82 100644 --- a/superdesk/upload.py +++ b/superdesk/upload.py @@ -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