MCPcopy Create free account
hub / github.com/ipython/traitlets / This

Class This

traitlets/traitlets.py:2378–2399  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

2376
2377
2378class 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
2402class Union(TraitType[t.Any, t.Any]):

Callers 2

BarClass · 0.90
TreeClass · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…