move the numpy type spec parsing function out of NumpyDocstring

This commit is contained in:
Keewis 2020-05-22 19:26:31 +02:00
parent e9822139ee
commit ace933107a

View File

@ -792,6 +792,84 @@ class GoogleDocstring:
return lines
def _parse_numpy_type_spec(_type):
def recombine_set(tokens):
def combine_set(tokens):
in_set = False
set_items = []
for token in tokens:
if token.startswith("{"):
in_set = True
elif token.endswith("}"):
in_set = False
set_items.append(token)
if in_set:
set_items.append(token)
else:
if set_items:
token = "".join(set_items)
set_items = []
yield token
return list(combine_set(tokens))
def tokenize_type_spec(spec):
delimiters = r"(\sor\s|\sof\s|:\s|,\s|[{]|[}])"
split = [
item
for item in re.split(delimiters, _type)
if item
]
tokens = recombine_set(split)
return tokens
def token_type(token):
if token.startswith(" ") or token.endswith(" "):
type_ = "delimiter"
elif token.startswith("{") and token.endswith("}"):
type_ = "value_set"
elif token in ("optional", "default"):
type_ = "control"
elif re.match(":[^:]+:`[^`]+`", token):
type_ = "reference"
elif token.isnumeric() or (token.startswith('"') and token.endswith('"')):
type_ = "literal"
else:
type_ = "obj"
return type_
def convert_obj(obj, translations):
return translations.get(obj, ":obj:`{}`".format(obj))
tokens = tokenize_type_spec(_type)
types = [
(token, token_type(token))
for token in tokens
]
# TODO: make this configurable
translations = {
"sequence": ":term:`sequence`",
"mapping": ":term:`mapping`",
"dict-like": ":term:`dict-like <mapping>`",
}
converters = {
"value_set": lambda x: f":noref:`{x}`",
"literal": lambda x: f":noref:`{x}`",
"obj": lambda x: convert_obj(x, translations),
"control": lambda x: f":noref:`{x}`",
"delimiter": lambda x: x,
"reference": lambda x: x,
}
return "".join(converters.get(type_)(token) for token, type_ in types)
class NumpyDocstring(GoogleDocstring):
"""Convert NumPy style docstrings to reStructuredText.
@ -891,83 +969,6 @@ class NumpyDocstring(GoogleDocstring):
self._directive_sections = ['.. index::']
super().__init__(docstring, config, app, what, name, obj, options)
def _convert_type_spec(self, _type):
def recombine_set(tokens):
def combine_set(tokens):
in_set = False
set_items = []
for token in tokens:
if token.startswith("{"):
in_set = True
elif token.endswith("}"):
in_set = False
set_items.append(token)
if in_set:
set_items.append(token)
else:
if set_items:
token = "".join(set_items)
set_items = []
yield token
return list(combine_set(tokens))
def tokenize_type_spec(spec):
delimiters = r"(\sor\s|\sof\s|:\s|,\s|[{]|[}])"
split = [
item
for item in re.split(delimiters, _type)
if item
]
tokens = recombine_set(split)
return tokens
def token_type(token):
if token.startswith(" ") or token.endswith(" "):
type_ = "delimiter"
elif token.startswith("{") and token.endswith("}"):
type_ = "value_set"
elif token in ("optional", "default"):
type_ = "control"
elif re.match(":[^:]+:`[^`]+`", token):
type_ = "reference"
elif token.isnumeric() or (token.startswith('"') and token.endswith('"')):
type_ = "literal"
else:
type_ = "obj"
return type_
def convert_obj(obj, translations):
return translations.get(obj, ":obj:`{}`".format(obj))
tokens = tokenize_type_spec(_type)
types = [
(token, token_type(token))
for token in tokens
]
# TODO: make this configurable
translations = {
"sequence": ":term:`sequence`",
"mapping": ":term:`mapping`",
"dict-like": ":term:`dict-like <mapping>`",
}
converters = {
"value_set": lambda x: f":noref:`{x}`",
"literal": lambda x: f":noref:`{x}`",
"obj": lambda x: convert_obj(x, translations),
"control": lambda x: f":noref:`{x}`",
"delimiter": lambda x: x,
"reference": lambda x: x,
}
return "".join(converters.get(type_)(token) for token, type_ in types)
def _consume_field(self, parse_type: bool = True, prefer_type: bool = False
) -> Tuple[str, str, List[str]]:
line = next(self._line_iter)
@ -977,7 +978,7 @@ class NumpyDocstring(GoogleDocstring):
_name, _type = line, ''
_name, _type = _name.strip(), _type.strip()
_name = self._escape_args_and_kwargs(_name)
_type = self._convert_type_spec(_type)
_type = _parse_numpy_type_spec(_type)
if prefer_type and not _type:
_type, _name = _name, _type