Skip to content

Commit f07afea

Browse files
maartenbreddelspre-commit-ci[bot]naterush
authored
Performance: decrease runtime overhead for constructing HasTraits (up to 20x faster) (#777)
* perf: find default generators at class contruction time instead of at runtime, which gives a runtime overhead. * perf: find descriptors at class construction time * perf: only add notifiers when they exist * perf: avoid cross_validation_lock context manager this has a runtime overhead (about 20% faster without it) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * perf: get app traits during class construction instead of runtime * typing * perf: avoid dynamic default code path for static immutable defaults * typing * perf: avoid unnecessary dict presence checks * more trivial immutable static initial values * perf: better scaling due to skipping instance_init when possible. * perf: specialized version of hold_trait_notifications in ctor * perf: skip setup if no kwargs are given * fix: validate return value should be used * flake8: unused variable * perf: opt out instance_init for bool and enum * perf: all instances that are None are immutable and static * perf: remove usage of named attribute accesses on bunches * trigger ci * trigger ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Nate Rush <naterush1997@gmail.com>
1 parent ee10bcc commit f07afea

2 files changed

Lines changed: 220 additions & 62 deletions

File tree

traitlets/tests/test_traitlets.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ class A(HasTraitsStub):
123123
a.tt = 10 # type:ignore
124124
self.assertEqual(a.tt, -1)
125125

126+
a = A(tt=11)
127+
self.assertEqual(a.tt, -1)
128+
126129
def test_default_validate(self):
127130
class MyIntTT(TraitType):
128131
def validate(self, obj, value):
@@ -2035,6 +2038,23 @@ def check_valid(self, proposal):
20352038
with self.assertRaises(TraitError):
20362039
u.even = 3 # Trait Error
20372040

2041+
def test_validate_used(self):
2042+
"""Verify that the validate value is being used"""
2043+
2044+
class FixedValue(HasTraits):
2045+
value = Int(0)
2046+
2047+
@validate("value")
2048+
def _value_validate(self, proposal):
2049+
return -1
2050+
2051+
u = FixedValue(value=2)
2052+
assert u.value == -1
2053+
2054+
u = FixedValue()
2055+
u.value = 3
2056+
assert u.value == -1
2057+
20382058

20392059
class TestLink(TestCase):
20402060
def test_connect_same(self):

0 commit comments

Comments
 (0)