Runs pyupgrade to Python 3.8+ and adds a hook for it

This commit is contained in:
Trenton Holmes
2022-05-06 09:04:08 -07:00
parent 6d9a4d76f2
commit f62193099c
34 changed files with 113 additions and 109 deletions

View File

@@ -16,7 +16,7 @@ class TestDocumentAdmin(DirectoriesMixin, TestCase):
return searcher.document(id=doc.id)
def setUp(self) -> None:
super(TestDocumentAdmin, self).setUp()
super().setUp()
self.doc_admin = DocumentAdmin(model=Document, admin_site=AdminSite())
def test_save_model(self):

View File

@@ -27,7 +27,7 @@ from whoosh.writing import AsyncWriter
class TestDocumentApi(DirectoriesMixin, APITestCase):
def setUp(self):
super(TestDocumentApi, self).setUp()
super().setUp()
self.user = User.objects.create_superuser(username="temp_admin")
self.client.force_login(user=self.user)
@@ -70,7 +70,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
returned_doc["title"] = "the new title"
response = self.client.put(
"/api/documents/{}/".format(doc.pk),
f"/api/documents/{doc.pk}/",
returned_doc,
format="json",
)
@@ -82,7 +82,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
self.assertEqual(doc_after_save.correspondent, c2)
self.assertEqual(doc_after_save.title, "the new title")
self.client.delete("/api/documents/{}/".format(doc_after_save.pk))
self.client.delete(f"/api/documents/{doc_after_save.pk}/")
self.assertEqual(len(Document.objects.all()), 0)
@@ -163,22 +163,22 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
)
with open(
os.path.join(self.dirs.thumbnail_dir, "{:07d}.png".format(doc.pk)),
os.path.join(self.dirs.thumbnail_dir, f"{doc.pk:07d}.png"),
"wb",
) as f:
f.write(content_thumbnail)
response = self.client.get("/api/documents/{}/download/".format(doc.pk))
response = self.client.get(f"/api/documents/{doc.pk}/download/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, content)
response = self.client.get("/api/documents/{}/preview/".format(doc.pk))
response = self.client.get(f"/api/documents/{doc.pk}/preview/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, content)
response = self.client.get("/api/documents/{}/thumb/".format(doc.pk))
response = self.client.get(f"/api/documents/{doc.pk}/thumb/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, content_thumbnail)
@@ -202,25 +202,25 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
with open(doc.archive_path, "wb") as f:
f.write(content_archive)
response = self.client.get("/api/documents/{}/download/".format(doc.pk))
response = self.client.get(f"/api/documents/{doc.pk}/download/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, content_archive)
response = self.client.get(
"/api/documents/{}/download/?original=true".format(doc.pk),
f"/api/documents/{doc.pk}/download/?original=true",
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, content)
response = self.client.get("/api/documents/{}/preview/".format(doc.pk))
response = self.client.get(f"/api/documents/{doc.pk}/preview/")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, content_archive)
response = self.client.get(
"/api/documents/{}/preview/?original=true".format(doc.pk),
f"/api/documents/{doc.pk}/preview/?original=true",
)
self.assertEqual(response.status_code, 200)
@@ -234,13 +234,13 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
mime_type="application/pdf",
)
response = self.client.get("/api/documents/{}/download/".format(doc.pk))
response = self.client.get(f"/api/documents/{doc.pk}/download/")
self.assertEqual(response.status_code, 404)
response = self.client.get("/api/documents/{}/preview/".format(doc.pk))
response = self.client.get(f"/api/documents/{doc.pk}/preview/")
self.assertEqual(response.status_code, 404)
response = self.client.get("/api/documents/{}/thumb/".format(doc.pk))
response = self.client.get(f"/api/documents/{doc.pk}/thumb/")
self.assertEqual(response.status_code, 404)
def test_document_filters(self):
@@ -283,7 +283,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc2.id, doc3.id])
response = self.client.get(
"/api/documents/?tags__id__in={},{}".format(tag_inbox.id, tag_3.id),
f"/api/documents/?tags__id__in={tag_inbox.id},{tag_3.id}",
)
self.assertEqual(response.status_code, 200)
results = response.data["results"]
@@ -291,7 +291,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc1.id, doc3.id])
response = self.client.get(
"/api/documents/?tags__id__in={},{}".format(tag_2.id, tag_3.id),
f"/api/documents/?tags__id__in={tag_2.id},{tag_3.id}",
)
self.assertEqual(response.status_code, 200)
results = response.data["results"]
@@ -299,7 +299,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc2.id, doc3.id])
response = self.client.get(
"/api/documents/?tags__id__all={},{}".format(tag_2.id, tag_3.id),
f"/api/documents/?tags__id__all={tag_2.id},{tag_3.id}",
)
self.assertEqual(response.status_code, 200)
results = response.data["results"]
@@ -307,27 +307,27 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
self.assertEqual(results[0]["id"], doc3.id)
response = self.client.get(
"/api/documents/?tags__id__all={},{}".format(tag_inbox.id, tag_3.id),
f"/api/documents/?tags__id__all={tag_inbox.id},{tag_3.id}",
)
self.assertEqual(response.status_code, 200)
results = response.data["results"]
self.assertEqual(len(results), 0)
response = self.client.get(
"/api/documents/?tags__id__all={}a{}".format(tag_inbox.id, tag_3.id),
f"/api/documents/?tags__id__all={tag_inbox.id}a{tag_3.id}",
)
self.assertEqual(response.status_code, 200)
results = response.data["results"]
self.assertEqual(len(results), 3)
response = self.client.get("/api/documents/?tags__id__none={}".format(tag_3.id))
response = self.client.get(f"/api/documents/?tags__id__none={tag_3.id}")
self.assertEqual(response.status_code, 200)
results = response.data["results"]
self.assertEqual(len(results), 2)
self.assertCountEqual([results[0]["id"], results[1]["id"]], [doc1.id, doc2.id])
response = self.client.get(
"/api/documents/?tags__id__none={},{}".format(tag_3.id, tag_2.id),
f"/api/documents/?tags__id__none={tag_3.id},{tag_2.id}",
)
self.assertEqual(response.status_code, 200)
results = response.data["results"]
@@ -335,7 +335,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
self.assertEqual(results[0]["id"], doc1.id)
response = self.client.get(
"/api/documents/?tags__id__none={},{}".format(tag_2.id, tag_inbox.id),
f"/api/documents/?tags__id__none={tag_2.id},{tag_inbox.id}",
)
self.assertEqual(response.status_code, 200)
results = response.data["results"]
@@ -1284,7 +1284,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
class TestDocumentApiV2(DirectoriesMixin, APITestCase):
def setUp(self):
super(TestDocumentApiV2, self).setUp()
super().setUp()
self.user = User.objects.create_superuser(username="temp_admin")
@@ -1365,7 +1365,7 @@ class TestDocumentApiV2(DirectoriesMixin, APITestCase):
class TestBulkEdit(DirectoriesMixin, APITestCase):
def setUp(self):
super(TestBulkEdit, self).setUp()
super().setUp()
user = User.objects.create_superuser(username="temp_admin")
self.client.force_login(user=user)
@@ -1886,7 +1886,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
class TestBulkDownload(DirectoriesMixin, APITestCase):
def setUp(self):
super(TestBulkDownload, self).setUp()
super().setUp()
user = User.objects.create_superuser(username="temp_admin")
self.client.force_login(user=user)

View File

@@ -19,7 +19,7 @@ from documents.tests.utils import DirectoriesMixin
class TestClassifier(DirectoriesMixin, TestCase):
def setUp(self):
super(TestClassifier, self).setUp()
super().setUp()
self.classifier = DocumentClassifier()
def generate_test_data(self):

View File

@@ -41,7 +41,7 @@ class TestAttributes(TestCase):
self.assertEqual(file_info.title, title, filename)
self.assertEqual(tuple([t.name for t in file_info.tags]), tags, filename)
self.assertEqual(tuple(t.name for t in file_info.tags), tags, filename)
def test_guess_attributes_from_name_when_title_starts_with_dash(self):
self._test_guess_attributes_from_name(
@@ -176,7 +176,7 @@ class DummyParser(DocumentParser):
raise NotImplementedError()
def __init__(self, logging_group, scratch_dir, archive_path):
super(DummyParser, self).__init__(logging_group, None)
super().__init__(logging_group, None)
_, self.fake_thumb = tempfile.mkstemp(suffix=".png", dir=scratch_dir)
self.archive_path = archive_path
@@ -195,7 +195,7 @@ class CopyParser(DocumentParser):
return self.fake_thumb
def __init__(self, logging_group, progress_callback=None):
super(CopyParser, self).__init__(logging_group, progress_callback)
super().__init__(logging_group, progress_callback)
_, self.fake_thumb = tempfile.mkstemp(suffix=".png", dir=self.tempdir)
def parse(self, document_path, mime_type, file_name=None):
@@ -210,7 +210,7 @@ class FaultyParser(DocumentParser):
raise NotImplementedError()
def __init__(self, logging_group, scratch_dir):
super(FaultyParser, self).__init__(logging_group)
super().__init__(logging_group)
_, self.fake_thumb = tempfile.mkstemp(suffix=".png", dir=scratch_dir)
def get_optimised_thumbnail(self, document_path, mime_type, file_name=None):
@@ -270,7 +270,7 @@ class TestConsumer(DirectoriesMixin, TestCase):
return FaultyParser(logging_group, self.dirs.scratch_dir)
def setUp(self):
super(TestConsumer, self).setUp()
super().setUp()
patcher = mock.patch("documents.parsers.document_consumer_declaration.send")
m = patcher.start()

View File

@@ -16,7 +16,7 @@ class TestDate(TestCase):
os.path.dirname(__file__),
"../../paperless_tesseract/tests/samples",
)
SCRATCH = "/tmp/paperless-tests-{}".format(str(uuid4())[:8])
SCRATCH = f"/tmp/paperless-tests-{str(uuid4())[:8]}"
def setUp(self):
os.makedirs(self.SCRATCH, exist_ok=True)

View File

@@ -32,12 +32,12 @@ class TestFileHandling(DirectoriesMixin, TestCase):
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
document.save()
self.assertEqual(generate_filename(document), "{:07d}.pdf".format(document.pk))
self.assertEqual(generate_filename(document), f"{document.pk:07d}.pdf")
document.storage_type = Document.STORAGE_TYPE_GPG
self.assertEqual(
generate_filename(document),
"{:07d}.pdf.gpg".format(document.pk),
f"{document.pk:07d}.pdf.gpg",
)
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{correspondent}")
@@ -50,7 +50,7 @@ class TestFileHandling(DirectoriesMixin, TestCase):
# Test default source_path
self.assertEqual(
document.source_path,
settings.ORIGINALS_DIR + "/{:07d}.pdf".format(document.pk),
settings.ORIGINALS_DIR + f"/{document.pk:07d}.pdf",
)
document.filename = generate_filename(document)

View File

@@ -39,7 +39,7 @@ class ConsumerMixin:
sample_file = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")
def setUp(self) -> None:
super(ConsumerMixin, self).setUp()
super().setUp()
self.t = None
patcher = mock.patch(
"documents.management.commands.document_consumer.async_task",
@@ -60,7 +60,7 @@ class ConsumerMixin:
# wait for the consumer to exit.
self.t.join()
super(ConsumerMixin, self).tearDown()
super().tearDown()
def wait_for_task_mock_call(self, excpeted_call_count=1):
n = 0

View File

@@ -65,7 +65,7 @@ class TestExportImport(DirectoriesMixin, TestCase):
self.d1.correspondent = self.c1
self.d1.document_type = self.dt1
self.d1.save()
super(TestExportImport, self).setUp()
super().setUp()
def _get_document_from_manifest(self, manifest, id):
f = list(

View File

@@ -82,7 +82,7 @@ class TestRetagger(DirectoriesMixin, TestCase):
)
def setUp(self) -> None:
super(TestRetagger, self).setUp()
super().setUp()
self.make_models()
def test_add_tags(self):

View File

@@ -39,7 +39,7 @@ class TestMakeThumbnails(DirectoriesMixin, TestCase):
)
def setUp(self) -> None:
super(TestMakeThumbnails, self).setUp()
super().setUp()
self.make_models()
def test_process_document(self):

View File

@@ -36,13 +36,13 @@ class _TestMatchingBase(TestCase):
doc = Document(content=string)
self.assertTrue(
matching.matches(instance, doc),
'"%s" should match "%s" but it does not' % (match_text, string),
f'"{match_text}" should match "{string}" but it does not',
)
for string in no_match:
doc = Document(content=string)
self.assertFalse(
matching.matches(instance, doc),
'"%s" should not match "%s" but it does' % (match_text, string),
f'"{match_text}" should not match "{string}" but it does',
)

View File

@@ -22,7 +22,7 @@ def archive_path_old(self):
if self.filename:
fname = archive_name_from_filename(self.filename)
else:
fname = "{:07}.pdf".format(self.pk)
fname = f"{self.pk:07}.pdf"
return os.path.join(settings.ARCHIVE_DIR, fname)
@@ -38,7 +38,7 @@ def source_path(doc):
if doc.filename:
fname = str(doc.filename)
else:
fname = "{:07}{}".format(doc.pk, doc.file_type)
fname = f"{doc.pk:07}{doc.file_type}"
if doc.storage_type == STORAGE_TYPE_GPG:
fname += ".gpg" # pragma: no cover
@@ -46,7 +46,7 @@ def source_path(doc):
def thumbnail_path(doc):
file_name = "{:07}.png".format(doc.pk)
file_name = f"{doc.pk:07}.png"
if doc.storage_type == STORAGE_TYPE_GPG:
file_name += ".gpg"

View File

@@ -15,7 +15,7 @@ def source_path_before(self):
if self.filename:
fname = str(self.filename)
else:
fname = "{:07}.{}".format(self.pk, self.file_type)
fname = f"{self.pk:07}.{self.file_type}"
if self.storage_type == STORAGE_TYPE_GPG:
fname += ".gpg"
@@ -30,7 +30,7 @@ def source_path_after(doc):
if doc.filename:
fname = str(doc.filename)
else:
fname = "{:07}{}".format(doc.pk, file_type_after(doc))
fname = f"{doc.pk:07}{file_type_after(doc)}"
if doc.storage_type == STORAGE_TYPE_GPG:
fname += ".gpg" # pragma: no cover

View File

@@ -31,7 +31,7 @@ def fake_magic_from_file(file, mime=False):
class TestParserDiscovery(TestCase):
@mock.patch("documents.parsers.document_consumer_declaration.send")
def test__get_parser_class_1_parser(self, m, *args):
class DummyParser(object):
class DummyParser:
pass
m.return_value = (
@@ -49,10 +49,10 @@ class TestParserDiscovery(TestCase):
@mock.patch("documents.parsers.document_consumer_declaration.send")
def test__get_parser_class_n_parsers(self, m, *args):
class DummyParser1(object):
class DummyParser1:
pass
class DummyParser2(object):
class DummyParser2:
pass
m.return_value = (

View File

@@ -76,10 +76,10 @@ class DirectoriesMixin:
def setUp(self) -> None:
self.dirs = setup_directories()
super(DirectoriesMixin, self).setUp()
super().setUp()
def tearDown(self) -> None:
super(DirectoriesMixin, self).tearDown()
super().tearDown()
remove_dirs(self.dirs)
@@ -93,7 +93,7 @@ class TestMigrations(TransactionTestCase):
auto_migrate = True
def setUp(self):
super(TestMigrations, self).setUp()
super().setUp()
assert (
self.migrate_from and self.migrate_to