Chore(mypy): Annotate None returns for typing improvements (#11213)

This commit is contained in:
Sebastian Steinbeißer authored and GitHub committed 2026-02-02 08:44:12 -08:00
1 parent a9c0b06e28
commit 3b5ffbf9fa
113 files changed
+1598 -1510

No files matched your search

+63 -59
View File
@@ -31,7 +31,7 @@ from documents.tests.utils import FileSystemAssertsMixin
class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@override_settings(FILENAME_FORMAT="")
def test_generate_source_filename(self):
def test_generate_source_filename(self) -> None:
document = Document()
document.mime_type = "application/pdf"
document.save()
@@ -39,7 +39,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(generate_filename(document), Path(f"{document.pk:07d}.pdf"))
@override_settings(FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_file_renaming(self):
def test_file_renaming(self) -> None:
document = Document()
document.mime_type = "application/pdf"
document.save()
@@ -78,7 +78,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
)
@override_settings(FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_file_renaming_missing_permissions(self):
def test_file_renaming_missing_permissions(self) -> None:
document = Document()
document.mime_type = "application/pdf"
@@ -112,7 +112,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
(settings.ORIGINALS_DIR / "none").chmod(0o777)
@override_settings(FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_file_renaming_database_error(self):
def test_file_renaming_database_error(self) -> None:
Document.objects.create(
mime_type="application/pdf",
checksum="AAAAA",
@@ -153,7 +153,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(document.filename, "none/none.pdf")
@override_settings(FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_document_delete(self):
def test_document_delete(self) -> None:
document = Document()
document.mime_type = "application/pdf"
@@ -179,7 +179,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
FILENAME_FORMAT="{correspondent}/{correspondent}",
EMPTY_TRASH_DIR=Path(tempfile.mkdtemp()),
)
def test_document_delete_trash_dir(self):
def test_document_delete_trash_dir(self) -> None:
document = Document()
document.mime_type = "application/pdf"
@@ -218,7 +218,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertIsFile(Path(settings.EMPTY_TRASH_DIR) / "none_01.pdf")
@override_settings(FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_document_delete_nofile(self):
def test_document_delete_nofile(self) -> None:
document = Document()
document.mime_type = "application/pdf"
@@ -228,7 +228,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
empty_trash([document.pk])
@override_settings(FILENAME_FORMAT="{correspondent}/{correspondent}")
def test_directory_not_empty(self):
def test_directory_not_empty(self) -> None:
document = Document()
document.mime_type = "application/pdf"
@@ -254,7 +254,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertIsFile(important_file)
@override_settings(FILENAME_FORMAT="{document_type} - {title}")
def test_document_type(self):
def test_document_type(self) -> None:
dt = DocumentType.objects.create(name="my_doc_type")
d = Document.objects.create(title="the_doc", mime_type="application/pdf")
@@ -265,7 +265,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(generate_filename(d), Path("my_doc_type - the_doc.pdf"))
@override_settings(FILENAME_FORMAT="{asn} - {title}")
def test_asn(self):
def test_asn(self) -> None:
d1 = Document.objects.create(
title="the_doc",
mime_type="application/pdf",
@@ -282,7 +282,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(generate_filename(d2), Path("none - the_doc.pdf"))
@override_settings(FILENAME_FORMAT="{title} {tag_list}")
def test_tag_list(self):
def test_tag_list(self) -> None:
doc = Document.objects.create(title="doc1", mime_type="application/pdf")
doc.tags.create(name="tag2")
doc.tags.create(name="tag1")
@@ -298,7 +298,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(generate_filename(doc), Path("doc2.pdf"))
@override_settings(FILENAME_FORMAT="//etc/something/{title}")
def test_filename_relative(self):
def test_filename_relative(self) -> None:
doc = Document.objects.create(title="doc1", mime_type="application/pdf")
doc.filename = generate_filename(doc)
doc.save()
@@ -311,7 +311,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@override_settings(
FILENAME_FORMAT="{created_year}-{created_month}-{created_day}",
)
def test_created_year_month_day(self):
def test_created_year_month_day(self) -> None:
d1 = timezone.make_aware(datetime.datetime(2020, 3, 6, 1, 1, 1))
doc1 = Document.objects.create(
title="doc1",
@@ -328,7 +328,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@override_settings(
FILENAME_FORMAT="{added_year}-{added_month}-{added_day}",
)
def test_added_year_month_day(self):
def test_added_year_month_day(self) -> None:
d1 = timezone.make_aware(datetime.datetime(232, 1, 9, 1, 1, 1))
doc1 = Document.objects.create(
title="doc1",
@@ -345,7 +345,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@override_settings(
FILENAME_FORMAT="{correspondent}/{correspondent}/{correspondent}",
)
def test_nested_directory_cleanup(self):
def test_nested_directory_cleanup(self) -> None:
document = Document()
document.mime_type = "application/pdf"
@@ -372,7 +372,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertIsDir(settings.ORIGINALS_DIR)
@override_settings(FILENAME_FORMAT="{doc_pk}")
def test_format_doc_pk(self):
def test_format_doc_pk(self) -> None:
document = Document()
document.pk = 1
document.mime_type = "application/pdf"
@@ -384,14 +384,14 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(generate_filename(document), Path("0013579.pdf"))
@override_settings(FILENAME_FORMAT=None)
def test_format_none(self):
def test_format_none(self) -> None:
document = Document()
document.pk = 1
document.mime_type = "application/pdf"
self.assertEqual(generate_filename(document), Path("0000001.pdf"))
def test_try_delete_empty_directories(self):
def test_try_delete_empty_directories(self) -> None:
# Create our working directory
tmp: Path = settings.ORIGINALS_DIR / "test_delete_empty"
tmp.mkdir(exist_ok=True, parents=True)
@@ -409,7 +409,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertIsNotDir(tmp / "notempty" / "empty")
@override_settings(FILENAME_FORMAT="{% if x is None %}/{title]")
def test_invalid_format(self):
def test_invalid_format(self) -> None:
document = Document()
document.pk = 1
document.mime_type = "application/pdf"
@@ -417,7 +417,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(generate_filename(document), Path("0000001.pdf"))
@override_settings(FILENAME_FORMAT="{created__year}")
def test_invalid_format_key(self):
def test_invalid_format_key(self) -> None:
document = Document()
document.pk = 1
document.mime_type = "application/pdf"
@@ -425,7 +425,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertEqual(generate_filename(document), Path("0000001.pdf"))
@override_settings(FILENAME_FORMAT="{title}")
def test_duplicates(self):
def test_duplicates(self) -> None:
document = Document.objects.create(
mime_type="application/pdf",
title="qwe",
@@ -479,7 +479,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@override_settings(FILENAME_FORMAT="{title}")
@mock.patch("documents.signals.handlers.Document.objects.filter")
@mock.patch("documents.signals.handlers.shutil.move")
def test_no_move_only_save(self, mock_move, mock_filter):
def test_no_move_only_save(self, mock_move, mock_filter) -> None:
"""
GIVEN:
- A document with a filename
@@ -516,7 +516,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
CELERY_TASK_ALWAYS_EAGER=True,
)
@mock.patch("documents.signals.handlers.update_filename_and_move_files")
def test_select_cf_updated(self, m):
def test_select_cf_updated(self, m) -> None:
"""
GIVEN:
- A document with a select type custom field
@@ -569,7 +569,7 @@ class TestFileHandling(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
@override_settings(FILENAME_FORMAT=None)
def test_create_no_format(self):
def test_create_no_format(self) -> None:
original = settings.ORIGINALS_DIR / "0000001.pdf"
archive = settings.ARCHIVE_DIR / "0000001.pdf"
Path(original).touch()
@@ -588,7 +588,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def test_create_with_format(self):
def test_create_with_format(self) -> None:
original = settings.ORIGINALS_DIR / "0000001.pdf"
archive = settings.ARCHIVE_DIR / "0000001.pdf"
Path(original).touch()
@@ -616,7 +616,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def test_move_archive_gone(self):
def test_move_archive_gone(self) -> None:
original = settings.ORIGINALS_DIR / "0000001.pdf"
archive = settings.ARCHIVE_DIR / "0000001.pdf"
Path(original).touch()
@@ -635,7 +635,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
self.assertIsNotFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def test_move_archive_exists(self):
def test_move_archive_exists(self) -> None:
original = settings.ORIGINALS_DIR / "0000001.pdf"
archive = settings.ARCHIVE_DIR / "0000001.pdf"
existing_archive_file = settings.ARCHIVE_DIR / "none" / "my_doc.pdf"
@@ -660,7 +660,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
self.assertEqual(doc.archive_filename, "none/my_doc_01.pdf")
@override_settings(FILENAME_FORMAT="{title}")
def test_move_original_only(self):
def test_move_original_only(self) -> None:
original = settings.ORIGINALS_DIR / "document_01.pdf"
archive = settings.ARCHIVE_DIR / "document.pdf"
Path(original).touch()
@@ -682,7 +682,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{title}")
def test_move_archive_only(self):
def test_move_archive_only(self) -> None:
original = settings.ORIGINALS_DIR / "document.pdf"
archive = settings.ARCHIVE_DIR / "document_01.pdf"
Path(original).touch()
@@ -705,8 +705,8 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
@mock.patch("documents.signals.handlers.shutil.move")
def test_move_archive_error(self, m):
def fake_rename(src, dst):
def test_move_archive_error(self, m) -> None:
def fake_rename(src, dst) -> None:
if "archive" in str(src):
raise OSError
else:
@@ -735,7 +735,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def test_move_file_gone(self):
def test_move_file_gone(self) -> None:
original = settings.ORIGINALS_DIR / "0000001.pdf"
archive = settings.ARCHIVE_DIR / "0000001.pdf"
# Path(original).touch()
@@ -756,8 +756,8 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
@mock.patch("documents.signals.handlers.shutil.move")
def test_move_file_error(self, m):
def fake_rename(src, dst):
def test_move_file_error(self, m) -> None:
def fake_rename(src, dst) -> None:
if "original" in str(src):
raise OSError
else:
@@ -786,7 +786,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
self.assertIsFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="")
def test_archive_deleted(self):
def test_archive_deleted(self) -> None:
original = settings.ORIGINALS_DIR / "0000001.pdf"
archive = settings.ARCHIVE_DIR / "0000001.pdf"
Path(original).touch()
@@ -814,7 +814,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
self.assertIsNotFile(doc.archive_path)
@override_settings(FILENAME_FORMAT="{title}")
def test_archive_deleted2(self):
def test_archive_deleted2(self) -> None:
original = settings.ORIGINALS_DIR / "document.webp"
original2 = settings.ORIGINALS_DIR / "0000001.pdf"
archive = settings.ARCHIVE_DIR / "0000001.pdf"
@@ -849,7 +849,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
self.assertIsNotFile(doc2.source_path)
@override_settings(FILENAME_FORMAT="{correspondent}/{title}")
def test_database_error(self):
def test_database_error(self) -> None:
original = settings.ORIGINALS_DIR / "0000001.pdf"
archive = settings.ARCHIVE_DIR / "0000001.pdf"
Path(original).touch()
@@ -876,7 +876,7 @@ class TestFileHandlingWithArchive(DirectoriesMixin, FileSystemAssertsMixin, Test
class TestFilenameGeneration(DirectoriesMixin, TestCase):
@override_settings(FILENAME_FORMAT="{title}")
def test_invalid_characters(self):
def test_invalid_characters(self) -> None:
doc = Document.objects.create(
title="This. is the title.",
mime_type="application/pdf",
@@ -894,7 +894,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
self.assertEqual(generate_filename(doc), Path("my-invalid-..-title-yay.pdf"))
@override_settings(FILENAME_FORMAT="{created}")
def test_date(self):
def test_date(self) -> None:
doc = Document.objects.create(
title="does not matter",
created=datetime.date(2020, 5, 21),
@@ -904,7 +904,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
)
self.assertEqual(generate_filename(doc), Path("2020-05-21.pdf"))
def test_dynamic_path(self):
def test_dynamic_path(self) -> None:
"""
GIVEN:
- A document with a defined storage path
@@ -923,7 +923,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
)
self.assertEqual(generate_filename(doc), Path("TestFolder/2020-06-25.pdf"))
def test_dynamic_path_with_none(self):
def test_dynamic_path_with_none(self) -> None:
"""
GIVEN:
- A document with a defined storage path
@@ -947,7 +947,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
@override_settings(
FILENAME_FORMAT_REMOVE_NONE=True,
)
def test_dynamic_path_remove_none(self):
def test_dynamic_path_remove_none(self) -> None:
"""
GIVEN:
- A document with a defined storage path
@@ -981,7 +981,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
sp.save()
self.assertEqual(generate_filename(doc), Path("2020/does not matter.pdf"))
def test_multiple_doc_paths(self):
def test_multiple_doc_paths(self) -> None:
"""
GIVEN:
- Two documents, each with different storage paths
@@ -1026,7 +1026,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
@override_settings(
FILENAME_FORMAT=None,
)
def test_no_path_fallback(self):
def test_no_path_fallback(self) -> None:
"""
GIVEN:
- Two documents, one with defined storage path, the other not
@@ -1071,7 +1071,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
"{{ correspondent or 'missing' }}/{{ title }}"
),
)
def test_placeholder_matches_none_variants_and_false(self):
def test_placeholder_matches_none_variants_and_false(self) -> None:
"""
GIVEN:
- Templates that compare against 'none', '-none-' and rely on truthiness
@@ -1106,7 +1106,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
@override_settings(
FILENAME_FORMAT="{created_year_short}/{created_month_name_short}/{created_month_name}/{title}",
)
def test_short_names_created(self):
def test_short_names_created(self) -> None:
doc = Document.objects.create(
title="The Title",
created=datetime.date(1989, 12, 2),
@@ -1119,7 +1119,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
@override_settings(
FILENAME_FORMAT="{added_year_short}/{added_month_name}/{added_month_name_short}/{title}",
)
def test_short_names_added(self):
def test_short_names_added(self) -> None:
doc = Document.objects.create(
title="The Title",
added=timezone.make_aware(datetime.datetime(1984, 8, 21, 7, 36, 51, 153)),
@@ -1132,7 +1132,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
@override_settings(
FILENAME_FORMAT="{owner_username}/{title}",
)
def test_document_owner_string(self):
def test_document_owner_string(self) -> None:
"""
GIVEN:
- Document with an other
@@ -1166,7 +1166,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
@override_settings(
FILENAME_FORMAT="{original_name}",
)
def test_document_original_filename(self):
def test_document_original_filename(self) -> None:
"""
GIVEN:
- Document with an original filename
@@ -1226,7 +1226,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
FILENAME_FORMAT="XX{correspondent}/{title}",
FILENAME_FORMAT_REMOVE_NONE=True,
)
def test_remove_none_not_dir(self):
def test_remove_none_not_dir(self) -> None:
"""
GIVEN:
- A document with & filename format that includes correspondent as part of directory name
@@ -1247,7 +1247,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
document.filename = generate_filename(document)
self.assertEqual(document.filename, Path("XX/doc1.pdf"))
def test_complex_template_strings(self):
def test_complex_template_strings(self) -> None:
"""
GIVEN:
- Storage paths with complex conditionals and logic
@@ -1326,7 +1326,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
@override_settings(
FILENAME_FORMAT="{{creation_date}}/{{ title_name_str }}",
)
def test_template_with_undefined_var(self):
def test_template_with_undefined_var(self) -> None:
"""
GIVEN:
- Filename format with one or more undefined variables
@@ -1361,7 +1361,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
@override_settings(
FILENAME_FORMAT="{{created}}/{{ document.save() }}",
)
def test_template_with_security(self):
def test_template_with_security(self) -> None:
"""
GIVEN:
- Filename format with one or more undefined variables
@@ -1393,7 +1393,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
"WARNING:paperless.templating:Template attempted restricted operation: <bound method Model.save of <Document: 2020-06-25 Does Matter>> is not safely callable",
)
def test_template_with_custom_fields(self):
def test_template_with_custom_fields(self) -> None:
"""
GIVEN:
- Filename format which accesses custom field data
@@ -1499,7 +1499,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
Path("invoices/0.pdf"),
)
def test_datetime_filter(self):
def test_datetime_filter(self) -> None:
"""
GIVEN:
- Filename format with datetime filter
@@ -1554,7 +1554,7 @@ class TestFilenameGeneration(DirectoriesMixin, TestCase):
Path("2024-10-01/Some Title.pdf"),
)
def test_slugify_filter(self):
def test_slugify_filter(self) -> None:
"""
GIVEN:
- Filename format with slugify filter
@@ -1638,7 +1638,7 @@ class TestCustomFieldFilenameUpdates(
return super().setUp()
@override_settings(FILENAME_FORMAT=None)
def test_custom_field_not_in_template_skips_filename_work(self):
def test_custom_field_not_in_template_skips_filename_work(self) -> None:
storage_path = StoragePath.objects.create(path="{{created}}/{{ title }}")
self.doc.storage_path = storage_path
self.doc.save()
@@ -1658,7 +1658,7 @@ class TestCustomFieldFilenameUpdates(
self.assertEqual(m.call_count, 0)
@override_settings(FILENAME_FORMAT=None)
def test_custom_field_in_template_triggers_filename_update(self):
def test_custom_field_in_template_triggers_filename_update(self) -> None:
storage_path = StoragePath.objects.create(
path="{{ custom_fields|get_cf_value('flavor') }}/{{ title }}",
)
@@ -1710,7 +1710,11 @@ class TestPathDateLocalization:
),
],
)
def test_localize_date_path_building(self, filename_format, expected_filename):
def test_localize_date_path_building(
self,
filename_format,
expected_filename,
) -> None:
document = DocumentFactory.create(
title="My Document",
mime_type="application/pdf",