Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type all context manager dunders #4581

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions _distutils_hack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
41 changes: 33 additions & 8 deletions setuptools/command/editable_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -395,15 +404,21 @@ 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}
"""
_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):
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 10 additions & 3 deletions setuptools/config/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion setuptools/config/pyprojecttoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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``.
"""
Expand Down
23 changes: 19 additions & 4 deletions setuptools/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +26,9 @@
_open = open


if TYPE_CHECKING:
from typing_extensions import Self

__all__ = [
"AbstractSandbox",
"DirectorySandbox",
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
Loading