Skip to content

Commit c11cc27

Browse files
authored
Type context manager dunders (#4581)
1 parent 2c47de2 commit c11cc27

File tree

5 files changed

+68
-17
lines changed

5 files changed

+68
-17
lines changed

_distutils_hack/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ def add_shim():
217217

218218

219219
class shim:
220-
def __enter__(self):
220+
def __enter__(self) -> None:
221221
insert_shim()
222222

223-
def __exit__(self, exc, value, tb):
223+
def __exit__(self, exc: object, value: object, tb: object) -> None:
224224
_remove_shim()
225225

226226

setuptools/command/editable_wheel.py

+30-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from itertools import chain, starmap
2424
from pathlib import Path
2525
from tempfile import TemporaryDirectory
26+
from types import TracebackType
2627
from typing import TYPE_CHECKING, Iterable, Iterator, Mapping, Protocol, TypeVar, cast
2728

2829
from .. import Command, _normalization, _path, errors, namespaces
@@ -383,7 +384,13 @@ def __call__(self, wheel: WheelFile, files: list[str], mapping: dict[str, str]):
383384

384385
def __enter__(self) -> Self: ...
385386

386-
def __exit__(self, _exc_type, _exc_value, _traceback): ...
387+
def __exit__(
388+
self,
389+
exc_type: type[BaseException] | None,
390+
exc_value: BaseException | None,
391+
traceback: TracebackType | None,
392+
/,
393+
) -> object: ...
387394

388395

389396
class _StaticPth:
@@ -397,15 +404,21 @@ def __call__(self, wheel: WheelFile, files: list[str], mapping: dict[str, str]):
397404
contents = _encode_pth(f"{entries}\n")
398405
wheel.writestr(f"__editable__.{self.name}.pth", contents)
399406

400-
def __enter__(self):
407+
def __enter__(self) -> Self:
401408
msg = f"""
402409
Editable install will be performed using .pth file to extend `sys.path` with:
403410
{list(map(os.fspath, self.path_entries))!r}
404411
"""
405412
_logger.warning(msg + _LENIENT_WARNING)
406413
return self
407414

408-
def __exit__(self, _exc_type, _exc_value, _traceback): ...
415+
def __exit__(
416+
self,
417+
_exc_type: object,
418+
_exc_value: object,
419+
_traceback: object,
420+
) -> None:
421+
pass
409422

410423

411424
class _LinkTree(_StaticPth):
@@ -463,12 +476,17 @@ def _create_links(self, outputs, output_mapping):
463476
for relative, src in mappings.items():
464477
self._create_file(relative, src, link=link_type)
465478

466-
def __enter__(self):
479+
def __enter__(self) -> Self:
467480
msg = "Strict editable install will be performed using a link tree.\n"
468481
_logger.warning(msg + _STRICT_WARNING)
469482
return self
470483

471-
def __exit__(self, _exc_type, _exc_value, _traceback):
484+
def __exit__(
485+
self,
486+
_exc_type: object,
487+
_exc_value: object,
488+
_traceback: object,
489+
) -> None:
472490
msg = f"""\n
473491
Strict editable installation performed using the auxiliary directory:
474492
{self.auxiliary_dir}
@@ -524,12 +542,17 @@ def __call__(self, wheel: WheelFile, files: list[str], mapping: dict[str, str]):
524542
for file, content in self.get_implementation():
525543
wheel.writestr(file, content)
526544

527-
def __enter__(self):
545+
def __enter__(self) -> Self:
528546
msg = "Editable install will be performed using a meta path finder.\n"
529547
_logger.warning(msg + _LENIENT_WARNING)
530548
return self
531549

532-
def __exit__(self, _exc_type, _exc_value, _traceback):
550+
def __exit__(
551+
self,
552+
_exc_type: object,
553+
_exc_value: object,
554+
_traceback: object,
555+
) -> None:
533556
msg = """\n
534557
Please be careful with folders in your working directory with the same
535558
name as your package as they may take precedence during imports.

setuptools/config/expand.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from importlib.machinery import ModuleSpec, all_suffixes
3131
from itertools import chain
3232
from pathlib import Path
33-
from types import ModuleType
33+
from types import ModuleType, TracebackType
3434
from typing import TYPE_CHECKING, Any, Callable, Iterable, Iterator, Mapping, TypeVar
3535

3636
from .._path import StrPath, same_path as _same_path
@@ -40,6 +40,8 @@
4040
from distutils.errors import DistutilsOptionError
4141

4242
if TYPE_CHECKING:
43+
from typing_extensions import Self
44+
4345
from setuptools.dist import Distribution
4446

4547
_K = TypeVar("_K")
@@ -385,10 +387,15 @@ def __call__(self):
385387
self._called = True
386388
self._dist.set_defaults(name=False) # Skip name, we can still be parsing
387389

388-
def __enter__(self):
390+
def __enter__(self) -> Self:
389391
return self
390392

391-
def __exit__(self, _exc_type, _exc_value, _traceback):
393+
def __exit__(
394+
self,
395+
exc_type: type[BaseException] | None,
396+
exc_value: BaseException | None,
397+
traceback: TracebackType | None,
398+
) -> None:
392399
if self._called:
393400
self._dist.set_defaults.analyse_name() # Now we can set a default name
394401

setuptools/config/pyprojecttoml.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
from contextlib import contextmanager
1717
from functools import partial
18+
from types import TracebackType
1819
from typing import TYPE_CHECKING, Any, Callable, Mapping
1920

2021
from .._path import StrPath
@@ -434,7 +435,12 @@ def __enter__(self) -> Self:
434435

435436
return super().__enter__()
436437

437-
def __exit__(self, exc_type, exc_value, traceback):
438+
def __exit__(
439+
self,
440+
exc_type: type[BaseException] | None,
441+
exc_value: BaseException | None,
442+
traceback: TracebackType | None,
443+
) -> None:
438444
"""When exiting the context, if values of ``packages``, ``py_modules`` and
439445
``package_dir`` are missing in ``setuptools_cfg``, copy from ``dist``.
440446
"""

setuptools/sandbox.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import sys
1212
import tempfile
1313
import textwrap
14+
from types import TracebackType
15+
from typing import TYPE_CHECKING
1416

1517
import pkg_resources
1618
from pkg_resources import working_set
@@ -24,6 +26,9 @@
2426
_open = open
2527

2628

29+
if TYPE_CHECKING:
30+
from typing_extensions import Self
31+
2732
__all__ = [
2833
"AbstractSandbox",
2934
"DirectorySandbox",
@@ -118,10 +123,15 @@ class ExceptionSaver:
118123
later.
119124
"""
120125

121-
def __enter__(self):
126+
def __enter__(self) -> Self:
122127
return self
123128

124-
def __exit__(self, type, exc, tb):
129+
def __exit__(
130+
self,
131+
type: type[BaseException] | None,
132+
exc: BaseException | None,
133+
tb: TracebackType | None,
134+
) -> bool:
125135
if not exc:
126136
return False
127137

@@ -278,12 +288,17 @@ def _copy(self, source):
278288
for name in self._attrs:
279289
setattr(os, name, getattr(source, name))
280290

281-
def __enter__(self):
291+
def __enter__(self) -> None:
282292
self._copy(self)
283293
builtins.open = self._open
284294
self._active = True
285295

286-
def __exit__(self, exc_type, exc_value, traceback):
296+
def __exit__(
297+
self,
298+
exc_type: object,
299+
exc_value: object,
300+
traceback: object,
301+
) -> None:
287302
self._active = False
288303
builtins.open = _open
289304
self._copy(_os)

0 commit comments

Comments
 (0)