Skip to content

Commit 6e2ebaa

Browse files
authored
Merge pull request #8474 from python-pillow/renovate/mypy-1.x
Update dependency mypy to v1.12.0
2 parents 11c654c + 4611a24 commit 6e2ebaa

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

.ci/requirements-mypy.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
mypy==1.11.2
1+
mypy==1.12.0
22
IceSpringPySideStubs-PyQt6
33
IceSpringPySideStubs-PySide6
44
ipython

src/PIL/TiffImagePlugin.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def _accept(prefix: bytes) -> bool:
294294
def _limit_rational(
295295
val: float | Fraction | IFDRational, max_val: int
296296
) -> tuple[IntegralLike, IntegralLike]:
297-
inv = abs(float(val)) > 1
297+
inv = abs(val) > 1
298298
n_d = IFDRational(1 / val if inv else val).limit_rational(max_val)
299299
return n_d[::-1] if inv else n_d
300300

@@ -685,22 +685,33 @@ def _setitem(self, tag: int, value: Any, legacy_api: bool) -> None:
685685
else:
686686
self.tagtype[tag] = TiffTags.UNDEFINED
687687
if all(isinstance(v, IFDRational) for v in values):
688-
self.tagtype[tag] = (
689-
TiffTags.RATIONAL
690-
if all(v >= 0 for v in values)
691-
else TiffTags.SIGNED_RATIONAL
692-
)
688+
for v in values:
689+
assert isinstance(v, IFDRational)
690+
if v < 0:
691+
self.tagtype[tag] = TiffTags.SIGNED_RATIONAL
692+
break
693+
else:
694+
self.tagtype[tag] = TiffTags.RATIONAL
693695
elif all(isinstance(v, int) for v in values):
694-
if all(0 <= v < 2**16 for v in values):
696+
short = True
697+
signed_short = True
698+
long = True
699+
for v in values:
700+
assert isinstance(v, int)
701+
if short and not (0 <= v < 2**16):
702+
short = False
703+
if signed_short and not (-(2**15) < v < 2**15):
704+
signed_short = False
705+
if long and v < 0:
706+
long = False
707+
if short:
695708
self.tagtype[tag] = TiffTags.SHORT
696-
elif all(-(2**15) < v < 2**15 for v in values):
709+
elif signed_short:
697710
self.tagtype[tag] = TiffTags.SIGNED_SHORT
711+
elif long:
712+
self.tagtype[tag] = TiffTags.LONG
698713
else:
699-
self.tagtype[tag] = (
700-
TiffTags.LONG
701-
if all(v >= 0 for v in values)
702-
else TiffTags.SIGNED_LONG
703-
)
714+
self.tagtype[tag] = TiffTags.SIGNED_LONG
704715
elif all(isinstance(v, float) for v in values):
705716
self.tagtype[tag] = TiffTags.DOUBLE
706717
elif all(isinstance(v, str) for v in values):
@@ -718,7 +729,10 @@ def _setitem(self, tag: int, value: Any, legacy_api: bool) -> None:
718729

719730
is_ifd = self.tagtype[tag] == TiffTags.LONG and isinstance(values, dict)
720731
if not is_ifd:
721-
values = tuple(info.cvt_enum(value) for value in values)
732+
values = tuple(
733+
info.cvt_enum(value) if isinstance(value, str) else value
734+
for value in values
735+
)
722736

723737
dest = self._tags_v1 if legacy_api else self._tags_v2
724738

0 commit comments

Comments
 (0)