Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e7a1b56

Browse files
committedAug 20, 2024·
Type context manager dunders
1 parent ebddeb3 commit e7a1b56

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
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

+33-8
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
@@ -39,6 +40,8 @@
3940
from .install_scripts import install_scripts as install_scripts_cls
4041

4142
if TYPE_CHECKING:
43+
from typing_extensions import Self
44+
4245
from .._vendor.wheel.wheelfile import WheelFile
4346

4447
_P = TypeVar("_P", bound=StrPath)
@@ -379,9 +382,15 @@ def _select_strategy(
379382
class EditableStrategy(Protocol):
380383
def __call__(self, wheel: WheelFile, files: list[str], mapping: dict[str, str]): ...
381384

382-
def __enter__(self): ...
385+
def __enter__(self) -> Self: ...
383386

384-
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: ...
385394

386395

387396
class _StaticPth:
@@ -395,15 +404,21 @@ def __call__(self, wheel: WheelFile, files: list[str], mapping: dict[str, str]):
395404
contents = _encode_pth(f"{entries}\n")
396405
wheel.writestr(f"__editable__.{self.name}.pth", contents)
397406

398-
def __enter__(self):
407+
def __enter__(self) -> Self:
399408
msg = f"""
400409
Editable install will be performed using .pth file to extend `sys.path` with:
401410
{list(map(os.fspath, self.path_entries))!r}
402411
"""
403412
_logger.warning(msg + _LENIENT_WARNING)
404413
return self
405414

406-
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
407422

408423

409424
class _LinkTree(_StaticPth):
@@ -461,12 +476,17 @@ def _create_links(self, outputs, output_mapping):
461476
for relative, src in mappings.items():
462477
self._create_file(relative, src, link=link_type)
463478

464-
def __enter__(self):
479+
def __enter__(self) -> Self:
465480
msg = "Strict editable install will be performed using a link tree.\n"
466481
_logger.warning(msg + _STRICT_WARNING)
467482
return self
468483

469-
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:
470490
msg = f"""\n
471491
Strict editable installation performed using the auxiliary directory:
472492
{self.auxiliary_dir}
@@ -522,12 +542,17 @@ def __call__(self, wheel: WheelFile, files: list[str], mapping: dict[str, str]):
522542
for file, content in self.get_implementation():
523543
wheel.writestr(file, content)
524544

525-
def __enter__(self):
545+
def __enter__(self) -> Self:
526546
msg = "Editable install will be performed using a meta path finder.\n"
527547
_logger.warning(msg + _LENIENT_WARNING)
528548
return self
529549

530-
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:
531556
msg = """\n
532557
Please be careful with folders in your working directory with the same
533558
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
@@ -430,7 +431,12 @@ def __enter__(self) -> Self:
430431

431432
return super().__enter__()
432433

433-
def __exit__(self, exc_type, exc_value, traceback):
434+
def __exit__(
435+
self,
436+
exc_type: type[BaseException] | None,
437+
exc_value: BaseException | None,
438+
traceback: TracebackType | None,
439+
) -> None:
434440
"""When exiting the context, if values of ``packages``, ``py_modules`` and
435441
``package_dir`` are missing in ``setuptools_cfg``, copy from ``dist``.
436442
"""

‎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)
Please sign in to comment.