Skip to content

Commit 2f15eb1

Browse files
committed
Use typing.Self
1 parent 09c72fa commit 2f15eb1

9 files changed

Lines changed: 25 additions & 24 deletions

File tree

src/PIL/ContainerIO.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
from collections.abc import Iterable
2020
from typing import IO, AnyStr, NoReturn
2121

22+
TYPE_CHECKING = False
23+
if TYPE_CHECKING:
24+
from typing import Self
25+
2226

2327
class ContainerIO(IO[AnyStr]):
2428
"""
@@ -147,13 +151,13 @@ def writelines(self, lines: Iterable[AnyStr]) -> NoReturn:
147151
def truncate(self, size: int | None = None) -> int:
148152
raise NotImplementedError()
149153

150-
def __enter__(self) -> ContainerIO[AnyStr]:
154+
def __enter__(self) -> Self:
151155
return self
152156

153157
def __exit__(self, *args: object) -> None:
154158
self.close()
155159

156-
def __iter__(self) -> ContainerIO[AnyStr]:
160+
def __iter__(self) -> Self:
157161
return self
158162

159163
def __next__(self) -> AnyStr:

src/PIL/GimpPaletteFile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
TYPE_CHECKING = False
2222
if TYPE_CHECKING:
23-
from typing import IO
23+
from typing import IO, Self
2424

2525

2626
class GimpPaletteFile:
@@ -66,7 +66,7 @@ def __init__(self, fp: IO[bytes]) -> None:
6666
self._read(fp)
6767

6868
@classmethod
69-
def frombytes(cls, data: bytes) -> GimpPaletteFile:
69+
def frombytes(cls, data: bytes) -> Self:
7070
self = cls.__new__(cls)
7171
self._read(BytesIO(data), False)
7272
return self

src/PIL/Image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
if TYPE_CHECKING:
6868
from collections.abc import Callable, Iterator, Sequence
6969
from types import ModuleType
70-
from typing import Any, Literal
70+
from typing import Any, Literal, Self
7171

7272
logger = logging.getLogger(__name__)
7373

@@ -691,7 +691,7 @@ def _new(self, im: core.ImagingCore) -> Image:
691691
return new
692692

693693
# Context manager support
694-
def __enter__(self) -> Image:
694+
def __enter__(self) -> Self:
695695
return self
696696

697697
def __exit__(self, *args: object) -> None:

src/PIL/ImageFile.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
TYPE_CHECKING = False
4343
if TYPE_CHECKING:
44+
from typing import Self
45+
4446
from ._typing import StrOrBytesPath
4547

4648
logger = logging.getLogger(__name__)
@@ -173,10 +175,6 @@ def __init__(
173175
def _open(self) -> None:
174176
pass
175177

176-
# Context manager support
177-
def __enter__(self) -> ImageFile:
178-
return self
179-
180178
def _close_fp(self) -> None:
181179
if getattr(self, "_fp", False) and not isinstance(self._fp, DeferredError):
182180
if self._fp != self.fp:
@@ -185,6 +183,7 @@ def _close_fp(self) -> None:
185183
if self.fp:
186184
self.fp.close()
187185

186+
# Context manager support
188187
def __exit__(self, *args: object) -> None:
189188
if getattr(self, "_exclusive_fp", False):
190189
self._close_fp()
@@ -601,7 +600,7 @@ def feed(self, data: bytes) -> None:
601600

602601
self.image = im
603602

604-
def __enter__(self) -> Parser:
603+
def __enter__(self) -> Self:
605604
return self
606605

607606
def __exit__(self, *args: object) -> None:

src/PIL/ImageMath.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
if TYPE_CHECKING:
2525
from collections.abc import Callable
2626
from types import CodeType
27-
from typing import Any
27+
from typing import Any, Self
2828

2929

3030
class _Operand:
@@ -104,7 +104,7 @@ def __bool__(self) -> bool:
104104
def __abs__(self) -> _Operand:
105105
return self.apply("abs", self)
106106

107-
def __pos__(self) -> _Operand:
107+
def __pos__(self) -> Self:
108108
return self
109109

110110
def __neg__(self) -> _Operand:

src/PIL/ImageSequence.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
TYPE_CHECKING = False
2222
if TYPE_CHECKING:
2323
from collections.abc import Callable
24+
from typing import Self
2425

2526

2627
class Iterator:
@@ -50,7 +51,7 @@ def __getitem__(self, ix: int) -> Image.Image:
5051
msg = "end of sequence"
5152
raise IndexError(msg) from e
5253

53-
def __iter__(self) -> Iterator:
54+
def __iter__(self) -> Self:
5455
return self
5556

5657
def __next__(self) -> Image.Image:

src/PIL/PdfParser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
TYPE_CHECKING = False
1616
if TYPE_CHECKING:
17-
from typing import IO
17+
from typing import IO, Self
1818

1919
_DictBase = collections.UserDict[str | bytes, Any]
2020
else:
@@ -427,7 +427,7 @@ def __init__(
427427
if f:
428428
self.seek_end()
429429

430-
def __enter__(self) -> PdfParser:
430+
def __enter__(self) -> Self:
431431
return self
432432

433433
def __exit__(self, *args: object) -> None:

src/PIL/PngImagePlugin.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
TYPE_CHECKING = False
5454
if TYPE_CHECKING:
5555
from collections.abc import Callable
56-
from typing import Any, NoReturn
56+
from typing import Any, NoReturn, Self
5757

5858
from . import _imaging
5959

@@ -184,7 +184,7 @@ def read(self) -> tuple[bytes, int, int]:
184184

185185
return cid, pos, length
186186

187-
def __enter__(self) -> ChunkStream:
187+
def __enter__(self) -> Self:
188188
return self
189189

190190
def __exit__(self, *args: object) -> None:
@@ -265,7 +265,7 @@ class iTXt(str):
265265
@staticmethod
266266
def __new__(
267267
cls, text: str, lang: str | None = None, tkey: str | None = None
268-
) -> iTXt:
268+
) -> Self:
269269
"""
270270
:param cls: the class to use when creating the instance
271271
:param text: value for this key
@@ -399,9 +399,6 @@ def __init__(self, fp: IO[bytes]) -> None:
399399

400400
self.text_memory = 0
401401

402-
def __enter__(self) -> PngStream:
403-
return self
404-
405402
def check_text_memory(self, chunklen: int) -> None:
406403
self.text_memory += chunklen
407404
if self.text_memory > MAX_TEXT_MEMORY:

src/PIL/TiffImagePlugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
TYPE_CHECKING = False
6363
if TYPE_CHECKING:
6464
from collections.abc import Iterator
65-
from typing import NoReturn
65+
from typing import NoReturn, Self
6666

6767
from ._typing import Buffer, IntegralLike, StrOrBytesPath
6868

@@ -2117,7 +2117,7 @@ def newFrame(self) -> None:
21172117
self.finalize()
21182118
self.setup()
21192119

2120-
def __enter__(self) -> AppendingTiffWriter:
2120+
def __enter__(self) -> Self:
21212121
return self
21222122

21232123
def __exit__(self, *args: object) -> None:

0 commit comments

Comments
 (0)