Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: switch to sphinx-autodoc-typehints #1854

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"sphinxcontrib.apidoc",
"sphinx.ext.autodoc",
#'sphinx.ext.autosummary',
"sphinx_autodoc_typehints",
"sphinx.ext.doctest",
"sphinx.ext.intersphinx",
"sphinx.ext.todo",
Expand All @@ -42,8 +43,12 @@

apidoc_module_dir = "../rdflib"
apidoc_output_dir = "apidocs"

# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
autodoc_default_options = {"special-members": True}
autodoc_typehints = "both"

# https://github.com/tox-dev/sphinx-autodoc-typehints
always_document_param_types = True

autosummary_generate = True

Expand Down
1 change: 1 addition & 0 deletions docs/sphinx-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ myst-parser
sphinx<5
sphinxcontrib-apidoc
sphinxcontrib-kroki
sphinx-autodoc-typehints
29 changes: 29 additions & 0 deletions rdflib/_type_checking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
This module contains type aliases that should only be used when type checking
as it would otherwise introduce a runtime dependency on `typing_extensions` for
older python versions which is not desirable.

This was made mainly to accommodate ``sphinx-autodoc-typehints`` which cannot
recognize type aliases from imported files if the type aliases are defined
inside ``if TYPE_CHECKING:``. So instead of placing the type aliases in normal
modules inside ``TYPE_CHECKING`` guards they are in this file which should only
be imported inside ``TYPE_CHECKING`` guards.

.. important::
Things inside this module are not for use outside of RDFLib
and this module is not part the the RDFLib public API.
"""

import sys

__all__ = [
"_NamespaceSetString",
]


if sys.version_info >= (3, 8):
from typing import Literal as PyLiteral
else:
from typing_extensions import Literal as PyLiteral

_NamespaceSetString = PyLiteral["core", "rdflib", "none"]
2 changes: 1 addition & 1 deletion rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
assert Namespace # avoid warning

if TYPE_CHECKING:
from rdflib.namespace import _NamespaceSetString
from rdflib._type_checking import _NamespaceSetString

logger = logging.getLogger(__name__)

Expand Down
7 changes: 1 addition & 6 deletions rdflib/namespace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,7 @@ def _ipython_key_completions_(self) -> List[str]:
XMLNS = Namespace("http://www.w3.org/XML/1998/namespace")

if TYPE_CHECKING:
if sys.version_info >= (3, 8):
from typing import Literal as PyLiteral
else:
from typing_extensions import Literal as PyLiteral

_NamespaceSetString = PyLiteral["core", "rdflib", "none"]
from rdflib._type_checking import _NamespaceSetString


class NamespaceManager(object):
Expand Down
5 changes: 1 addition & 4 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ flake8-black
html5lib
isort
mypy
myst-parser
pytest
pytest-cov
pytest-subtests
sphinx<5
sphinxcontrib-apidoc
sphinxcontrib-kroki
types-setuptools
-r docs/sphinx-requirements.txt
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ exclude = host,extras,transform,rdfs,pyRdfa,sparql,results,pyMicrodata
[coverage:run]
branch = True
source = rdflib
omit =
*/_type_checking.py

[coverage:report]
# Regexes for lines to exclude from consideration
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"sphinx < 5",
"sphinxcontrib-apidoc",
"sphinxcontrib-kroki",
"sphinx-autodoc-typehints",
],
"berkeleydb": ["berkeleydb"],
"networkx": ["networkx"],
Expand Down