Skip to content

Commit 530d580

Browse files
committed
MAINT: split _clean_text_signature out to _utils.
All other functions defined in numpydoc.py module depend on sphinx and therefore can be skipped for testing purposes when sphinx is not installed. importorskips the numpydoc tests but maintains the testing of _clean_text_signature in new test_utils.py module.
1 parent 4d3b301 commit 530d580

File tree

4 files changed

+53
-48
lines changed

4 files changed

+53
-48
lines changed

numpydoc/_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import re
2+
3+
4+
def _clean_text_signature(sig):
5+
if sig is None:
6+
return None
7+
start_pattern = re.compile(r"^[^(]*\(")
8+
start, end = start_pattern.search(sig).span()
9+
start_sig = sig[start:end]
10+
sig = sig[end:-1]
11+
sig = re.sub(r"^\$(self|module|type)(,\s|$)", "", sig, count=1)
12+
sig = re.sub(r"(^|(?<=,\s))/,\s\*", "*", sig, count=1)
13+
return start_sig + sig + ")"

numpydoc/numpydoc.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from .docscrape_sphinx import get_doc_object
3838
from .validate import get_validation_checks, validate
3939
from .xref import DEFAULT_LINKS
40+
from ._utils import _clean_text_signature
4041

4142
logger = logging.getLogger(__name__)
4243

@@ -301,18 +302,6 @@ def mangle_signature(app: SphinxApp, what, name, obj, options, sig, retann):
301302
return sig, ""
302303

303304

304-
def _clean_text_signature(sig):
305-
if sig is None:
306-
return None
307-
start_pattern = re.compile(r"^[^(]*\(")
308-
start, end = start_pattern.search(sig).span()
309-
start_sig = sig[start:end]
310-
sig = sig[end:-1]
311-
sig = re.sub(r"^\$(self|module|type)(,\s|$)", "", sig, count=1)
312-
sig = re.sub(r"(^|(?<=,\s))/,\s\*", "*", sig, count=1)
313-
return start_sig + sig + ")"
314-
315-
316305
def setup(app: SphinxApp, get_doc_object_=get_doc_object):
317306
if not hasattr(app, "add_config_value"):
318307
return None # probably called by nose, better bail out

numpydoc/tests/test_numpydoc.py

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import pytest
2+
pytest.importorskip("sphinx")
3+
pytest.importorskip("docutils")
4+
15
from collections import defaultdict
26
from copy import deepcopy
37
from io import StringIO
48
from pathlib import PosixPath
59

6-
import pytest
710
from docutils import nodes
811
from sphinx.ext.autodoc import ALL
912
from sphinx.util import logging
1013

1114
from numpydoc.numpydoc import (
12-
_clean_text_signature,
1315
clean_backrefs,
1416
mangle_docstrings,
1517
update_config,
@@ -111,40 +113,6 @@ def test_mangle_docstrings_inherited_class_members():
111113
assert "samefile" not in lines
112114

113115

114-
def test_clean_text_signature():
115-
assert _clean_text_signature(None) is None
116-
assert _clean_text_signature("func($self)") == "func()"
117-
assert (
118-
_clean_text_signature("func($self, *args, **kwargs)") == "func(*args, **kwargs)"
119-
)
120-
assert _clean_text_signature("($self)") == "()"
121-
assert _clean_text_signature("()") == "()"
122-
assert _clean_text_signature("func()") == "func()"
123-
assert (
124-
_clean_text_signature("func($self, /, *args, **kwargs)")
125-
== "func(*args, **kwargs)"
126-
)
127-
assert (
128-
_clean_text_signature("func($self, other, /, *args, **kwargs)")
129-
== "func(other, *args, **kwargs)"
130-
)
131-
assert _clean_text_signature("($module)") == "()"
132-
assert _clean_text_signature("func($type)") == "func()"
133-
assert (
134-
_clean_text_signature('func($self, foo="hello world")')
135-
== 'func(foo="hello world")'
136-
)
137-
assert (
138-
_clean_text_signature("func($self, foo='hello world')")
139-
== "func(foo='hello world')"
140-
)
141-
assert _clean_text_signature('func(foo="hello world")') == 'func(foo="hello world")'
142-
assert _clean_text_signature('func(foo="$self")') == 'func(foo="$self")'
143-
assert _clean_text_signature('func($self, foo="$self")') == 'func(foo="$self")'
144-
assert _clean_text_signature("func(self, other)") == "func(self, other)"
145-
assert _clean_text_signature("func($self, *args)") == "func(*args)"
146-
147-
148116
@pytest.fixture
149117
def f():
150118
def _function_without_seealso_and_examples():

numpydoc/tests/test_utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from numpydoc._utils import _clean_text_signature
2+
3+
4+
def test_clean_text_signature():
5+
assert _clean_text_signature(None) is None
6+
assert _clean_text_signature("func($self)") == "func()"
7+
assert (
8+
_clean_text_signature("func($self, *args, **kwargs)") == "func(*args, **kwargs)"
9+
)
10+
assert _clean_text_signature("($self)") == "()"
11+
assert _clean_text_signature("()") == "()"
12+
assert _clean_text_signature("func()") == "func()"
13+
assert (
14+
_clean_text_signature("func($self, /, *args, **kwargs)")
15+
== "func(*args, **kwargs)"
16+
)
17+
assert (
18+
_clean_text_signature("func($self, other, /, *args, **kwargs)")
19+
== "func(other, *args, **kwargs)"
20+
)
21+
assert _clean_text_signature("($module)") == "()"
22+
assert _clean_text_signature("func($type)") == "func()"
23+
assert (
24+
_clean_text_signature('func($self, foo="hello world")')
25+
== 'func(foo="hello world")'
26+
)
27+
assert (
28+
_clean_text_signature("func($self, foo='hello world')")
29+
== "func(foo='hello world')"
30+
)
31+
assert _clean_text_signature('func(foo="hello world")') == 'func(foo="hello world")'
32+
assert _clean_text_signature('func(foo="$self")') == 'func(foo="$self")'
33+
assert _clean_text_signature('func($self, foo="$self")') == 'func(foo="$self")'
34+
assert _clean_text_signature("func(self, other)") == "func(self, other)"
35+
assert _clean_text_signature("func($self, *args)") == "func(*args)"

0 commit comments

Comments
 (0)