Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 6 additions & 8 deletions fsspec_python/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from importlib.machinery import SOURCE_SUFFIXES, ModuleSpec
from os.path import join
from types import ModuleType
from typing import TYPE_CHECKING, Dict, Union
from typing import TYPE_CHECKING

from fsspec import url_to_fs
from fsspec.implementations.local import AbstractFileSystem
Expand Down Expand Up @@ -55,7 +55,7 @@ def __eq__(self, other: object) -> bool:


# Singleton for use elsewhere
_finders: Dict[str, FSSpecImportFinder] = {}
_finders: dict[str, FSSpecImportFinder] = {}


class FSSpecImportLoader(SourceLoader):
Expand All @@ -64,7 +64,7 @@ def __init__(self, fullname: str, path: str, fs: PythonFileSystem):
self.path = path
self.fs = fs

def get_filename(self, fullname: str) -> str: # noqa: ARG002
def get_filename(self, fullname: str) -> str:
return self.path

def get_data(self, path: str | bytes) -> bytes:
Expand All @@ -76,18 +76,17 @@ def get_data(self, path: str | bytes) -> bytes:
# source = source_bytes.decode("utf-8")


def install_importer(fs: Union[str, AbstractFileSystem], **kwargs: str) -> FSSpecImportFinder:
def install_importer(fs: str | AbstractFileSystem, **kwargs: str) -> FSSpecImportFinder:
"""Install the fsspec importer."""
if isinstance(fs, AbstractFileSystem):
fsspec_str = normalize_fsspec(fs=fs, **kwargs)
elif not isinstance(fs, str):
raise ValueError("fs must be a string or AbstractFileSystem instance")
raise TypeError("fs must be a string or AbstractFileSystem instance")
else:
fsspec_str = fs
assert "fo" not in kwargs, "fo cannot be used with string fs"
fs, kwargs["fo"] = url_to_fs(fsspec_str)

global _finders
if fsspec_str not in _finders:
python_fs = fs if isinstance(fs, PythonFileSystem) else PythonFileSystem(fs=fs, install=False, **kwargs)

Expand All @@ -97,9 +96,8 @@ def install_importer(fs: Union[str, AbstractFileSystem], **kwargs: str) -> FSSpe
return _finders[fsspec_str].fs


def uninstall_importer(fs: Union[str, AbstractFileSystem] = "") -> None:
def uninstall_importer(fs: str | AbstractFileSystem = "") -> None:
"""Uninstall the fsspec importer."""
global _finders
if not _finders:
return

Expand Down
2 changes: 1 addition & 1 deletion fsspec_python/tests/test_all.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fsspec_python import * # noqa
from fsspec_python import *


def test_all():
Expand Down
4 changes: 2 additions & 2 deletions fsspec_python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

def normalize_fsspec(
fs: AbstractFileSystem = None,
target_protocol: str = None,
target_options: dict = None,
target_protocol: str | None = None,
target_options: dict | None = None,
fo: str = "",
**kwargs,
):
Expand Down