Skip to content

Commit 2b87079

Browse files
perf: move resolve classes to validate
1 parent 589c1c1 commit 2b87079

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

traitlets/traitlets.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,12 @@ class or its subclasses. Our implementation is quite different
21342134
super().__init__(**kwargs)
21352135

21362136
def validate(self, obj, value):
2137+
# We can't do resolve the classes in subclass_init because that
2138+
# might be called before all imports are done.
2139+
# but instance_init will give us runtime overhead for every trait
2140+
# (even those that are not set / None).
2141+
# This place is a good compromise.
2142+
self._resolve_classes()
21372143
assert self.klass is not None
21382144
if isinstance(value, self.klass): # type:ignore[arg-type]
21392145
return value
@@ -2149,10 +2155,8 @@ def info(self):
21492155
result += " or None"
21502156
return result
21512157

2152-
def instance_init(self, obj):
2153-
# we can't do this in subclass_init because that
2154-
# might be called before all imports are done.
2155-
self._resolve_classes()
2158+
def subclass_init(self, cls):
2159+
pass
21562160

21572161
def _resolve_classes(self):
21582162
if isinstance(self.klass, str):

0 commit comments

Comments
 (0)