Backward-compatibility shim decorator for observers Use with: @observe('name') @observe_compat def _foo_changed(self, change): ... With this, `super()._foo_changed(self, name, old, new)` in subclasses will still work. Allows adoption of new observer API without bre
(func: FuncT)
| 1127 | |
| 1128 | |
| 1129 | def observe_compat(func: FuncT) -> FuncT: |
| 1130 | """Backward-compatibility shim decorator for observers |
| 1131 | |
| 1132 | Use with: |
| 1133 | |
| 1134 | @observe('name') |
| 1135 | @observe_compat |
| 1136 | def _foo_changed(self, change): |
| 1137 | ... |
| 1138 | |
| 1139 | With this, `super()._foo_changed(self, name, old, new)` in subclasses will still work. |
| 1140 | Allows adoption of new observer API without breaking subclasses that override and super. |
| 1141 | """ |
| 1142 | |
| 1143 | def compatible_observer( |
| 1144 | self: t.Any, change_or_name: str, old: t.Any = Undefined, new: t.Any = Undefined |
| 1145 | ) -> t.Any: |
| 1146 | if isinstance(change_or_name, dict): # type:ignore[unreachable] |
| 1147 | change = Bunch(change_or_name) # type:ignore[unreachable] |
| 1148 | else: |
| 1149 | clsname = self.__class__.__name__ |
| 1150 | warn( |
| 1151 | f"A parent of {clsname}._{change_or_name}_changed has adopted the new (traitlets 4.1) @observe(change) API", |
| 1152 | DeprecationWarning, |
| 1153 | stacklevel=2, |
| 1154 | ) |
| 1155 | change = Bunch( |
| 1156 | type="change", |
| 1157 | old=old, |
| 1158 | new=new, |
| 1159 | name=change_or_name, |
| 1160 | owner=self, |
| 1161 | ) |
| 1162 | return func(self, change) |
| 1163 | |
| 1164 | return compatible_observer # type:ignore[return-value] |
| 1165 | |
| 1166 | |
| 1167 | def validate(*names: Sentinel | str) -> ValidateHandler: |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…