Skip to content

Clobber default change - follow up #1249

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

Merged
merged 2 commits into from
Apr 20, 2023
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
28 changes: 16 additions & 12 deletions fsspec/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,25 @@ def register_implementation(name, cls, clobber=False, errtxt=None):
"""
if isinstance(cls, str):
if name in known_implementations and clobber is False:
raise ValueError(
"Name (%s) already in the known_implementations and clobber "
"is False" % name
)
known_implementations[name] = {
"class": cls,
"err": errtxt or "%s import failed for protocol %s" % (cls, name),
}
if cls != known_implementations[name]["class"]:
raise ValueError(
"Name (%s) already in the known_implementations and clobber "
"is False" % name
)
else:
known_implementations[name] = {
"class": cls,
"err": errtxt or "%s import failed for protocol %s" % (cls, name),
}

else:
if name in registry and clobber is False:
raise ValueError(
"Name (%s) already in the registry and clobber is False" % name
)
_registry[name] = cls
if _registry[name] is not cls:
raise ValueError(
"Name (%s) already in the registry and clobber is False" % name
)
else:
_registry[name] = cls


# protocols mapped to the class which implements them. This dict can
Expand Down
15 changes: 12 additions & 3 deletions fsspec/tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pytest

import fsspec
from fsspec.implementations.zip import ZipFileSystem
from fsspec.registry import (
_registry,
filesystem,
Expand Down Expand Up @@ -69,12 +71,16 @@ def test_register_fail(clear_registry):
with pytest.raises(ImportError):
get_filesystem_class("test")

# NOOP
register_implementation("test", "doesntexist.AbstractFileSystem", clobber=False)
with pytest.raises(ValueError):
register_implementation("test", "doesntexist.AbstractFileSystem", clobber=False)
register_implementation(
"test", "doesntexist.AbstractFileSystemm", clobber=False
)

# by default we do not allow clobbering
with pytest.raises(ValueError):
register_implementation("test", "doesntexist.AbstractFileSystem")
register_implementation("test", "doesntexist.AbstractFileSystemm")

register_implementation(
"test", "doesntexist.AbstractFileSystem", errtxt="hiho", clobber=True
Expand All @@ -84,9 +90,12 @@ def test_register_fail(clear_registry):
assert "hiho" in str(e.value)
register_implementation("test", AbstractFileSystem)

# NOOP
register_implementation("test", AbstractFileSystem)
with pytest.raises(ValueError):
register_implementation("test", AbstractFileSystem)
register_implementation("test", ZipFileSystem)
register_implementation("test", AbstractFileSystem, clobber=True)
assert isinstance(fsspec.filesystem("test"), AbstractFileSystem)


def test_entry_points_registered_on_import(clear_registry, clean_imports):
Expand Down