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

upath.implementations.local: remove dependency on packaging #187

Merged
merged 1 commit into from
Feb 17, 2024
Merged
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
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
Loading