Skip to content

Commit fa0ae1e

Browse files
authored
Clobber default change - follow up (#1249)
* Let register_implementation pass without clobber if same class * Allow non-clobber register is no change
1 parent b595ff8 commit fa0ae1e

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

fsspec/registry.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,25 @@ def register_implementation(name, cls, clobber=False, errtxt=None):
3434
"""
3535
if isinstance(cls, str):
3636
if name in known_implementations and clobber is False:
37-
raise ValueError(
38-
"Name (%s) already in the known_implementations and clobber "
39-
"is False" % name
40-
)
41-
known_implementations[name] = {
42-
"class": cls,
43-
"err": errtxt or "%s import failed for protocol %s" % (cls, name),
44-
}
37+
if cls != known_implementations[name]["class"]:
38+
raise ValueError(
39+
"Name (%s) already in the known_implementations and clobber "
40+
"is False" % name
41+
)
42+
else:
43+
known_implementations[name] = {
44+
"class": cls,
45+
"err": errtxt or "%s import failed for protocol %s" % (cls, name),
46+
}
4547

4648
else:
4749
if name in registry and clobber is False:
48-
raise ValueError(
49-
"Name (%s) already in the registry and clobber is False" % name
50-
)
51-
_registry[name] = cls
50+
if _registry[name] is not cls:
51+
raise ValueError(
52+
"Name (%s) already in the registry and clobber is False" % name
53+
)
54+
else:
55+
_registry[name] = cls
5256

5357

5458
# protocols mapped to the class which implements them. This dict can

fsspec/tests/test_registry.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import pytest
66

7+
import fsspec
8+
from fsspec.implementations.zip import ZipFileSystem
79
from fsspec.registry import (
810
_registry,
911
filesystem,
@@ -69,12 +71,16 @@ def test_register_fail(clear_registry):
6971
with pytest.raises(ImportError):
7072
get_filesystem_class("test")
7173

74+
# NOOP
75+
register_implementation("test", "doesntexist.AbstractFileSystem", clobber=False)
7276
with pytest.raises(ValueError):
73-
register_implementation("test", "doesntexist.AbstractFileSystem", clobber=False)
77+
register_implementation(
78+
"test", "doesntexist.AbstractFileSystemm", clobber=False
79+
)
7480

7581
# by default we do not allow clobbering
7682
with pytest.raises(ValueError):
77-
register_implementation("test", "doesntexist.AbstractFileSystem")
83+
register_implementation("test", "doesntexist.AbstractFileSystemm")
7884

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

93+
# NOOP
94+
register_implementation("test", AbstractFileSystem)
8795
with pytest.raises(ValueError):
88-
register_implementation("test", AbstractFileSystem)
96+
register_implementation("test", ZipFileSystem)
8997
register_implementation("test", AbstractFileSystem, clobber=True)
98+
assert isinstance(fsspec.filesystem("test"), AbstractFileSystem)
9099

91100

92101
def test_entry_points_registered_on_import(clear_registry, clean_imports):

0 commit comments

Comments
 (0)