diff --git a/ruff.toml b/ruff.toml index 48db0af1e7..0176566151 100644 --- a/ruff.toml +++ b/ruff.toml @@ -8,4 +8,11 @@ ignore = ["D100","D101","D102","D103","D105","N806","E501"] exclude = ["wagtail/project_template/*","node_modules","venv",".venv","migrations"] line-length = 88 -select = ["E", "F", "I", "T20"] + +# E: pycodestyle errors +# F: Pyflakes +# I: isort +# T20: flake8-print +# BLE: flake8-blind-except +# C4: flake8-comprehensions +select = ["E", "F", "I", "T20", "BLE", "C4"] diff --git a/wagtail/admin/views/pages/bulk_actions/publish.py b/wagtail/admin/views/pages/bulk_actions/publish.py index 79e8d58035..e6f9a17893 100644 --- a/wagtail/admin/views/pages/bulk_actions/publish.py +++ b/wagtail/admin/views/pages/bulk_actions/publish.py @@ -24,7 +24,7 @@ class PublishBulkAction(PageBulkAction): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["has_draft_descendants"] = any( - map(lambda x: x["draft_descendant_count"], context["items"]) + item["draft_descendant_count"] for item in context["items"] ) return context diff --git a/wagtail/admin/views/pages/bulk_actions/unpublish.py b/wagtail/admin/views/pages/bulk_actions/unpublish.py index 8cc7119e42..74a64fcc63 100644 --- a/wagtail/admin/views/pages/bulk_actions/unpublish.py +++ b/wagtail/admin/views/pages/bulk_actions/unpublish.py @@ -23,7 +23,7 @@ class UnpublishBulkAction(PageBulkAction): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["has_live_descendants"] = any( - map(lambda x: x["live_descendant_count"] > 0, context["items"]) + item["live_descendant_count"] > 0 for item in context["items"] ) return context diff --git a/wagtail/contrib/modeladmin/views.py b/wagtail/contrib/modeladmin/views.py index 4f897aacf8..6c6612d380 100644 --- a/wagtail/contrib/modeladmin/views.py +++ b/wagtail/contrib/modeladmin/views.py @@ -651,7 +651,7 @@ class IndexView(SpreadsheetExportMixin, WMABaseView): # Allow certain types of errors to be re-raised as-is so that the # caller can treat them in a special way. raise - except Exception as e: + except Exception as e: # noqa: BLE001 # Every other error is caught with a naked except, because we don't # have any other way of validating lookup parameters. They might be # invalid if the keyword arguments are incorrect, or if the values diff --git a/wagtail/contrib/redirects/views.py b/wagtail/contrib/redirects/views.py index c64e16b607..e6c0629c8f 100644 --- a/wagtail/contrib/redirects/views.py +++ b/wagtail/contrib/redirects/views.py @@ -269,7 +269,7 @@ def start_import(request): % {"error_message": e}, ) return redirect("wagtailredirects:start_import") - except Exception as e: # pragma: no cover + except Exception as e: # noqa: BLE001; pragma: no cover messages.error( request, _("%(error)s encountered while trying to read file: %(filename)s") diff --git a/wagtail/contrib/settings/tests/site_specific/test_model.py b/wagtail/contrib/settings/tests/site_specific/test_model.py index a94fda1fc9..15da762469 100644 --- a/wagtail/contrib/settings/tests/site_specific/test_model.py +++ b/wagtail/contrib/settings/tests/site_specific/test_model.py @@ -62,7 +62,7 @@ class SettingModelTestCase(SiteSettingsTestMixin, TestCase): # Attempt to pickle ImportantPages instance try: pickled = pickle.dumps(obj, -1) - except Exception as e: + except Exception as e: # noqa: BLE001 raise AssertionError( "An error occured when attempting to pickle %r: %s" % (obj, e) ) @@ -70,7 +70,7 @@ class SettingModelTestCase(SiteSettingsTestMixin, TestCase): # Now unpickle the pickled ImportantPages try: unpickled = pickle.loads(pickled) - except Exception as e: + except Exception as e: # noqa: BLE001 raise AssertionError( "An error occured when attempting to unpickle %r: %s" % (obj, e) ) diff --git a/wagtail/documents/models.py b/wagtail/documents/models.py index fced0bb8e9..ac4d5b4f19 100644 --- a/wagtail/documents/models.py +++ b/wagtail/documents/models.py @@ -114,7 +114,7 @@ class AbstractDocument(CollectionMember, index.Indexed, models.Model): if self.file_size is None: try: self.file_size = self.file.size - except Exception: + except Exception: # noqa: BLE001 # File doesn't exist return diff --git a/wagtail/images/fields.py b/wagtail/images/fields.py index 53ec64d653..e52349ebf6 100644 --- a/wagtail/images/fields.py +++ b/wagtail/images/fields.py @@ -163,7 +163,7 @@ class WagtailImageField(ImageField): f.image = willow.Image.open(file) f.content_type = image_format_name_to_content_type(f.image.format_name) - except Exception as exc: + except Exception as exc: # noqa: BLE001 # Willow doesn't recognize it as an image. raise ValidationError( self.error_messages["invalid_image"], diff --git a/wagtail/images/management/commands/wagtail_update_image_renditions.py b/wagtail/images/management/commands/wagtail_update_image_renditions.py index 8f617bdfe9..d7580560a7 100644 --- a/wagtail/images/management/commands/wagtail_update_image_renditions.py +++ b/wagtail/images/management/commands/wagtail_update_image_renditions.py @@ -28,7 +28,7 @@ class Command(BaseCommand): rendition_image = rendition.image rendition.delete() success_count = success_count + 1 - except Exception: + except Exception: # noqa: BLE001 self.stderr.write( f"Could not purge rendition for {rendition_image.title}" ) @@ -45,7 +45,7 @@ class Command(BaseCommand): rendition.delete() rendition_image.get_rendition(rendition_filter) success_count = success_count + 1 - except Exception: + except Exception: # noqa: BLE001 self.stderr.write( f"Could not regenerate rendition for {rendition_image.title}" ) diff --git a/wagtail/images/models.py b/wagtail/images/models.py index a12dee81a6..2e79f17ba1 100644 --- a/wagtail/images/models.py +++ b/wagtail/images/models.py @@ -139,7 +139,7 @@ class ImageFileMixin: if self.file_size is None: try: self.file_size = self.file.size - except Exception as e: + except Exception as e: # noqa: BLE001 # File not found # # Have to catch everything, because the exception diff --git a/wagtail/images/tests/test_management_commands.py b/wagtail/images/tests/test_management_commands.py index 6f2ea80214..8f7de2252f 100644 --- a/wagtail/images/tests/test_management_commands.py +++ b/wagtail/images/tests/test_management_commands.py @@ -33,7 +33,7 @@ class TestUpdateImageRenditions(TestCase): try: rendition_image = rendition.image rendition.delete() - except Exception: + except Exception: # noqa: BLE001 warnings.warn(f"Could not delete rendition for {rendition_image}") def run_command(self, **options): diff --git a/wagtail/management/commands/create_log_entries_from_revisions.py b/wagtail/management/commands/create_log_entries_from_revisions.py index 2aded83a60..29c0d07652 100644 --- a/wagtail/management/commands/create_log_entries_from_revisions.py +++ b/wagtail/management/commands/create_log_entries_from_revisions.py @@ -45,7 +45,7 @@ class Command(BaseCommand): if not PageLogEntry.objects.filter(revision=revision).exists(): try: current_revision_as_page = revision.as_object() - except Exception: + except Exception: # noqa: BLE001 # restoring old revisions may fail if e.g. they have an on_delete=PROTECT foreign key # to a no-longer-existing model instance. We cannot compare changes between two # non-restorable revisions, although we can at least infer that there was a content @@ -57,7 +57,7 @@ class Command(BaseCommand): if previous_revision is not None: try: previous_revision_as_page = previous_revision.as_object() - except Exception: + except Exception: # noqa: BLE001 previous_revision_as_page = None if ( diff --git a/wagtail/test/dummy_external_storage.py b/wagtail/test/dummy_external_storage.py index c3c9dfb80a..52c0a72e77 100644 --- a/wagtail/test/dummy_external_storage.py +++ b/wagtail/test/dummy_external_storage.py @@ -85,5 +85,5 @@ class DummyExternalStorageFile(File): def size(self): try: return super().size - except Exception as e: + except Exception as e: # noqa: BLE001 raise DummyExternalStorageError(str(e)) diff --git a/wagtail/test/utils/decorators.py b/wagtail/test/utils/decorators.py index f069cc7ed8..368e2c6f4c 100644 --- a/wagtail/test/utils/decorators.py +++ b/wagtail/test/utils/decorators.py @@ -19,7 +19,7 @@ class disconnect_signal_receiver: try: func(*args, **kwargs) - except Exception as e: + except Exception as e: # noqa: BLE001 exception = e finally: self.signal.connect(self.receiver) diff --git a/wagtail/test/utils/page_tests.py b/wagtail/test/utils/page_tests.py index f65b24ebad..2b350fd539 100644 --- a/wagtail/test/utils/page_tests.py +++ b/wagtail/test/utils/page_tests.py @@ -257,7 +257,7 @@ class WagtailPageTestCase(WagtailTestUtils, TestCase): resp = self.client.get(path, data=query_data) else: resp = self.client.post(path, **post_kwargs) - except Exception as e: + except Exception as e: # noqa: BLE001 msg = self._formatMessage( msg, 'Failed to render route "%(route_path)s" for %(page_type)s "%(page)s":\n%(exc)s' @@ -333,7 +333,7 @@ class WagtailPageTestCase(WagtailTestUtils, TestCase): path = reverse("wagtailadmin_pages:edit", kwargs={"page_id": page.id}) try: response = self.client.get(path) - except Exception as e: + except Exception as e: # noqa: BLE001 self.client.logout() msg = self._formatMessage( msg, @@ -364,7 +364,7 @@ class WagtailPageTestCase(WagtailTestUtils, TestCase): try: self.client.post(path, data_to_post) - except Exception as e: + except Exception as e: # noqa: BLE001 msg = self._formatMessage( msg, 'Failed to load edit view via POST for %(page_type)s "%(page)s":\n%(exc)s' @@ -419,7 +419,7 @@ class WagtailPageTestCase(WagtailTestUtils, TestCase): response.content.decode(), {"is_valid": True, "is_available": True}, ) - except Exception as e: + except Exception as e: # noqa: BLE001 self.client.logout() msg = self._formatMessage( msg, @@ -435,7 +435,7 @@ class WagtailPageTestCase(WagtailTestUtils, TestCase): try: self.client.get(preview_path, data={"mode": mode}) - except Exception as e: + except Exception as e: # noqa: BLE001 msg = self._formatMessage( msg, 'Failed to load preview for %(page_type)s "%(page)s" with mode="%(mode)s":\n%(exc)s' diff --git a/wagtail/tests/test_utils.py b/wagtail/tests/test_utils.py index acf397e9c3..45659cac06 100644 --- a/wagtail/tests/test_utils.py +++ b/wagtail/tests/test_utils.py @@ -175,14 +175,14 @@ class TestInvokeViaAttributeShortcut(SimpleTestCase): def test_pickleability(self): try: pickled = pickle.dumps(self.test_object, -1) - except Exception as e: + except Exception as e: # noqa: BLE001 raise AssertionError( "An error occured when attempting to pickle %r: %s" % (self.test_object, e) ) try: self.test_object = pickle.loads(pickled) - except Exception as e: + except Exception as e: # noqa: BLE001 raise AssertionError( "An error occured when attempting to unpickle %r: %s" % (self.test_object, e)