Skip to content

Commit

Permalink
upath.implementations.local: remove dependency on packaging (#187)
Browse files Browse the repository at this point in the history
Close #186
  • Loading branch information
ap-- authored Feb 17, 2024
1 parent 569ceab commit 7528316
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions upath/implementations/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
from typing import MutableMapping
from urllib.parse import SplitResult

from fsspec import __version__ as fsspec_version
from packaging.version import Version

from upath._flavour import FSSpecFlavour as _FSSpecFlavour
from upath.core import UPath

Expand All @@ -24,7 +21,21 @@
"WindowsUPath",
]

_LISTDIR_WORKS_ON_FILES = Version(fsspec_version) >= Version("2024.2.0")
_LISTDIR_WORKS_ON_FILES: bool | None = None


def _check_listdir_works_on_files() -> bool:
global _LISTDIR_WORKS_ON_FILES
from fsspec.implementations.local import LocalFileSystem

fs = LocalFileSystem()
try:
fs.ls(__file__)
except NotADirectoryError:
_LISTDIR_WORKS_ON_FILES = w = False
else:
_LISTDIR_WORKS_ON_FILES = w = True
return w


class LocalPath(UPath):
Expand All @@ -49,6 +60,8 @@ class FilePath(LocalPath):
__slots__ = ()

def iterdir(self):
if _LISTDIR_WORKS_ON_FILES is None:
_check_listdir_works_on_files()
if _LISTDIR_WORKS_ON_FILES and self.is_file():
raise NotADirectoryError(f"{self}")
return super().iterdir()
Expand Down

0 comments on commit 7528316

Please sign in to comment.