| 646 | return value |
| 647 | |
| 648 | def get(self, obj: HasTraits, cls: type[t.Any] | None = None) -> G | None: |
| 649 | assert self.name is not None |
| 650 | try: |
| 651 | value = obj._trait_values[self.name] |
| 652 | except KeyError: |
| 653 | # Check for a dynamic initializer. |
| 654 | default = obj.trait_defaults(self.name) |
| 655 | if default is Undefined: |
| 656 | warn( |
| 657 | "Explicit using of Undefined as the default value " |
| 658 | "is deprecated in traitlets 5.0, and may cause " |
| 659 | "exceptions in the future.", |
| 660 | DeprecationWarning, |
| 661 | stacklevel=2, |
| 662 | ) |
| 663 | # Using a context manager has a large runtime overhead, so we |
| 664 | # write out the obj.cross_validation_lock call here. |
| 665 | _cross_validation_lock = obj._cross_validation_lock |
| 666 | try: |
| 667 | obj._cross_validation_lock = True |
| 668 | value = self._validate(obj, default) |
| 669 | finally: |
| 670 | obj._cross_validation_lock = _cross_validation_lock |
| 671 | obj._trait_values[self.name] = value |
| 672 | obj._notify_observers( |
| 673 | Bunch( |
| 674 | name=self.name, |
| 675 | value=value, |
| 676 | owner=obj, |
| 677 | type="default", |
| 678 | ) |
| 679 | ) |
| 680 | return value # type:ignore[no-any-return] |
| 681 | except Exception as e: |
| 682 | # This should never be reached. |
| 683 | raise TraitError("Unexpected error in TraitType: default value not set properly") from e |
| 684 | else: |
| 685 | return value # type:ignore[no-any-return] |
| 686 | |
| 687 | @t.overload |
| 688 | def __get__(self, obj: None, cls: type[t.Any]) -> Self: |