Skip to content

Commit

Permalink
chore: optional anyio
Browse files Browse the repository at this point in the history
  • Loading branch information
phi-friday committed Oct 27, 2024
1 parent ba54417 commit a0256c1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ classifiers = [
]
requires-python = ">=3.9"
dependencies = [
"anyio>=4",
"cloudpickle>=3.1.0",
"pydantic>=2",
"sqlalchemy-utils",
Expand All @@ -33,6 +32,7 @@ dependencies = [
]
[project.optional-dependencies]
asyncio = [
"anyio>=4",
"aiosqlite",
"sqlalchemy[asyncio]>=2",
]
Expand All @@ -44,14 +44,14 @@ test = [
"pytest-env>=1.1.5",
"pytest-xdist>=3.6.1",
"uvloop>=0.21.0",
"typed-diskcache[asyncio]"
]
dev = [
{ include-group = "test"},
"ruff==0.7.1",
"ipykernel>=6.29.5",
"pre-commit>=4.0.1",
"poethepoet>=0.29.0",
"typed-diskcache[asyncio]"
]
docs = [
"linkify-it-py>=2.0.3; python_version < '3.10'",
Expand Down
18 changes: 14 additions & 4 deletions src/typed_diskcache/implement/cache/default/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal

import anyio
import sqlalchemy as sa
from sqlalchemy.exc import OperationalError
from typing_extensions import TypeAlias, TypeVar, Unpack
Expand Down Expand Up @@ -45,6 +44,7 @@
)
from os import PathLike

from anyio import Path as AnyioPath
from anyio.streams.memory import MemoryObjectSendStream
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session
Expand Down Expand Up @@ -262,6 +262,8 @@ async def aiter_disk(
def extend_queue(
stream: MemoryObjectSendStream[_T],
) -> Callable[[Iterable[_T]], Awaitable[Any]]:
import anyio

async def extend(items: Iterable[_T]) -> None:
logger.debug("Stream stats: %r", stream.statistics())
with stream.clone() as clone:
Expand Down Expand Up @@ -469,6 +471,8 @@ async def async_transact(
filename: str | PathLike[str] | None = None,
stacklevel: int = 3,
) -> AsyncGenerator[tuple[AsyncSession, AsyncCleanupFunc], None]:
import anyio

send, receive = anyio.create_memory_object_stream["str | PathLike[str] | None"](
1_000_000
)
Expand Down Expand Up @@ -1027,7 +1031,7 @@ async def acheck_files(
fix: bool,
stacklevel: int = 2,
) -> None:
filenames: set[anyio.Path] = set()
filenames: set[AnyioPath] = set()
rows_fetch = await session.execute(
sa.select(
CacheTable.id,
Expand Down Expand Up @@ -1070,9 +1074,11 @@ async def acheck_file_exists( # noqa: PLR0913
row: sa.Row[tuple[int, int, str]],
directory: str | PathLike[str],
fix: bool,
filenames: set[anyio.Path],
filenames: set[AnyioPath],
stacklevel: int = 3,
) -> None:
import anyio

full_path: anyio.Path = anyio.Path(directory) / row.filepath
filenames.add(full_path)

Expand Down Expand Up @@ -1104,9 +1110,11 @@ async def acheck_unknown_file(
dirpath: str | PathLike[str],
fix: bool,
files: list[str],
filenames: set[anyio.Path],
filenames: set[AnyioPath],
stacklevel: int = 3,
) -> None:
import anyio

paths = {anyio.Path(dirpath) / file for file in files}
error = paths - filenames

Expand All @@ -1131,6 +1139,8 @@ async def acheck_empty_dir(
fix: bool,
stacklevel: int = 3,
) -> None:
import anyio

if not (dirs or files):
message = f"Empty directory: {dirpath}"
warnings.warn(message, te.TypedDiskcacheEmptyDirWarning, stacklevel=stacklevel)
Expand Down
5 changes: 4 additions & 1 deletion src/typed_diskcache/implement/disk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any

import anyio
import cloudpickle
from typing_extensions import override

Expand Down Expand Up @@ -272,6 +271,8 @@ def fetch( # noqa: PLR0911
async def afetch( # noqa: PLR0911
self, *, mode: CacheMode, filename: str | PathLike[str] | None, value: Any
) -> Any:
import anyio

if mode == CacheMode.NONE:
logger.debug("Fetching null value")
return None
Expand Down Expand Up @@ -330,6 +331,8 @@ def remove(self, file_path: str | PathLike[str]) -> None:
@context
@override
async def aremove(self, file_path: str | PathLike[str]) -> None:
import anyio

full_path = anyio.Path(self._directory / file_path)
full_dir = full_path.parent

Expand Down
11 changes: 7 additions & 4 deletions src/typed_diskcache/implement/disk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from contextlib import suppress
from typing import TYPE_CHECKING, Any, Literal, overload

import anyio
from typing_extensions import TypeAlias

if TYPE_CHECKING:
from pathlib import Path

from anyio import Path as AnyioPath

from typed_diskcache.interface.disk import DiskProtocol


Expand Down Expand Up @@ -69,24 +70,26 @@ def write(

@overload
async def async_write(
full_path: Path | anyio.Path,
full_path: Path | AnyioPath,
iterator: Iterator[str] | AsyncIterator[str],
mode: OpenTextModeWriting,
encoding: str | None = None,
) -> int | None: ...
@overload
async def async_write(
full_path: Path | anyio.Path,
full_path: Path | AnyioPath,
iterator: Iterator[bytes] | AsyncIterator[bytes],
mode: OpenBinaryModeWriting,
encoding: str | None = None,
) -> int | None: ...
async def async_write(
full_path: Path | anyio.Path,
full_path: Path | AnyioPath,
iterator: Iterator[str | bytes] | AsyncIterator[str | bytes],
mode: OpenTextModeWriting | OpenBinaryModeWriting,
encoding: str | None = None,
) -> int | None:
import anyio

full_path = anyio.Path(full_path)
full_dir = full_path.parent

Expand Down
5 changes: 4 additions & 1 deletion src/typed_diskcache/implement/sync/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from contextlib import AsyncExitStack, ExitStack
from typing import TYPE_CHECKING, Any

import anyio
from pydantic import TypeAdapter
from typing_extensions import override

Expand Down Expand Up @@ -225,6 +224,8 @@ def locked(self) -> bool:
@context("AsyncLock.acquire")
@override
async def acquire(self) -> None:
import anyio

try:
with anyio.fail_after(self.timeout):
while True:
Expand Down Expand Up @@ -264,6 +265,8 @@ class AsyncRLock(AsyncLock):
@context("AsyncRLock.acquire")
@override
async def acquire(self) -> None:
import anyio

pid = os.getpid()
tid = threading.get_ident()
pid_tid = f"{pid}-{tid}"
Expand Down
3 changes: 2 additions & 1 deletion src/typed_diskcache/implement/sync/semaphore.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from contextlib import AsyncExitStack, ExitStack
from typing import TYPE_CHECKING, Any

import anyio
from pydantic import TypeAdapter
from typing_extensions import override

Expand Down Expand Up @@ -187,6 +186,8 @@ def tags(self) -> frozenset[str]:
@context("AsyncSemaphore.acquire")
@override
async def acquire(self) -> None:
import anyio

try:
async with AsyncExitStack() as stack:
stack.enter_context(anyio.fail_after(self.timeout))
Expand Down
3 changes: 2 additions & 1 deletion src/typed_diskcache/utils/memo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from itertools import chain
from typing import TYPE_CHECKING, Any, Generic, Protocol, overload

import anyio
from pydantic import TypeAdapter
from typing_extensions import ParamSpec, TypeVar, override

Expand Down Expand Up @@ -588,6 +587,8 @@ def async_thread_recompute(
*args: _P.args,
**kwargs: _P.kwargs,
) -> None:
import anyio

new_func = partial(
async_thread_recompute_process, cache, key, func, expire, tags, *args, **kwargs
)
Expand Down

0 comments on commit a0256c1

Please sign in to comment.