mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-08-27 21:23:20 +00:00
Chore: enable flake8-bugbear (B) default-subset ruff rules
3 B009 (getattr with a constant string, rewrite as attribute access) hits autofixed. 7 B017 (assert blind Exception) hits: one narrowed to the actual ValueError raised by bulk_edit.edit_pdf, the other six suppressed with noqa since the code under test genuinely raises (or a mock genuinely injects) a bare Exception, so a narrower assertion would be wrong. Only the 29 B codes ruff 0.16 enables by default; the rest of flake8-bugbear needs a separate, deliberate decision.
This commit is contained in:
@@ -187,6 +187,35 @@ line-ending = "lf"
|
||||
select = [ "E4", "E7", "E9", "F" ]
|
||||
extend-select = [
|
||||
"ASYNC", # https://docs.astral.sh/ruff/rules/#flake8-async-async
|
||||
"B002", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B003", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B004", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B005", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B006", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B008", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B009", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B010", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B012", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B013", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B014", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B015", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B016", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B017", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B018", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B019", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B020", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B021", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B022", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B023", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B025", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B026", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B029", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B030", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B031", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B032", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B033", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B035", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"B039", # https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
|
||||
"C4", # https://docs.astral.sh/ruff/rules/#flake8-comprehensions-c4
|
||||
"COM", # https://docs.astral.sh/ruff/rules/#flake8-commas-com
|
||||
"D419", # https://docs.astral.sh/ruff/rules/#pydocstyle-d
|
||||
|
||||
@@ -377,7 +377,7 @@ class Document(SoftDeleteModel, ModelWithOwner): # type: ignore[django-manager-
|
||||
from documents.versioning import versions_newest_first
|
||||
|
||||
if hasattr(self, "effective_content"):
|
||||
return getattr(self, "effective_content")
|
||||
return self.effective_content
|
||||
|
||||
if self.root_document_id is not None or self.pk is None:
|
||||
return self.content
|
||||
|
||||
@@ -1147,7 +1147,7 @@ class DocumentSerializer(
|
||||
def to_representation(self, instance):
|
||||
doc = super().to_representation(instance)
|
||||
if "content" in self.fields and hasattr(instance, "effective_content"):
|
||||
doc["content"] = getattr(instance, "effective_content") or ""
|
||||
doc["content"] = instance.effective_content or ""
|
||||
if self.truncate_content and "content" in self.fields:
|
||||
doc["content"] = doc.get("content")[0:550]
|
||||
return doc
|
||||
@@ -2922,7 +2922,7 @@ class ShareLinkBundleSerializer(OwnedObjectSerializer):
|
||||
return share_link_bundle
|
||||
|
||||
def get_document_count(self, obj: ShareLinkBundle) -> int:
|
||||
return getattr(obj, "document_total") or obj.documents.count()
|
||||
return obj.document_total or obj.documents.count()
|
||||
|
||||
|
||||
class BulkEditObjectsSerializer(SerializerWithPerms, SetPermissionsMixin):
|
||||
|
||||
@@ -777,7 +777,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
|
||||
sig.set.return_value.apply_async.side_effect = Exception("boom")
|
||||
mock_consume_file.return_value = sig
|
||||
|
||||
with self.assertRaises(Exception):
|
||||
with self.assertRaises(Exception): # noqa: B017 - mock injects a bare Exception
|
||||
bulk_edit.merge(doc_ids, delete_originals=True)
|
||||
|
||||
self.doc1.refresh_from_db()
|
||||
@@ -1318,7 +1318,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
|
||||
sig.apply_async.side_effect = Exception("boom")
|
||||
mock_chord.return_value = sig
|
||||
|
||||
with self.assertRaises(Exception):
|
||||
with self.assertRaises(Exception): # noqa: B017 - mock injects a bare Exception
|
||||
bulk_edit.edit_pdf(doc_ids, operations, delete_original=True)
|
||||
|
||||
self.doc2.refresh_from_db()
|
||||
@@ -1430,7 +1430,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
|
||||
{"page": 9999}, # invalid page, forces error during PDF load
|
||||
]
|
||||
with self.assertLogs("paperless.bulk_edit", level="ERROR"):
|
||||
with self.assertRaises(Exception):
|
||||
with self.assertRaises(ValueError):
|
||||
bulk_edit.edit_pdf(doc_ids, operations)
|
||||
mock_group.assert_not_called()
|
||||
mock_consume_file.assert_not_called()
|
||||
|
||||
@@ -783,7 +783,7 @@ class TestClassifier(DirectoriesMixin, TestCase):
|
||||
|
||||
Path(settings.MODEL_FILE).touch()
|
||||
mock_load.side_effect = Exception()
|
||||
with self.assertRaises(Exception):
|
||||
with self.assertRaises(Exception): # noqa: B017 - mock injects a bare Exception
|
||||
load_classifier(raise_exception=True)
|
||||
|
||||
|
||||
|
||||
@@ -2851,7 +2851,7 @@ class TestWorkflows(
|
||||
doc = Document.objects.create(
|
||||
title="test",
|
||||
)
|
||||
self.assertRaises(Exception, document_matches_workflow, doc, w, 99)
|
||||
self.assertRaises(Exception, document_matches_workflow, doc, w, 99) # noqa: B017 - raises a bare Exception for unsupported trigger types
|
||||
|
||||
def test_removal_action_document_updated_workflow(self) -> None:
|
||||
"""
|
||||
|
||||
@@ -167,7 +167,7 @@ def test_get_ai_document_classification_failure(mock_run_llm_query, mock_documen
|
||||
"""
|
||||
mock_run_llm_query.side_effect = Exception("LLM query failed")
|
||||
|
||||
with pytest.raises(Exception):
|
||||
with pytest.raises(Exception): # noqa: B017 - mock injects a bare Exception
|
||||
get_ai_document_classification(mock_document)
|
||||
|
||||
|
||||
|
||||
@@ -184,7 +184,7 @@ class TestMailMessageGpgDecryptor(TestMail):
|
||||
EMAIL_GNUPG_HOME=empty_gpg_home,
|
||||
):
|
||||
message_decryptor = MailMessageDecryptor()
|
||||
self.assertRaises(Exception, message_decryptor.run, encrypted_message)
|
||||
self.assertRaises(Exception, message_decryptor.run, encrypted_message) # noqa: B017 - raises a bare Exception on decryption failure
|
||||
finally:
|
||||
# Clean up the temporary GPG home used only by this test
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user