Skip to content

Commit af93d38

Browse files
committed
docs: switch to sphinx-autodoc-typehints
Currently rdflib is relying on sphinx-autodoc's built in handling for type hints, this however has some deficiencies as it does not handle stuff inside `if TYPE_CHECKING:` and thus only work in cases where there are no potential for circular dependencies. This patch changes sphinx to use sphinx-autodoc-typehints instead, the documentation generated by this plugin looks slightly different but as a positive it correctly works with things defined inside `if TYPE_CHECKING:` guards. Also: - moved the `_NamespaceSetString` type alias to `rdflib._type_checking` This is so that `sphinx-autodoc-typehints` recognizes it up, as it does not handle type aliases that are defined inside `if TYPE_CHECKING:` guards in imported files. - remove sphinx requirements from requirements.dev.txt and replaced it with -r docs/sphinx-requirements.txt to reduce redundancy.
1 parent e9f5c22 commit af93d38

File tree

8 files changed

+42
-12
lines changed

8 files changed

+42
-12
lines changed

docs/conf.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"sphinxcontrib.apidoc",
3131
"sphinx.ext.autodoc",
3232
#'sphinx.ext.autosummary',
33+
"sphinx_autodoc_typehints",
3334
"sphinx.ext.doctest",
3435
"sphinx.ext.intersphinx",
3536
"sphinx.ext.todo",
@@ -42,8 +43,12 @@
4243

4344
apidoc_module_dir = "../rdflib"
4445
apidoc_output_dir = "apidocs"
46+
47+
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
4548
autodoc_default_options = {"special-members": True}
46-
autodoc_typehints = "both"
49+
50+
# https://github.com/tox-dev/sphinx-autodoc-typehints
51+
always_document_param_types = True
4752

4853
autosummary_generate = True
4954

docs/sphinx-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ myst-parser
33
sphinx<5
44
sphinxcontrib-apidoc
55
sphinxcontrib-kroki
6+
sphinx-autodoc-typehints

rdflib/_type_checking.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
This module contains type aliases that should only be used when type checking
3+
as it would otherwise introduce a runtime dependency on `typing_extensions` for
4+
older python versions which is not desirable.
5+
6+
This was made mainly to accommodate ``sphinx-autodoc-typehints`` which cannot
7+
recognize type aliases from imported files if the type aliases are defined
8+
inside ``if TYPE_CHECKING:``. So instead of placing the type aliases in normal
9+
modules inside ``TYPE_CHECKING`` guards they are in this file which should only
10+
be imported inside ``TYPE_CHECKING`` guards.
11+
12+
.. important::
13+
Things inside this module are not for use outside of RDFLib
14+
and this module is not part the the RDFLib public API.
15+
"""
16+
17+
import sys
18+
19+
__all__ = [
20+
"_NamespaceSetString",
21+
]
22+
23+
24+
if sys.version_info >= (3, 8):
25+
from typing import Literal as PyLiteral
26+
else:
27+
from typing_extensions import Literal as PyLiteral
28+
29+
_NamespaceSetString = PyLiteral["core", "rdflib", "none"]

rdflib/graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
assert Namespace # avoid warning
4141

4242
if TYPE_CHECKING:
43-
from rdflib.namespace import _NamespaceSetString
43+
from rdflib._type_checking import _NamespaceSetString
4444

4545
logger = logging.getLogger(__name__)
4646

rdflib/namespace/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,7 @@ def _ipython_key_completions_(self) -> List[str]:
344344
XMLNS = Namespace("http://www.w3.org/XML/1998/namespace")
345345

346346
if TYPE_CHECKING:
347-
if sys.version_info >= (3, 8):
348-
from typing import Literal as PyLiteral
349-
else:
350-
from typing_extensions import Literal as PyLiteral
351-
352-
_NamespaceSetString = PyLiteral["core", "rdflib", "none"]
347+
from rdflib._type_checking import _NamespaceSetString
353348

354349

355350
class NamespaceManager(object):

requirements.dev.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ flake8-black
66
html5lib
77
isort
88
mypy
9-
myst-parser
109
pytest
1110
pytest-cov
1211
pytest-subtests
13-
sphinx<5
14-
sphinxcontrib-apidoc
15-
sphinxcontrib-kroki
1612
types-setuptools
13+
-r docs/sphinx-requirements.txt

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ exclude = host,extras,transform,rdfs,pyRdfa,sparql,results,pyMicrodata
99
[coverage:run]
1010
branch = True
1111
source = rdflib
12+
omit =
13+
*/_type_checking.py
1214

1315
[coverage:report]
1416
# Regexes for lines to exclude from consideration

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"sphinx < 5",
2828
"sphinxcontrib-apidoc",
2929
"sphinxcontrib-kroki",
30+
"sphinx-autodoc-typehints",
3031
],
3132
"berkeleydb": ["berkeleydb"],
3233
"networkx": ["networkx"],

0 commit comments

Comments
 (0)