|
1 | 1 | """Tests for :mod:`sphinx.ext.napoleon.docstring` module.""" |
2 | 2 |
|
3 | 3 | import re |
| 4 | +import zlib |
4 | 5 | from collections import namedtuple |
5 | 6 | from inspect import cleandoc |
| 7 | +from itertools import product |
6 | 8 | from textwrap import dedent |
7 | 9 | from unittest import mock |
8 | 10 |
|
9 | 11 | import pytest |
| 12 | +from html5lib import HTMLParser |
10 | 13 |
|
| 14 | +from sphinx.ext.intersphinx import load_mappings, normalize_intersphinx_mapping |
11 | 15 | from sphinx.ext.napoleon import Config |
12 | 16 | from sphinx.ext.napoleon.docstring import ( |
13 | 17 | GoogleDocstring, |
@@ -2659,3 +2663,42 @@ def test_napoleon_and_autodoc_typehints_description_documented_params(app, statu |
2659 | 2663 | '\n' |
2660 | 2664 | ' * ****kwargs** (*int*) -- Extra arguments.\n' |
2661 | 2665 | ) |
| 2666 | + |
| 2667 | + |
| 2668 | +@pytest.mark.sphinx('html', testroot='ext-napoleon-paramtype', freshenv=True) |
| 2669 | +def test_napoleon_keyword_and_paramtype(app, tmp_path): |
| 2670 | + inv_file = tmp_path / 'objects.inv' |
| 2671 | + inv_file.write_bytes(b'''\ |
| 2672 | +# Sphinx inventory version 2 |
| 2673 | +# Project: Intersphinx Test |
| 2674 | +# Version: 42 |
| 2675 | +# The remainder of this file is compressed using zlib. |
| 2676 | +''' + zlib.compress(b'''\ |
| 2677 | +None py:data 1 none.html - |
| 2678 | +list py:class 1 list.html - |
| 2679 | +int py:class 1 int.html - |
| 2680 | +''')) # NoQA: W291 |
| 2681 | + app.config.intersphinx_mapping = {'python': ('127.0.0.1:5555', str(inv_file))} |
| 2682 | + normalize_intersphinx_mapping(app, app.config) |
| 2683 | + load_mappings(app) |
| 2684 | + |
| 2685 | + app.build(force_all=True) |
| 2686 | + |
| 2687 | + buffer = (app.outdir / 'index.html').read_bytes() |
| 2688 | + etree = HTMLParser(namespaceHTMLElements=False).parse(buffer) |
| 2689 | + |
| 2690 | + for name, typename in product(('keyword', 'kwarg', 'kwparam'), ('paramtype', 'kwtype')): |
| 2691 | + param = f'{name}_{typename}' |
| 2692 | + li_ = list(etree.findall(f'.//li/p/strong[.="{param}"]/../..')) |
| 2693 | + assert len(li_) == 1 |
| 2694 | + li = li_[0] |
| 2695 | + |
| 2696 | + text = li.text or ''.join(li.itertext()) |
| 2697 | + assert text == f'{param} (list[int]) \u2013 some param' |
| 2698 | + |
| 2699 | + a_ = list(li.findall('.//a[@class="reference external"]')) |
| 2700 | + |
| 2701 | + assert len(a_) == 2 |
| 2702 | + for a, uri in zip(a_, ('list.html', 'int.html')): |
| 2703 | + assert a.attrib['href'] == f'127.0.0.1:5555/{uri}' |
| 2704 | + assert a.attrib['title'] == '(in Intersphinx Test v42)' |
0 commit comments