Skip to content

Commit a13731d

Browse files
committed
feat: allow setting instantiate to regsiter a class as its instance
1 parent 90a20b9 commit a13731d

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

simplug.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ def __init__(self, plugin: Any, batch_index: int, index: int) -> None:
274274
(batch_index, index) if priority is None else (priority, batch_index)
275275
)
276276

277+
instantiate = getattr(self.plugin, "instantiate", False)
278+
if instantiate and inspect.isclass(self.plugin):
279+
self.plugin = self.plugin()
280+
277281
self.enabled = True # type: bool
278282

279283
@property

tests/test_simplug.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,8 @@ def hook2(arg):
20012001
...
20022002

20032003
class PluginBase:
2004+
instantiate = True
2005+
20042006
@simplug.impl
20052007
def hook1(self, arg):
20062008
return arg + 1
@@ -2014,12 +2016,12 @@ def hook2(arg):
20142016
assert pb.hook2(1) == 3
20152017
assert PluginBase.hook2(1) == 3
20162018

2017-
with pytest.warns(ImplMightNeedInstanceWarning):
2018-
simplug.register(PluginBase)
2019+
simplug.register(PluginBase)
20192020
assert simplug.hooks.hook1(1) == [2]
20202021
assert simplug.hooks.hook2(1) == [3]
20212022

20222023
class PluginChild(PluginBase):
2024+
instantiate = False
20232025

20242026
def __init__(self, name="pluginchild"):
20252027
self.name = name

0 commit comments

Comments
 (0)