A trait for instances of the class containing this trait. Because how how and when class bodies are executed, the ``This`` trait can only have a default value of None. This, and because we always validate default values, ``allow_none`` is *always* true.
| 2376 | |
| 2377 | |
| 2378 | class This(ClassBasedTraitType[t.Optional[T], t.Optional[T]]): |
| 2379 | """A trait for instances of the class containing this trait. |
| 2380 | |
| 2381 | Because how how and when class bodies are executed, the ``This`` |
| 2382 | trait can only have a default value of None. This, and because we |
| 2383 | always validate default values, ``allow_none`` is *always* true. |
| 2384 | """ |
| 2385 | |
| 2386 | info_text = "an instance of the same type as the receiver or None" |
| 2387 | |
| 2388 | def __init__(self, **kwargs: t.Any) -> None: |
| 2389 | super().__init__(None, **kwargs) |
| 2390 | |
| 2391 | def validate(self, obj: t.Any, value: t.Any) -> HasTraits | None: |
| 2392 | # What if value is a superclass of obj.__class__? This is |
| 2393 | # complicated if it was the superclass that defined the This |
| 2394 | # trait. |
| 2395 | assert self.this_class is not None |
| 2396 | if isinstance(value, self.this_class) or (value is None): |
| 2397 | return value |
| 2398 | else: |
| 2399 | self.error(obj, value) |
| 2400 | |
| 2401 | |
| 2402 | class Union(TraitType[t.Any, t.Any]): |