Skip to content

Commit

Permalink
perf: move resolve classes to validate
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Sep 29, 2022
1 parent 589c1c1 commit 2b87079
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions traitlets/traitlets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2134,6 +2134,12 @@ class or its subclasses. Our implementation is quite different
super().__init__(**kwargs)

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

def instance_init(self, obj):
# we can't do this in subclass_init because that
# might be called before all imports are done.
self._resolve_classes()
def subclass_init(self, cls):
pass

def _resolve_classes(self):
if isinstance(self.klass, str):
Expand Down

0 comments on commit 2b87079

Please sign in to comment.