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

Method __get_default

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

Source from the content-addressed store, hash-verified

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

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.80
__setitem__Method · 0.45

Tested by

no test coverage detected