aiopath icon indicating copy to clipboard operation
aiopath copied to clipboard

rglob is broken on python 3.12

Open valerino opened this issue 2 years ago • 0 comments

on python 3.12 (at least, have not tested 3.11), using rglob() leads to this:

/usr/lib/python3.12/pathlib.py:169: RuntimeWarning: coroutine 'AsyncPath.is_dir' was never awaited
  if not parent_path.is_dir():
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

sample code:

async def _list_directory_async_recursive(path: str, mask: str = None, files_only: bool = False) -> list[str]:
    paths = []
    if mask is None:
        mask = '*'

    p: AsyncPath = AsyncPath(path)
    async for pp in p.rglob(mask):
        if files_only and await pp.is_dir():
            # skip directories
            continue
        paths.append(str(pp))

    return paths

glob() seems unaffected.

a simple fix is, in aiopath.path.py, to change this:

async def rglob(self: Self, pattern: str, *, case_sensitive: bool | None = None) -> AsyncIterable[Self]:
    for path in await to_thread(super().rglob, pattern, case_sensitive=case_sensitive):
      yield AsyncPath(path)

to this

async def rglob(self: Self, pattern: str, *, case_sensitive: bool | None = None) -> AsyncIterable[Self]:
    for path in await to_thread(self._path.rglob, pattern, case_sensitive=case_sensitive):
      yield AsyncPath(path)

if you agree, i could make a PR. i also see the same pattern with super() is used in other functions in aiopath.path.py, though i have not tested if they have the same issue.

valerino avatar Jan 04 '24 14:01 valerino