diff --git a/_distutils_hack/__init__.py b/_distutils_hack/__init__.py index b05d04e98e..30ac3a7403 100644 --- a/_distutils_hack/__init__.py +++ b/_distutils_hack/__init__.py @@ -217,10 +217,10 @@ def add_shim(): class shim: - def __enter__(self): + def __enter__(self) -> None: insert_shim() - def __exit__(self, exc, value, tb): + def __exit__(self, exc: object, value: object, tb: object) -> None: _remove_shim() diff --git a/setuptools/command/editable_wheel.py b/setuptools/command/editable_wheel.py index 46852c1a94..4a8b3abb43 100644 --- a/setuptools/command/editable_wheel.py +++ b/setuptools/command/editable_wheel.py @@ -23,6 +23,7 @@ from itertools import chain, starmap from pathlib import Path from tempfile import TemporaryDirectory +from types import TracebackType from typing import TYPE_CHECKING, Iterable, Iterator, Mapping, Protocol, TypeVar, cast from .. import Command, _normalization, _path, errors, namespaces @@ -39,6 +40,8 @@ from .install_scripts import install_scripts as install_scripts_cls if TYPE_CHECKING: + from typing_extensions import Self + from .._vendor.wheel.wheelfile import WheelFile _P = TypeVar("_P", bound=StrPath) @@ -379,9 +382,15 @@ def _select_strategy( class EditableStrategy(Protocol): def __call__(self, wheel: WheelFile, files: list[str], mapping: dict[str, str]): ... - def __enter__(self): ... + def __enter__(self) -> Self: ... - def __exit__(self, _exc_type, _exc_value, _traceback): ... + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + /, + ) -> object: ... class _StaticPth: @@ -395,7 +404,7 @@ def __call__(self, wheel: WheelFile, files: list[str], mapping: dict[str, str]): contents = _encode_pth(f"{entries}\n") wheel.writestr(f"__editable__.{self.name}.pth", contents) - def __enter__(self): + def __enter__(self) -> Self: msg = f""" Editable install will be performed using .pth file to extend `sys.path` with: {list(map(os.fspath, self.path_entries))!r} @@ -403,7 +412,13 @@ def __enter__(self): _logger.warning(msg + _LENIENT_WARNING) return self - def __exit__(self, _exc_type, _exc_value, _traceback): ... + def __exit__( + self, + _exc_type: object, + _exc_value: object, + _traceback: object, + ) -> None: + pass class _LinkTree(_StaticPth): @@ -461,12 +476,17 @@ def _create_links(self, outputs, output_mapping): for relative, src in mappings.items(): self._create_file(relative, src, link=link_type) - def __enter__(self): + def __enter__(self) -> Self: msg = "Strict editable install will be performed using a link tree.\n" _logger.warning(msg + _STRICT_WARNING) return self - def __exit__(self, _exc_type, _exc_value, _traceback): + def __exit__( + self, + _exc_type: object, + _exc_value: object, + _traceback: object, + ) -> None: msg = f"""\n Strict editable installation performed using the auxiliary directory: {self.auxiliary_dir} @@ -522,12 +542,17 @@ def __call__(self, wheel: WheelFile, files: list[str], mapping: dict[str, str]): for file, content in self.get_implementation(): wheel.writestr(file, content) - def __enter__(self): + def __enter__(self) -> Self: msg = "Editable install will be performed using a meta path finder.\n" _logger.warning(msg + _LENIENT_WARNING) return self - def __exit__(self, _exc_type, _exc_value, _traceback): + def __exit__( + self, + _exc_type: object, + _exc_value: object, + _traceback: object, + ) -> None: msg = """\n Please be careful with folders in your working directory with the same name as your package as they may take precedence during imports. diff --git a/setuptools/config/expand.py b/setuptools/config/expand.py index dfee1c5d37..7f7ca9059c 100644 --- a/setuptools/config/expand.py +++ b/setuptools/config/expand.py @@ -30,7 +30,7 @@ from importlib.machinery import ModuleSpec, all_suffixes from itertools import chain from pathlib import Path -from types import ModuleType +from types import ModuleType, TracebackType from typing import TYPE_CHECKING, Any, Callable, Iterable, Iterator, Mapping, TypeVar from .._path import StrPath, same_path as _same_path @@ -40,6 +40,8 @@ from distutils.errors import DistutilsOptionError if TYPE_CHECKING: + from typing_extensions import Self + from setuptools.dist import Distribution _K = TypeVar("_K") @@ -385,10 +387,15 @@ def __call__(self): self._called = True self._dist.set_defaults(name=False) # Skip name, we can still be parsing - def __enter__(self): + def __enter__(self) -> Self: return self - def __exit__(self, _exc_type, _exc_value, _traceback): + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: if self._called: self._dist.set_defaults.analyse_name() # Now we can set a default name diff --git a/setuptools/config/pyprojecttoml.py b/setuptools/config/pyprojecttoml.py index 943b9f5a00..d11064d087 100644 --- a/setuptools/config/pyprojecttoml.py +++ b/setuptools/config/pyprojecttoml.py @@ -15,6 +15,7 @@ import os from contextlib import contextmanager from functools import partial +from types import TracebackType from typing import TYPE_CHECKING, Any, Callable, Mapping from .._path import StrPath @@ -430,7 +431,12 @@ def __enter__(self) -> Self: return super().__enter__() - def __exit__(self, exc_type, exc_value, traceback): + def __exit__( + self, + exc_type: type[BaseException] | None, + exc_value: BaseException | None, + traceback: TracebackType | None, + ) -> None: """When exiting the context, if values of ``packages``, ``py_modules`` and ``package_dir`` are missing in ``setuptools_cfg``, copy from ``dist``. """ diff --git a/setuptools/sandbox.py b/setuptools/sandbox.py index 7d545f1004..8ad8c8a863 100644 --- a/setuptools/sandbox.py +++ b/setuptools/sandbox.py @@ -11,6 +11,8 @@ import sys import tempfile import textwrap +from types import TracebackType +from typing import TYPE_CHECKING import pkg_resources from pkg_resources import working_set @@ -24,6 +26,9 @@ _open = open +if TYPE_CHECKING: + from typing_extensions import Self + __all__ = [ "AbstractSandbox", "DirectorySandbox", @@ -118,10 +123,15 @@ class ExceptionSaver: later. """ - def __enter__(self): + def __enter__(self) -> Self: return self - def __exit__(self, type, exc, tb): + def __exit__( + self, + type: type[BaseException] | None, + exc: BaseException | None, + tb: TracebackType | None, + ) -> bool: if not exc: return False @@ -278,12 +288,17 @@ def _copy(self, source): for name in self._attrs: setattr(os, name, getattr(source, name)) - def __enter__(self): + def __enter__(self) -> None: self._copy(self) builtins.open = self._open self._active = True - def __exit__(self, exc_type, exc_value, traceback): + def __exit__( + self, + exc_type: object, + exc_value: object, + traceback: object, + ) -> None: self._active = False builtins.open = _open self._copy(_os)