Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jaraco/jaraco.context
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v6.0.0
Choose a base ref
...
head repository: jaraco/jaraco.context
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v6.0.1
Choose a head ref
  • 3 commits
  • 2 files changed
  • 1 contributor

Commits on Aug 20, 2024

  1. Revert "Apply Gemini's ideas for improving the types."

    This reverts commit e051242.
    
    Closes #13
    jaraco committed Aug 20, 2024
    Copy the full SHA
    591cb84 View commit details
  2. Add news fragment.

    jaraco committed Aug 20, 2024
    Copy the full SHA
    97cd186 View commit details
  3. Finalize

    jaraco committed Aug 20, 2024
    Copy the full SHA
    088151e View commit details
Showing with 21 additions and 29 deletions.
  1. +9 −0 NEWS.rst
  2. +12 −29 jaraco/context/__init__.py
9 changes: 9 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
v6.0.1
======

Bugfixes
--------

- Removed type declarations as suggested by Gemini. (#13)


v6.0.0
======

41 changes: 12 additions & 29 deletions jaraco/context/__init__.py
Original file line number Diff line number Diff line change
@@ -11,22 +11,18 @@
import subprocess
import sys
import tempfile
import types
import urllib.request
from typing import Iterator, TypeVar, Union, Optional, Callable, Type, Tuple
from typing import Iterator


if sys.version_info < (3, 12):
from backports import tarfile
else:
import tarfile

PathLike = Union[str, os.PathLike]
T = TypeVar('T')


@contextlib.contextmanager
def pushd(dir: PathLike) -> Iterator[PathLike]:
def pushd(dir: str | os.PathLike) -> Iterator[str | os.PathLike]:
"""
>>> tmp_path = getfixture('tmp_path')
>>> with pushd(tmp_path):
@@ -44,8 +40,8 @@ def pushd(dir: PathLike) -> Iterator[PathLike]:

@contextlib.contextmanager
def tarball(
url: str, target_dir: str | os.PathLike | None = None
) -> Iterator[PathLike]:
url, target_dir: str | os.PathLike | None = None
) -> Iterator[str | os.PathLike]:
"""
Get a URL to a tarball, download, extract, yield, then clean up.
@@ -96,11 +92,7 @@ def strip_first_component(
return member


CM = TypeVar('CM', bound=contextlib.AbstractContextManager)
"""Type var for context managers."""


def _compose(*cmgrs: Callable[..., CM]) -> Callable[..., CM]:
def _compose(*cmgrs):
"""
Compose any number of dependent context managers into a single one.
@@ -159,7 +151,7 @@ def robust_remover():


@contextlib.contextmanager
def temp_dir(remover: Callable[[str], None] = shutil.rmtree) -> Iterator[str]:
def temp_dir(remover=shutil.rmtree):
"""
Create a temporary directory context. Pass a custom remover
to override the removal behavior.
@@ -181,10 +173,7 @@ def temp_dir(remover: Callable[[str], None] = shutil.rmtree) -> Iterator[str]:

@contextlib.contextmanager
def repo_context(
url,
branch: str | None = None,
quiet: bool = True,
dest_ctx: Callable[[], contextlib.AbstractContextManager[str]] = robust_temp_dir,
url, branch: str | None = None, quiet: bool = True, dest_ctx=robust_temp_dir
):
"""
Check out the repo indicated by url.
@@ -207,7 +196,7 @@ def repo_context(
yield repo_dir


class ExceptionTrap(contextlib.AbstractContextManager):
class ExceptionTrap:
"""
A context manager that will catch certain exceptions and provide an
indication they occurred.
@@ -241,13 +230,9 @@ class ExceptionTrap(contextlib.AbstractContextManager):
False
"""

exc_info: Tuple[
Optional[Type[BaseException]],
Optional[BaseException],
Optional[types.TracebackType],
] = (None, None, None) # Explicitly type the tuple
exc_info = None, None, None

def __init__(self, exceptions: Tuple[Type[BaseException], ...] = (Exception,)):
def __init__(self, exceptions=(Exception,)):
self.exceptions = exceptions

def __enter__(self):
@@ -275,9 +260,7 @@ def __exit__(self, *exc_info):
def __bool__(self):
return bool(self.type)

def raises(
self, func: Callable[..., T], *, _test: Callable[[ExceptionTrap], bool] = bool
):
def raises(self, func, *, _test=bool):
"""
Wrap func and replace the result with the truth
value of the trap (True if an exception occurred).
@@ -304,7 +287,7 @@ def wrapper(*args, **kwargs):

return wrapper

def passes(self, func: Callable[..., T]) -> Callable[..., bool]:
def passes(self, func):
"""
Wrap func and replace the result with the truth
value of the trap (True if no exception).