Skip to content

Commit 2b19ac0

Browse files
committed
Fix various type hints
1 parent 0ec2e4d commit 2b19ac0

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

pictures/models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def name(self) -> str:
105105
def path(self) -> Path:
106106
return Path(self.storage.path(self.name))
107107

108-
def process(self, image) -> Image:
108+
def process(self, image) -> Image.Image:
109109
image = ImageOps.exif_transpose(image) # crates a copy
110110
height = self.height or self.width / Fraction(*image.size)
111111
size = math.floor(self.width), math.floor(height)
@@ -130,7 +130,9 @@ def delete(self):
130130

131131

132132
class PictureFieldFile(ImageFieldFile):
133-
def __xor__(self, other) -> tuple[set[Picture], set[Picture]]:
133+
def __xor__(
134+
self, other
135+
) -> tuple[set[Picture], set[Picture]] | type[NotImplemented]:
134136
"""Return the new and obsolete :class:`Picture` instances."""
135137
if not isinstance(other, PictureFieldFile):
136138
return NotImplemented
@@ -192,7 +194,7 @@ def height(self):
192194
return self._get_image_dimensions()[1]
193195

194196
@property
195-
def aspect_ratios(self) -> {Fraction | None: {str: {int: Picture}}}:
197+
def aspect_ratios(self) -> dict[Fraction | None, dict[str, dict[int, Picture]]]:
196198
self._require_file()
197199
return self.get_picture_files(
198200
file_name=self.name,
@@ -210,7 +212,7 @@ def get_picture_files(
210212
img_height: int,
211213
storage: Storage,
212214
field: PictureField,
213-
) -> {Fraction | None: {str: {int: Picture}}}:
215+
) -> dict[Fraction | None, dict[str, dict[int, Picture]]]:
214216
PictureClass = import_string(conf.get_settings().PICTURE_CLASS)
215217
return {
216218
ratio: {

pictures/templatetags/pictures.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ def picture(field_file, img_alt=None, ratio=None, container=None, **kwargs):
2020
img_attrs = {
2121
"src": field_file.url,
2222
"alt": img_alt,
23-
"width": field_file.width,
24-
"height": field_file.height,
2523
}
24+
if ratio is None:
25+
img_attrs |= {
26+
"width": field_file.width,
27+
"height": field_file.height,
28+
}
2629
try:
2730
sources = field_file.aspect_ratios[ratio]
2831
except KeyError as e:

pictures/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def sizes(
6161

6262

6363
def source_set(
64-
size: (int, int), *, ratio: str | Fraction | None, max_width: int, cols: int
64+
size: tuple[int, int], *, ratio: str | Fraction | None, max_width: int, cols: int
6565
) -> set:
6666
ratio = Fraction(ratio) if ratio else None
6767
img_width, img_height = size
@@ -82,8 +82,8 @@ def placeholder(width: int, height: int, alt):
8282
hue = random.randint(0, 360) # NoQA S311
8383
img = Image.new("RGB", (width, height), color=f"hsl({hue}, 40%, 80%)")
8484
draw = ImageDraw.Draw(img)
85-
draw.line(((0, 0, width, height)), width=3, fill=f"hsl({hue}, 60%, 20%)")
86-
draw.line(((0, height, width, 0)), width=3, fill=f"hsl({hue}, 60%, 20%)")
85+
draw.line((0, 0, width, height), width=3, fill=f"hsl({hue}, 60%, 20%)")
86+
draw.line((0, height, width, 0), width=3, fill=f"hsl({hue}, 60%, 20%)")
8787
draw.rectangle(
8888
(width / 4, height / 4, width * 3 / 4, height * 3 / 4),
8989
fill=f"hsl({hue}, 40%, 80%)",

tests/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ def test_placeholder():
145145
assert img.height == 1200
146146

147147

148-
class TestPicture(Picture):
148+
class SamplePicture(Picture):
149149
@property
150150
def url(self):
151151
return f"/media/{self.parent_name}"
152152

153153

154154
def test_reconstruct(image_upload_file):
155-
picture = TestPicture(
155+
picture = SamplePicture(
156156
image_upload_file.name,
157157
"WEBP",
158158
"16/9",
@@ -173,7 +173,7 @@ def test_reconstruct(image_upload_file):
173173
"storage": default_storage,
174174
"width": 100,
175175
},
176-
) == TestPicture(
176+
) == SamplePicture(
177177
"test.jpg",
178178
"JPEG",
179179
"16/9",

0 commit comments

Comments
 (0)