MCPcopy Create free account
hub / github.com/cdgriffith/Box / __get_default

Method __get_default

box/box.py:659–716  ·  view source on GitHub ↗
(self, item)

Source from the content-addressed store, hash-verified

657 return False
658
659 def __get_default(self, item):
660 if item in ("getdoc", "shape") and _is_ipython():
661 return None
662 default_value = self._box_config["default_box_attr"]
663 if default_value in (self._box_config["box_class"], dict):
664 value = self._box_config["box_class"](**self.__box_config(extra_namespace=item))
665 elif isinstance(default_value, dict):
666 value = self._box_config["box_class"](**self.__box_config(extra_namespace=item), **default_value)
667 elif isinstance(default_value, list) or default_value is list:
668 items = default_value if isinstance(default_value, list) else None
669 value = box.BoxList(items, **self.__box_config(extra_namespace=item))
670 elif isinstance(default_value, Callable):
671 # Parameters named key / box_instance receive the missing key and this box. Any other
672 # parameter must be optional, so plain factories such as int, set or a lambda work too.
673 args = []
674 kwargs = {}
675 try:
676 params = list(signature(default_value).parameters.values())
677 except (TypeError, ValueError): # pragma: no cover (callables without a readable signature)
678 params = []
679 for param in params:
680 if param.kind in (param.VAR_POSITIONAL, param.VAR_KEYWORD):
681 continue
682 if param.name in ("key", "box_instance"):
683 passed = item if param.name == "key" else self
684 if param.kind is param.POSITIONAL_ONLY:
685 args.append(passed)
686 else:
687 kwargs[param.name] = passed
688 elif param.default is param.empty:
689 raise BoxError(
690 "default_box_attr can only have the required arguments 'key' and 'box_instance', "
691 f"not {param.name!r}"
692 )
693 value = default_value(*args, **kwargs)
694 elif hasattr(default_value, "copy"):
695 value = default_value.copy()
696 else:
697 value = default_value
698 if self._box_config["default_box_create_on_get"]:
699 if self.__process_dotted_key(item):
700 first_item, children = _parse_box_dots(self, item)
701 # An existing first_item only gets here when its value can't be looked into
702 # (for example an int), so there is nowhere to store the default
703 if first_item not in self.keys():
704 self._flush_pending_parent()
705 super().__setitem__(
706 first_item, self._box_config["box_class"](**self.__box_config(extra_namespace=first_item))
707 )
708 self[first_item].__setitem__(children, value)
709 else:
710 super().__setitem__(item, value)
711 elif isinstance(value, Box) and not self.__process_dotted_key(item):
712 # Lookups stay ephemeral, but a later assignment on this child
713 # should create the missing parent keys (see _flush_pending_parent).
714 # A strong reference is required: intermediate ephemeral parents are otherwise garbage collected.
715 value._box_config["__pending_parent"] = (self, item)
716 return value

Callers 3

getMethod · 0.95
__getitem__Method · 0.95
__getattr__Method · 0.95

Calls 10

__box_configMethod · 0.95
__process_dotted_keyMethod · 0.95
keysMethod · 0.95
_flush_pending_parentMethod · 0.95
BoxErrorClass · 0.90
_is_ipythonFunction · 0.85
_parse_box_dotsFunction · 0.85
appendMethod · 0.80
copyMethod · 0.45
__setitem__Method · 0.45

Tested by

no test coverage detected