def test_update_d(self) -> None: """ Tests inline update dict """ d = {} u = {"a": 5} self.assertDictEqual(update_d(d, u), u) self.assertDictEqual(d, u) del d d = {} self.assertDictEqual(update_d(d, **u), u) self.assertDictEqual(d, u)
def _parse_phase_rest( intermediate_repr, scanned, default_search_announce, infer_type, word_wrap, emit_default_prop, emit_default_doc, return_tokens, ): """ :param intermediate_repr: a dictionary of form { "name": Optional[str], "type": Optional[str], "doc": Optional[str], "params": OrderedDict[str, {'typ': str, 'doc': Optional[str], 'default': Any}] "returns": Optional[OrderedDict[Literal['return_type'], {'typ': str, 'doc': Optional[str], 'default': Any}),)]] } :type intermediate_repr: ```dict``` :param scanned: List with each element a tuple of (whether value is a token, value) :type scanned: ```List[Tuple[bool, str]]``` :param default_search_announce: Default text(s) to look for. If None, uses default specified in default_utils. :type default_search_announce: ```Optional[Union[str, Iterable[str]]]``` :param infer_type: Whether to try inferring the typ (from the default) :type infer_type: ```bool``` :param word_wrap: Whether to word-wrap. Set `DOCTRANS_LINE_LENGTH` to configure length. :type word_wrap: ```bool``` :param emit_default_prop: Whether to include the default dictionary property. :type emit_default_prop: ```bool``` :param emit_default_doc: Whether help/docstring should include 'With default' text :type emit_default_doc: ```bool``` :param return_tokens: Valid tokens like `":rtype:"` :type return_tokens: ```Tuple[str]``` """ param = [ None, {}, ] # First elem is name and second elem is dict with keys: 'typ', 'doc', 'default' for is_token, line in scanned: if is_token is True: if any(map(line.startswith, return_tokens)): nxt_colon = line.find(":", 1) val = line[nxt_colon + 1:].strip() if intermediate_repr["returns"] is None: intermediate_repr["returns"] = OrderedDict( (("return_type", {}), )) intermediate_repr["returns"]["return_type"].update( interpolate_defaults( ( "return_type", dict((_set_param_values(line, val, return_tokens[-1]), )), ), emit_default_doc=emit_default_doc, default_search_announce=default_search_announce, )[1]) else: fst_space = line.find(" ") nxt_colon = line.find(":", fst_space) name = line[fst_space + 1:nxt_colon] if param[0] is not None and not param[0] == name: if not param[0][0] == "*": intermediate_repr["params"][param[0]] = param[1] param = [None, {}] val = line[nxt_colon + 1:].strip() param = _set_name_and_type( interpolate_defaults( ( name, update_d(param[1], dict((_set_param_values(line, val), ))), ), emit_default_doc=emit_default_doc, ), infer_type=infer_type, word_wrap=word_wrap, ) if not emit_default_doc and not emit_default_prop: param = _remove_default_from_param( param, emit_default_prop=emit_default_doc) elif not intermediate_repr["doc"]: intermediate_repr["doc"] = line.strip() if param != [None, {}]: # if param['name'] == 'return_type': intermediate_repr['returns'] = param name, param = _set_name_and_type( interpolate_defaults(param, emit_default_doc=emit_default_doc), infer_type=infer_type, word_wrap=word_wrap, ) if not emit_default_doc and not emit_default_prop: name, param = _remove_default_from_param( (name, param), emit_default_prop=emit_default_doc) intermediate_repr["params"][name] = param
def _param2docstring_param( param, docstring_format="rest", emit_default_doc=True, indent_level=1, emit_types=False, ): """ Converts param dict from intermediate_repr to the right string representation :param param: Name, dict with keys: 'typ', 'doc', 'default' :type param: ```Tuple[str, dict]``` :param docstring_format: Format of docstring :type docstring_format: ```Literal['rest', 'numpydoc', 'google']``` :param emit_default_doc: Whether help/docstring should include 'With default' text :type emit_default_doc: ```bool``` :param indent_level: indentation level whence: 0=no_tabs, 1=one tab; 2=two tabs :type indent_level: ```int``` :param emit_types: whether to show `:type` lines :type emit_types: ```bool``` """ # if not isinstance(param, tuple): assert isinstance(param, tuple), "Expected 'tuple' got `{!r}`".format( type(param).__name__) name, _param = param del param if "doc" in _param: doc, default = extract_default(_param["doc"], emit_default_doc=emit_default_doc) if default is not None: _param["default"] = default _sep = abs(indent_level) * tab def _joiner(__param, param_type): """ Internal function to join new lines :param __param: The `:param` :type __param: ```Optional[str]``` :param param_type: The `:type` :type param_type: ```Optional[str]``` :returns: Newline joined string :rtype: ```str``` """ if param_type is None and __param is not None: return "{}\n{}".format(_fill(__param), _sep) elif __param is None: return __param return "".join(( _fill(__param.replace("\n", "\n{sep}".format(sep=_sep))), "\n", _sep, _fill(param_type.replace("\n", "\n{sep}".format(sep=_sep))), "\n", _sep, )) return _joiner( (emit_param_str( ( name, update_d( _param, doc=multiline( indent_all_but_first( set_default_doc( (name, _param), emit_default_doc=emit_default_doc, )[1]["doc"], indent_level=indent_level - 1, ), quote_with=("", ""), ), ), ), emit_type=False, emit_default_doc=emit_default_doc, style=docstring_format, word_wrap=word_wrap, )) if _param.get("doc") else None, (None if _param.get("typ") is None or not emit_types else emit_param_str( (name, _param), emit_doc=False, emit_default_doc=emit_default_doc, style=docstring_format, word_wrap=word_wrap, )), )