Skip to content

Commit

Permalink
feat: Add type hints to rdflib.graph
Browse files Browse the repository at this point in the history
More or less complete type hints for the rdflib.graph module.

Other changes:
- Improved/simplified type hints in `rdflib.store` and store plugins.
- Add type ignores for various type errors that occur with the type
  hints.

This is split-off from <#1850>.

This PR does not change runtime behaviour.
  • Loading branch information
aucampia committed Aug 14, 2022
1 parent a39d143 commit 97f88d3
Show file tree
Hide file tree
Showing 13 changed files with 699 additions and 364 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ and will be removed for release.
<!-- -->

- Added type hints.
[PR #2057](https://github.com/RDFLib/rdflib/pull/2057).
- `rdflib.store` and builtin stores have mostly complete type hints.
[PR #2057](https://github.com/RDFLib/rdflib/pull/2057).
- `rdflib.graph` have mostly complete type hints.
[PR #2080](https://github.com/RDFLib/rdflib/pull/2080).

<!-- -->
<!-- -->
Expand Down
19 changes: 12 additions & 7 deletions devtools/diffrtpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@
from strip_hints import strip_string_to_string


def clean_python(code: str) -> str:
code = strip_string_to_string(code, to_empty=True, strip_nl=True)
def clean_python(input: Path) -> str:
code = input.read_text()
try:
code = strip_string_to_string(code, to_empty=True, strip_nl=True)
except Exception:
logging.warning(
"failed to strip type hints from %s, falling back to using with type hints",
input,
)
code = code
code = python_minifier.minify(
code,
remove_annotations=True,
Expand Down Expand Up @@ -106,12 +114,9 @@ def handle(self, parse_result: argparse.Namespace) -> None:
"base = %s, lhs_file = %s, rhs_file = %s", base, lhs_file, rhs_file
)

lhs_file_content = lhs_file.read_text()
rhs_file_content = rhs_file.read_text()

if lhs_file.name.endswith(".py") and rhs_file.name.endswith(".py"):
lhs_file_content = clean_python(lhs_file_content)
rhs_file_content = clean_python(rhs_file_content)
lhs_file_content = clean_python(lhs_file)
rhs_file_content = clean_python(rhs_file)

lhs_file_lines = lhs_file_content.splitlines(keepends=True)
rhs_file_lines = rhs_file_content.splitlines(keepends=True)
Expand Down
19 changes: 15 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ def find_version(filename):
("py:class", "importlib.metadata.EntryPoint"),
("py:class", "xml.dom.minidom.Document"),
("py:class", "xml.dom.minidom.DocumentFragment"),
("py:class", "isodate.duration.Duration"),
# sphinx-autodoc-typehints has some issues with TypeVars.
# https://github.com/tox-dev/sphinx-autodoc-typehints/issues/39
("py:class", "rdflib.plugin.PluginT"),
Expand All @@ -282,13 +283,23 @@ def find_version(filename):
if sys.version_info < (3, 9):
nitpick_ignore.extend(
[
("py:class", "_TriplePatternType"),
("py:class", "_TripleType"),
("py:class", "_ContextIdentifierType"),
("py:class", "_ContextType"),
("py:class", "_GraphT"),
("py:class", "_NamespaceSetString"),
("py:class", "_ObjectType"),
("py:class", "_PredicateType"),
("py:class", "_QuadSelectorType"),
("py:class", "_SubjectType"),
("py:class", "_ContextType"),
("py:class", "_ContextIdentifierType"),
("py:class", "_TripleOrPathTripleType"),
("py:class", "_TripleOrQuadPathPatternType"),
("py:class", "_TripleOrQuadPatternType"),
("py:class", "_TriplePathPatternType"),
("py:class", "_TriplePathType"),
("py:class", "_TriplePatternType"),
("py:class", "_TripleSelectorType"),
("py:class", "_TripleType"),
("py:class", "_TripleOrTriplePathType"),
("py:class", "TextIO"),
]
)
Expand Down

0 comments on commit 97f88d3

Please sign in to comment.