mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
get the type converter function to work, verified by new tests
This commit is contained in:
parent
25937f745a
commit
fc70205fb4
@ -840,13 +840,7 @@ def _tokenize_type_spec(spec):
|
||||
return _recombine_set_tokens(tokens)
|
||||
|
||||
|
||||
def _parse_numpy_type_spec(_type):
|
||||
raw_tokens = _tokenize_type_spec(_type)
|
||||
tokens = list(_recombine_set_tokens(raw_tokens))
|
||||
return tokens
|
||||
|
||||
|
||||
def _parse_numpy_type_spec2(_type):
|
||||
def _convert_numpy_type_spec(_type):
|
||||
def token_type(token):
|
||||
if token.startswith(" ") or token.endswith(" "):
|
||||
type_ = "delimiter"
|
||||
@ -866,7 +860,7 @@ def _parse_numpy_type_spec2(_type):
|
||||
def convert_obj(obj, translations, default_translation=":obj:`{}`"):
|
||||
return translations.get(obj, default_translation.format(obj))
|
||||
|
||||
tokens = tokenize_type_spec(_type)
|
||||
tokens = _tokenize_type_spec(_type)
|
||||
types = [
|
||||
(token, token_type(token))
|
||||
for token in tokens
|
||||
@ -895,7 +889,9 @@ def _parse_numpy_type_spec2(_type):
|
||||
"reference": lambda x: x,
|
||||
}
|
||||
|
||||
return "".join(converters.get(type_)(token) for token, type_ in types)
|
||||
converted = "".join(converters.get(type_)(token) for token, type_ in types)
|
||||
|
||||
return converted
|
||||
|
||||
|
||||
class NumpyDocstring(GoogleDocstring):
|
||||
@ -1006,7 +1002,7 @@ class NumpyDocstring(GoogleDocstring):
|
||||
_name, _type = line, ''
|
||||
_name, _type = _name.strip(), _type.strip()
|
||||
_name = self._escape_args_and_kwargs(_name)
|
||||
_type = _parse_numpy_type_spec(_type)
|
||||
_type = _convert_numpy_type_spec(_type)
|
||||
|
||||
if prefer_type and not _type:
|
||||
_type, _name = _name, _type
|
||||
|
@ -16,7 +16,7 @@ from unittest import TestCase, mock
|
||||
|
||||
from sphinx.ext.napoleon import Config
|
||||
from sphinx.ext.napoleon.docstring import GoogleDocstring, NumpyDocstring
|
||||
from sphinx.ext.napoleon.docstring import _tokenize_type_spec, _recombine_set_tokens, _parse_numpy_type_spec
|
||||
from sphinx.ext.napoleon.docstring import _tokenize_type_spec, _recombine_set_tokens, _convert_numpy_type_spec
|
||||
|
||||
|
||||
class NamedtupleSubclass(namedtuple('NamedtupleSubclass', ('attr1', 'attr2'))):
|
||||
@ -2041,7 +2041,7 @@ definition_after_normal_text : int
|
||||
actual = _tokenize_type_spec(type_spec)
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_parse_numpy_type_spec(self):
|
||||
def test_convert_numpy_type_spec(self):
|
||||
types = (
|
||||
"str",
|
||||
"int or float or None",
|
||||
@ -2049,34 +2049,43 @@ definition_after_normal_text : int
|
||||
"{'F', 'C', 'N'}",
|
||||
)
|
||||
modifiers = (
|
||||
"",
|
||||
"optional",
|
||||
"default: None",
|
||||
)
|
||||
|
||||
type_tokens = (
|
||||
["str"],
|
||||
["int", " or ", "float", " or ", "None"],
|
||||
['{"F", "C", "N"}'],
|
||||
["{'F', 'C', 'N'}"],
|
||||
)
|
||||
modifier_tokens = (
|
||||
["optional"],
|
||||
["default", ": ", "None"],
|
||||
)
|
||||
|
||||
type_specs = tuple(
|
||||
", ".join([type_, modifier])
|
||||
if modifier
|
||||
else type_
|
||||
for type_ in types
|
||||
for modifier in modifiers
|
||||
)
|
||||
tokens = tuple(
|
||||
tuple(tokens_ + [", "] + modifier_tokens_)
|
||||
for tokens_ in type_tokens
|
||||
for modifier_tokens_ in modifier_tokens
|
||||
|
||||
converted_types = (
|
||||
":obj:`str`",
|
||||
":obj:`int` or :obj:`float` or :obj:`None`",
|
||||
':noref:`{"F", "C", "N"}`',
|
||||
":noref:`{'F', 'C', 'N'}`",
|
||||
)
|
||||
converted_modifiers = (
|
||||
"",
|
||||
":noref:`optional`",
|
||||
":noref:`default`: :obj:`None`",
|
||||
)
|
||||
converted = tuple(
|
||||
", ".join([converted_type, converted_modifier])
|
||||
if converted_modifier
|
||||
else (
|
||||
type_
|
||||
if ("{" not in type_ and "or" not in type_)
|
||||
else converted_type
|
||||
)
|
||||
for converted_type, type_ in zip(converted_types, types)
|
||||
for converted_modifier in converted_modifiers
|
||||
)
|
||||
|
||||
for type_spec, expected in zip(type_specs, tokens):
|
||||
actual = _parse_numpy_type_spec(type_spec)
|
||||
for type_, expected in zip(type_specs, converted):
|
||||
actual = _convert_numpy_type_spec(type_)
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_parameter_types(self):
|
||||
|
Loading…
Reference in New Issue
Block a user