Skip to content

Commit 5ff2027

Browse files
committed
Updated type hints
1 parent c252b70 commit 5ff2027

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/PIL/TiffImagePlugin.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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)