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

Class TraitType

traitlets/traitlets.py:517–902  ·  view source on GitHub ↗

A base class for all trait types.

Source from the content-addressed store, hash-verified

515# We use a type for the getter (G) and setter (G) because we allow
516# for traits to cast (for instance CInt will use G=int, S=t.Any)
517class TraitType(BaseDescriptor, t.Generic[G, S]):
518 """A base class for all trait types."""
519
520 metadata: dict[str, t.Any] = {}
521 allow_none: bool = False
522 read_only: bool = False
523 info_text: str = "any value"
524 default_value: t.Any = Undefined
525
526 def __init__(
527 self: TraitType[G, S],
528 default_value: t.Any = Undefined,
529 allow_none: bool = False,
530 read_only: bool | None = None,
531 help: str | None = None,
532 config: t.Any = None,
533 **kwargs: t.Any,
534 ) -> None:
535 """Declare a traitlet.
536
537 If *allow_none* is True, None is a valid value in addition to any
538 values that are normally valid. The default is up to the subclass.
539 For most trait types, the default value for ``allow_none`` is False.
540
541 If *read_only* is True, attempts to directly modify a trait attribute raises a TraitError.
542
543 If *help* is a string, it documents the attribute's purpose.
544
545 Extra metadata can be associated with the traitlet using the .tag() convenience method
546 or by using the traitlet instance's .metadata dictionary.
547 """
548 if default_value is not Undefined:
549 self.default_value = default_value
550 if allow_none:
551 self.allow_none = allow_none
552 if read_only is not None:
553 self.read_only = read_only
554 self.help = help if help is not None else ""
555 if self.help:
556 # define __doc__ so that inspectors like autodoc find traits
557 self.__doc__ = self.help
558
559 if len(kwargs) > 0:
560 stacklevel = 1
561 f = inspect.currentframe()
562 # count supers to determine stacklevel for warning
563 assert f is not None
564 while f.f_code.co_name == "__init__":
565 stacklevel += 1
566 f = f.f_back
567 assert f is not None
568 mod = f.f_globals.get("__name__") or ""
569 pkg = mod.split(".", 1)[0]
570 key = ("metadata-tag", pkg, *sorted(kwargs))
571 if should_warn(key):
572 warn(
573 f"metadata {kwargs} was set from the constructor. "
574 "With traitlets 4.1, metadata should be set using the .tag() method, "

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…