Skip to content

Commit fe0f9d9

Browse files
committed
avoid plugins overriding existing methods
1 parent 6e39c65 commit fe0f9d9

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

streamz/core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ def wrapped(*args, **kwargs):
306306

307307
@classmethod
308308
def register_plugin_entry_point(cls, entry_point, modifier=identity):
309+
if hasattr(cls, entry_point.name):
310+
raise ValueError(
311+
f"Can't add {entry_point.name} from {entry_point.module_name} "
312+
f"to {cls.__name__}: duplicate method name."
313+
)
314+
309315
def stub(*args, **kwargs):
310316
""" Entrypoints-based streamz plugin. Will be loaded on first call. """
311317
node = entry_point.load()

streamz/plugins.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
import warnings
2+
13
import pkg_resources
24

35

6+
def try_register(cls, entry_point, *modifier):
7+
try:
8+
cls.register_plugin_entry_point(entry_point, *modifier)
9+
except ValueError:
10+
warnings.warn(
11+
f"Can't add {entry_point.name} from {entry_point.module_name}: "
12+
"name collision with existing stream node."
13+
)
14+
15+
416
def load_plugins(cls):
517
for entry_point in pkg_resources.iter_entry_points("streamz.sources"):
6-
cls.register_plugin_entry_point(entry_point, staticmethod)
18+
try_register(cls, entry_point, staticmethod)
719
for entry_point in pkg_resources.iter_entry_points("streamz.nodes"):
8-
cls.register_plugin_entry_point(entry_point)
20+
try_register(cls, entry_point)
921
for entry_point in pkg_resources.iter_entry_points("streamz.sinks"):
10-
cls.register_plugin_entry_point(entry_point)
22+
try_register(cls, entry_point)

streamz/tests/test_plugins.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class test_source(Source):
4141
assert inspect.isfunction(Stream().from_test)
4242

4343

44-
def test_register_plugin_entry_point_raises():
44+
def test_register_plugin_entry_point_raises_type():
4545
class invalid_node:
4646
pass
4747

@@ -51,3 +51,10 @@ class invalid_node:
5151

5252
with pytest.raises(TypeError):
5353
Stream.test()
54+
55+
56+
def test_register_plugin_entry_point_raises_duplicate_name():
57+
entry_point = MockEntryPoint("map", None)
58+
59+
with pytest.raises(ValueError):
60+
Stream.register_plugin_entry_point(entry_point)

0 commit comments

Comments
 (0)