diff --git a/pictures/models.py b/pictures/models.py index 77500d6..6413c2d 100644 --- a/pictures/models.py +++ b/pictures/models.py @@ -6,6 +6,7 @@ import math from fractions import Fraction from pathlib import Path +from types import NotImplementedType from django.core import checks from django.core.files.base import ContentFile @@ -105,7 +106,7 @@ def name(self) -> str: def path(self) -> Path: return Path(self.storage.path(self.name)) - def process(self, image) -> Image: + def process(self, image) -> Image.Image: image = ImageOps.exif_transpose(image) # crates a copy height = self.height or self.width / Fraction(*image.size) size = math.floor(self.width), math.floor(height) @@ -130,7 +131,7 @@ def delete(self): class PictureFieldFile(ImageFieldFile): - def __xor__(self, other) -> tuple[set[Picture], set[Picture]]: + def __xor__(self, other) -> tuple[set[Picture], set[Picture]] | NotImplementedType: """Return the new and obsolete :class:`Picture` instances.""" if not isinstance(other, PictureFieldFile): return NotImplemented @@ -192,7 +193,7 @@ def height(self): return self._get_image_dimensions()[1] @property - def aspect_ratios(self) -> {Fraction | None: {str: {int: Picture}}}: + def aspect_ratios(self) -> dict[Fraction | None, dict[str, dict[int, Picture]]]: self._require_file() return self.get_picture_files( file_name=self.name, @@ -210,7 +211,7 @@ def get_picture_files( img_height: int, storage: Storage, field: PictureField, - ) -> {Fraction | None: {str: {int: Picture}}}: + ) -> dict[Fraction | None, dict[str, dict[int, Picture]]]: PictureClass = import_string(conf.get_settings().PICTURE_CLASS) return { ratio: { diff --git a/pictures/utils.py b/pictures/utils.py index e088042..f7e605a 100644 --- a/pictures/utils.py +++ b/pictures/utils.py @@ -61,7 +61,7 @@ def sizes( def source_set( - size: (int, int), *, ratio: str | Fraction | None, max_width: int, cols: int + size: tuple[int, int], *, ratio: str | Fraction | None, max_width: int, cols: int ) -> set: ratio = Fraction(ratio) if ratio else None img_width, img_height = size @@ -82,8 +82,8 @@ def placeholder(width: int, height: int, alt): hue = random.randint(0, 360) # NoQA S311 img = Image.new("RGB", (width, height), color=f"hsl({hue}, 40%, 80%)") draw = ImageDraw.Draw(img) - draw.line(((0, 0, width, height)), width=3, fill=f"hsl({hue}, 60%, 20%)") - draw.line(((0, height, width, 0)), width=3, fill=f"hsl({hue}, 60%, 20%)") + draw.line((0, 0, width, height), width=3, fill=f"hsl({hue}, 60%, 20%)") + draw.line((0, height, width, 0), width=3, fill=f"hsl({hue}, 60%, 20%)") draw.rectangle( (width / 4, height / 4, width * 3 / 4, height * 3 / 4), fill=f"hsl({hue}, 40%, 80%)", diff --git a/tests/test_utils.py b/tests/test_utils.py index 3ffc3f0..0b4ef34 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -145,14 +145,14 @@ def test_placeholder(): assert img.height == 1200 -class TestPicture(Picture): +class SamplePicture(Picture): @property def url(self): return f"/media/{self.parent_name}" def test_reconstruct(image_upload_file): - picture = TestPicture( + picture = SamplePicture( image_upload_file.name, "WEBP", "16/9", @@ -164,7 +164,7 @@ def test_reconstruct(image_upload_file): assert isinstance(reconstructed, Storage) assert utils.reconstruct( - "tests.test_utils.TestPicture", + "tests.test_utils.SamplePicture", [], { "parent_name": "test.jpg", @@ -173,7 +173,7 @@ def test_reconstruct(image_upload_file): "storage": default_storage, "width": 100, }, - ) == TestPicture( + ) == SamplePicture( "test.jpg", "JPEG", "16/9",