Skip to content

Commit 8e02ad8

Browse files
author
lihu
committed
Break out the dataclass test
py35 doesn't have support for the syntax needed to test this feature
1 parent 1f9745a commit 8e02ad8

File tree

7 files changed

+90
-22
lines changed

7 files changed

+90
-22
lines changed

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import sys
34
import pathlib
45
import shutil
@@ -42,3 +43,12 @@ def remove_sphinx_projects(sphinx_test_tempdir):
4243
@pytest.fixture
4344
def rootdir():
4445
return path(os.path.dirname(__file__) or '.').abspath() / 'roots'
46+
47+
48+
def pytest_ignore_collect(path, config):
49+
version_re = re.compile(r'_py(\d)(\d)\.py$')
50+
match = version_re.search(path.basename)
51+
if match:
52+
version = tuple(int(x) for x in match.groups())
53+
if sys.version_info < version:
54+
return True

tests/roots/test-dataclass/conf.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pathlib
2+
import sys
3+
4+
5+
# Make dataclass_module.py available for autodoc.
6+
sys.path.insert(0, str(pathlib.Path(__file__).parent))
7+
8+
9+
master_doc = 'index'
10+
11+
extensions = [
12+
'sphinx.ext.autodoc',
13+
'sphinx.ext.napoleon',
14+
'sphinx_autodoc_typehints',
15+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class DataClass:
6+
"""Class docstring."""
7+
8+
x: int

tests/roots/test-dataclass/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Dataclass Module
2+
================
3+
4+
.. autoclass:: dataclass_module.DataClass
5+
:undoc-members:
6+
:special-members: __init__

tests/roots/test-dummy/dummy_module.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,6 @@ def undocumented_function(x: int) -> str:
235235
return str(x)
236236

237237

238-
@dataclass
239-
class DataClass:
240-
"""Class docstring."""
241-
242-
x: int
243-
244-
245238
class Decorator:
246239
"""
247240
Initializer docstring.

tests/test_dataclass_py36.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pathlib
2+
import sys
3+
import textwrap
4+
5+
import pytest
6+
7+
8+
@pytest.mark.parametrize('always_document_param_types', [True, False])
9+
@pytest.mark.sphinx('text', testroot='dataclass')
10+
def test_sphinx_output(app, status, warning, always_document_param_types):
11+
test_path = pathlib.Path(__file__).parent
12+
13+
# Add test directory to sys.path to allow imports of dummy module.
14+
if str(test_path) not in sys.path:
15+
sys.path.insert(0, str(test_path))
16+
17+
app.config.always_document_param_types = always_document_param_types
18+
app.build()
19+
20+
assert 'build succeeded' in status.getvalue() # Build succeeded
21+
22+
format_args = {}
23+
if always_document_param_types:
24+
for indentation_level in range(3):
25+
format_args['undoc_params_{}'.format(indentation_level)] = textwrap.indent(
26+
'\n\n Parameters:\n **x** ("int") --', ' ' * indentation_level
27+
)
28+
else:
29+
for indentation_level in range(3):
30+
format_args['undoc_params_{}'.format(indentation_level)] = ''
31+
32+
text_path = pathlib.Path(app.srcdir) / '_build' / 'text' / 'index.txt'
33+
with text_path.open('r') as f:
34+
text_contents = f.read().replace('–', '--')
35+
expected_contents = textwrap.dedent('''\
36+
Dataclass Module
37+
****************
38+
39+
class dataclass_module.DataClass(x)
40+
41+
Class docstring.{undoc_params_0}
42+
43+
__init__(x)
44+
45+
Initialize self. See help(type(self)) for accurate signature.{undoc_params_1}
46+
''')
47+
expected_contents = expected_contents.format(**format_args).replace('–', '--')
48+
assert text_contents == expected_contents

tests/test_sphinx_autodoc_typehints.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,9 @@ def test_sphinx_output(app, status, warning, always_document_param_types):
224224

225225
format_args = {}
226226
if always_document_param_types:
227-
for indentation_level in range(3):
228-
format_args['undoc_params_{}'.format(indentation_level)] = textwrap.indent(
229-
'\n\n Parameters:\n **x** ("int") --', ' ' * indentation_level
230-
)
227+
format_args['undoc_params'] = '\n\n Parameters:\n **x** ("int") --'
231228
else:
232-
for indentation_level in range(3):
233-
format_args['undoc_params_{}'.format(indentation_level)] = ''
229+
format_args['undoc_params'] = ''
234230

235231
text_path = pathlib.Path(app.srcdir) / '_build' / 'text' / 'index.txt'
236232
with text_path.open('r') as f:
@@ -473,19 +469,11 @@ class dummy_module.ClassWithTypehintsNotInline(x=None)
473469
474470
dummy_module.undocumented_function(x)
475471
476-
Hi{undoc_params_0}
472+
Hi{undoc_params}
477473
478474
Return type:
479475
"str"
480476
481-
class dummy_module.DataClass(x)
482-
483-
Class docstring.{undoc_params_0}
484-
485-
__init__(x)
486-
487-
Initialize self. See help(type(self)) for accurate signature.{undoc_params_1}
488-
489477
@dummy_module.Decorator(func)
490478
491479
Initializer docstring.

0 commit comments

Comments
 (0)