Skip to content

Commit 5f4d35b

Browse files
committed
compatibility fixes for sphinx 4.0
closes #51
1 parent 0cb7b97 commit 5f4d35b

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

.github/workflows/python-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
python-version: [3.5, 3.6, 3.7, 3.8]
18-
sphinx-version: ["", "3.5.*", "3.4.*", "3.2.*", "3.1.*", "3.0.*", "2.4.*", "2.3.*", "2.2.*"]
18+
sphinx-version: ["", "4.0.*", "3.5.*", "3.4.*", "3.2.*", "3.1.*", "3.0.*", "2.4.*", "2.3.*", "2.2.*"]
1919

2020
steps:
2121
- uses: actions/checkout@v2

autodocsumm/__init__.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@
4040

4141
sphinx_version = list(map(float, re.findall(r'\d+', sphinx.__version__)[:3]))
4242

43-
from sphinx.util import force_decode
43+
try:
44+
from sphinx.util import force_decode
45+
except ImportError:
46+
# force_decode has been removed with sphinx 4.0
47+
def force_decode(string: str, encoding: str) -> str:
48+
return string
4449

4550

4651
try:
@@ -89,6 +94,15 @@ def bool_option(*args):
8994
return "True"
9095

9196

97+
def _get_arg(param, pos, default, *args, **kwargs):
98+
if param in kwargs:
99+
return kwargs[param]
100+
elif len(args) > pos:
101+
return args[pos]
102+
else:
103+
return default
104+
105+
92106
class AutosummaryDocumenter(object):
93107
"""Abstract class for for extending Documenter methods
94108
@@ -369,13 +383,14 @@ def format_args(self):
369383
return None
370384
return process_signature(callmeth)
371385

372-
def get_doc(self, encoding=None, ignore=1):
386+
def get_doc(self, *args, **kwargs):
373387
"""Reimplemented to include data from the call method"""
374388
content = self.env.config.autodata_content
375389
if content not in ('both', 'call') or not self.get_attr(
376390
self.get_attr(self.object, '__call__', None), '__doc__'):
377391
return super(CallableDataDocumenter, self).get_doc(
378-
encoding=encoding, ignore=ignore)
392+
*args, **kwargs
393+
)
379394

380395
# for classes, what the "docstring" is can be controlled via a
381396
# config value; the default is both docstrings
@@ -413,13 +428,14 @@ def format_args(self):
413428
return None
414429
return process_signature(callmeth)
415430

416-
def get_doc(self, encoding=None, ignore=1):
431+
def get_doc(self, *args, **kwargs):
417432
"""Reimplemented to include data from the call method"""
418433
content = self.env.config.autodata_content
419434
if content not in ('both', 'call') or not self.get_attr(
420435
self.get_attr(self.object, '__call__', None), '__doc__'):
421436
return super(CallableAttributeDocumenter, self).get_doc(
422-
encoding=encoding, ignore=ignore)
437+
*args, **kwargs
438+
)
423439

424440
# for classes, what the "docstring" is can be controlled via a
425441
# config value; the default is both docstrings
@@ -435,10 +451,17 @@ def get_doc(self, encoding=None, ignore=1):
435451
docstrings.append(calldocstring + '\n')
436452

437453
doc = []
438-
for docstring in docstrings:
439-
if not isinstance(docstring, str):
440-
docstring = force_decode(docstring, encoding)
441-
doc.append(prepare_docstring(docstring, ignore))
454+
if sphinx_version < [4, 0]:
455+
encoding = _get_arg("encoding", 0, None, *args, **kwargs)
456+
ignore = _get_arg("ignore", 1, 1, *args, **kwargs)
457+
for docstring in docstrings:
458+
if not isinstance(docstring, str):
459+
docstring = force_decode(docstring, encoding)
460+
doc.append(prepare_docstring(docstring, ignore))
461+
else:
462+
ignore = _get_arg("ignore", 0, 1, *args, **kwargs)
463+
for docstring in docstrings:
464+
doc.append(prepare_docstring(docstring, ignore))
442465

443466
return doc
444467

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def readme():
3636
license="GPLv2",
3737
packages=find_packages(exclude=['docs', 'tests*', 'examples']),
3838
install_requires=[
39-
'Sphinx>=2.2.*,<4.0.*',
39+
'Sphinx>=2.2.*',
4040
],
4141
setup_requires=pytest_runner,
4242
tests_require=['pytest', 'sphinx-testing'],

tests/test_autodocsumm.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ def test_module_no_nesting(self, app, status, warning):
115115
)
116116

117117
# test the members are still displayed
118-
self.assertIn('<dt id="dummy.Class_CallTest">', html)
118+
self.assertRegexpMatches(
119+
html,
120+
r'<dt( class=".*")? id="dummy.Class_CallTest"( class=".*")*>'
121+
)
119122

120123
@with_app(buildername='html', srcdir=sphinx_supp,
121124
copy_srcdir_to_tmpdir=True)
@@ -129,7 +132,10 @@ def test_module_summary_only(self, app, status, warning):
129132
self.assertIn('<span class="pre">large_data</span>', html)
130133
self.assertIn('<span class="pre">small_data</span>', html)
131134

132-
self.assertNotIn('<dt id="dummy.Class_CallTest">', html)
135+
self.assertNotRegexpMatches(
136+
html,
137+
r'<dt( class=".*")? id="dummy.Class_CallTest"( class=".*")*>'
138+
)
133139

134140
@with_app(buildername='html', srcdir=sphinx_supp,
135141
copy_srcdir_to_tmpdir=True)
@@ -198,7 +204,10 @@ def test_module_nosignatures(self, app, status, warning):
198204
self.assertIn('<span class="pre">large_data</span>', html)
199205
self.assertIn('<span class="pre">small_data</span>', html)
200206

201-
self.assertNotIn('<dt id="dummy.Class_CallTest">', html)
207+
self.assertNotRegexpMatches(
208+
html,
209+
r'<dt( class=".*")? id="dummy.Class_CallTest"( class=".*")*>'
210+
)
202211
self.assertNotIn('()', html)
203212

204213
@with_app(buildername='html', srcdir=sphinx_supp,
@@ -305,7 +314,10 @@ def test_class_summary_only(self, app, status, warning):
305314
self.assertIn('<span class="pre">large_data</span>', html)
306315
self.assertIn('<span class="pre">small_data</span>', html)
307316

308-
self.assertNotIn('<dt id="dummy.TestClass.small_data">', html)
317+
self.assertNotRegexpMatches(
318+
html,
319+
r'<dt( class=".*")? id="dummy.TestClass.small_data"( class=".*")*>'
320+
)
309321

310322
@with_app(buildername='html', srcdir=sphinx_supp,
311323
copy_srcdir_to_tmpdir=True)
@@ -332,7 +344,11 @@ def test_class_nosignatures(self, app, status, warning):
332344
self.assertIn('<span class="pre">large_data</span>', html)
333345
self.assertIn('<span class="pre">small_data</span>', html)
334346

335-
self.assertNotIn('<dt id="dummy.TestClass.small_data">', html)
347+
self.assertNotRegexpMatches(
348+
html,
349+
r'<dt( class=".*")? id="dummy.TestClass.small_data"( class=".*")*>'
350+
)
351+
336352
self.assertNotIn('()', html)
337353

338354
@with_app(buildername='html', srcdir=sphinx_supp,

0 commit comments

Comments
 (0)