Skip to content

[Feature Request] fsspec/universal_pathlib integration #60

Open

Description

@Mithrandir2k18

As far as I understand it, this package cannot be used with universal pathlib right away, but all the building block are already there. All that's missing is registering the AbstractFileSystem subclasses with fsspec and creating fsspec/universal_pathlib subclasses and registering them with their registry.

This seems simple enough and seems to work in my testing:

from fsspec import AbstractFileSystem
from upath import UPath
from upath.registry import register_implementation

from morefs.memory import MemFS

# local instance for inspectability during testing
mem_fs = MemFS()


class MorefsMemPath(UPath):
    @classmethod
    def _fs_factory(
        cls, urlpath: str, protocol: str, storage_options: dict
    ) -> AbstractFileSystem:
        return mem_fs  # this should be MemFS(...)


fsname = "customfs"
register_implementation(fsname, MorefsMemPath)


def test_upath():
    testtext = "Hello from MemFS!"
    testfile = "/example.txt"
    p = UPath(f"{fsname}:///{testfile}")
    p.write_text(testtext)
    assert testtext == p.read_text()
    assert mem_fs.ls("/") == [testfile]

Activity

  1. skshetry commented on Mar 11, 2025

    @skshetry
    Collaborator

    I think universal-pathlib is supposed to work right out of the box without requiring explicit registration (which I am not against, PR welcome).

    So, UPath("memfs://") should just work. Although, compared to fsspec's MemoryFileSystem, morefs's memfs and dictfs are not global. So I see that every child paths get created with different filesystem instances.

  2. Mithrandir2k18 commented on Mar 12, 2025

    @Mithrandir2k18
    Author

    Thanks for the speedy reply. Yeah I am new to fsspec and totally missed that there was a mechanism for this upstream already.
    For my usecase, memfs filesystems not being global works out well anyway.

    I wouldn't mind creating a PR, but since it's already implemented as a flavour upstream, what benefit would explicitly registering bring? And registering would require adding a (optional) dependency to the universal_pathlib repo, for as I see it, hardly any benefit/noticeable change? Maybe it could introduce a global variant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions