File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change 11
11
12
12
13
13
class ProgramMeta (type ):
14
- pass
14
+ """Metaclass ensuring every ``dspy.Module`` instance is properly initialised."""
15
+
16
+ def __call__ (cls , * args , ** kwargs ):
17
+ # Create the instance without invoking ``__init__`` so we can inject
18
+ # the base initialization beforehand.
19
+ obj = cls .__new__ (cls , * args , ** kwargs )
20
+ if isinstance (obj , cls ):
21
+ # ``_base_init`` sets attributes that should exist on all modules
22
+ # even when a subclass forgets to call ``super().__init__``.
23
+ Module ._base_init (obj )
24
+ cls .__init__ (obj , * args , ** kwargs )
25
+
26
+ # Guarantee existence of critical attributes if ``__init__`` didn't
27
+ # create them.
28
+ if not hasattr (obj , "callbacks" ):
29
+ obj .callbacks = []
30
+ if not hasattr (obj , "history" ):
31
+ obj .history = []
32
+ return obj
15
33
16
34
17
35
class Module (BaseModule , metaclass = ProgramMeta ):
18
36
def _base_init (self ):
19
37
self ._compiled = False
38
+ self .callbacks = []
39
+ self .history = []
20
40
21
41
def __init__ (self , callbacks = None ):
22
42
self .callbacks = callbacks or []
You can’t perform that action at this time.
0 commit comments