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)
|
return _recombine_set_tokens(tokens)
|
||||||
|
|
||||||
|
|
||||||
def _parse_numpy_type_spec(_type):
|
def _convert_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 token_type(token):
|
def token_type(token):
|
||||||
if token.startswith(" ") or token.endswith(" "):
|
if token.startswith(" ") or token.endswith(" "):
|
||||||
type_ = "delimiter"
|
type_ = "delimiter"
|
||||||
@ -866,7 +860,7 @@ def _parse_numpy_type_spec2(_type):
|
|||||||
def convert_obj(obj, translations, default_translation=":obj:`{}`"):
|
def convert_obj(obj, translations, default_translation=":obj:`{}`"):
|
||||||
return translations.get(obj, default_translation.format(obj))
|
return translations.get(obj, default_translation.format(obj))
|
||||||
|
|
||||||
tokens = tokenize_type_spec(_type)
|
tokens = _tokenize_type_spec(_type)
|
||||||
types = [
|
types = [
|
||||||
(token, token_type(token))
|
(token, token_type(token))
|
||||||
for token in tokens
|
for token in tokens
|
||||||
@ -895,7 +889,9 @@ def _parse_numpy_type_spec2(_type):
|
|||||||
"reference": lambda x: x,
|
"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):
|
class NumpyDocstring(GoogleDocstring):
|
||||||
@ -1006,7 +1002,7 @@ class NumpyDocstring(GoogleDocstring):
|
|||||||
_name, _type = line, ''
|
_name, _type = line, ''
|
||||||
_name, _type = _name.strip(), _type.strip()
|
_name, _type = _name.strip(), _type.strip()
|
||||||
_name = self._escape_args_and_kwargs(_name)
|
_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:
|
if prefer_type and not _type:
|
||||||
_type, _name = _name, _type
|
_type, _name = _name, _type
|
||||||
|
@ -16,7 +16,7 @@ from unittest import TestCase, mock
|
|||||||
|
|
||||||
from sphinx.ext.napoleon import Config
|
from sphinx.ext.napoleon import Config
|
||||||
from sphinx.ext.napoleon.docstring import GoogleDocstring, NumpyDocstring
|
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'))):
|
class NamedtupleSubclass(namedtuple('NamedtupleSubclass', ('attr1', 'attr2'))):
|
||||||
@ -2041,7 +2041,7 @@ definition_after_normal_text : int
|
|||||||
actual = _tokenize_type_spec(type_spec)
|
actual = _tokenize_type_spec(type_spec)
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
def test_parse_numpy_type_spec(self):
|
def test_convert_numpy_type_spec(self):
|
||||||
types = (
|
types = (
|
||||||
"str",
|
"str",
|
||||||
"int or float or None",
|
"int or float or None",
|
||||||
@ -2049,34 +2049,43 @@ definition_after_normal_text : int
|
|||||||
"{'F', 'C', 'N'}",
|
"{'F', 'C', 'N'}",
|
||||||
)
|
)
|
||||||
modifiers = (
|
modifiers = (
|
||||||
|
"",
|
||||||
"optional",
|
"optional",
|
||||||
"default: None",
|
"default: None",
|
||||||
)
|
)
|
||||||
|
|
||||||
type_tokens = (
|
|
||||||
["str"],
|
|
||||||
["int", " or ", "float", " or ", "None"],
|
|
||||||
['{"F", "C", "N"}'],
|
|
||||||
["{'F', 'C', 'N'}"],
|
|
||||||
)
|
|
||||||
modifier_tokens = (
|
|
||||||
["optional"],
|
|
||||||
["default", ": ", "None"],
|
|
||||||
)
|
|
||||||
|
|
||||||
type_specs = tuple(
|
type_specs = tuple(
|
||||||
", ".join([type_, modifier])
|
", ".join([type_, modifier])
|
||||||
|
if modifier
|
||||||
|
else type_
|
||||||
for type_ in types
|
for type_ in types
|
||||||
for modifier in modifiers
|
for modifier in modifiers
|
||||||
)
|
)
|
||||||
tokens = tuple(
|
|
||||||
tuple(tokens_ + [", "] + modifier_tokens_)
|
converted_types = (
|
||||||
for tokens_ in type_tokens
|
":obj:`str`",
|
||||||
for modifier_tokens_ in modifier_tokens
|
":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):
|
for type_, expected in zip(type_specs, converted):
|
||||||
actual = _parse_numpy_type_spec(type_spec)
|
actual = _convert_numpy_type_spec(type_)
|
||||||
self.assertEqual(expected, actual)
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
def test_parameter_types(self):
|
def test_parameter_types(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user