Skip to content

Commit e638690

Browse files
committed
feat: Add type hints to rdflib.graph
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 <RDFLib#1850>. This PR does not change runtime behaviour.
1 parent a70a9c8 commit e638690

File tree

13 files changed

+702
-364
lines changed

13 files changed

+702
-364
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ and will be removed for release.
166166
<!-- -->
167167

168168
- Added type hints.
169-
[PR #2057](https://github.com/RDFLib/rdflib/pull/2057).
170169
- `rdflib.store` and builtin stores have mostly complete type hints.
170+
[PR #2057](https://github.com/RDFLib/rdflib/pull/2057).
171+
- `rdflib.graph` have mostly complete type hints.
172+
[PR #2080](https://github.com/RDFLib/rdflib/pull/2080).
171173

172174
<!-- -->
173175
<!-- -->

devtools/diffrtpy.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@
3232
from strip_hints import strip_string_to_string
3333

3434

35-
def clean_python(code: str) -> str:
36-
code = strip_string_to_string(code, to_empty=True, strip_nl=True)
35+
def clean_python(input: Path) -> str:
36+
code = input.read_text()
37+
try:
38+
code = strip_string_to_string(code, to_empty=True, strip_nl=True)
39+
except Exception:
40+
logging.warning(
41+
"failed to strip type hints from %s, falling back to using with type hints",
42+
input,
43+
)
44+
code = code
3745
code = python_minifier.minify(
3846
code,
3947
remove_annotations=True,
@@ -106,12 +114,12 @@ def handle(self, parse_result: argparse.Namespace) -> None:
106114
"base = %s, lhs_file = %s, rhs_file = %s", base, lhs_file, rhs_file
107115
)
108116

109-
lhs_file_content = lhs_file.read_text()
110-
rhs_file_content = rhs_file.read_text()
111-
112117
if lhs_file.name.endswith(".py") and rhs_file.name.endswith(".py"):
113-
lhs_file_content = clean_python(lhs_file_content)
114-
rhs_file_content = clean_python(rhs_file_content)
118+
lhs_file_content = clean_python(lhs_file)
119+
rhs_file_content = clean_python(rhs_file)
120+
else:
121+
lhs_file_content = lhs_file.read_text()
122+
rhs_file_content = rhs_file.read_text()
115123

116124
lhs_file_lines = lhs_file_content.splitlines(keepends=True)
117125
rhs_file_lines = rhs_file_content.splitlines(keepends=True)

docs/conf.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ def find_version(filename):
264264
("py:class", "importlib.metadata.EntryPoint"),
265265
("py:class", "xml.dom.minidom.Document"),
266266
("py:class", "xml.dom.minidom.DocumentFragment"),
267+
("py:class", "isodate.duration.Duration"),
267268
# sphinx-autodoc-typehints has some issues with TypeVars.
268269
# https://github.com/tox-dev/sphinx-autodoc-typehints/issues/39
269270
("py:class", "rdflib.plugin.PluginT"),
@@ -282,13 +283,23 @@ def find_version(filename):
282283
if sys.version_info < (3, 9):
283284
nitpick_ignore.extend(
284285
[
285-
("py:class", "_TriplePatternType"),
286-
("py:class", "_TripleType"),
286+
("py:class", "_ContextIdentifierType"),
287+
("py:class", "_ContextType"),
288+
("py:class", "_GraphT"),
289+
("py:class", "_NamespaceSetString"),
287290
("py:class", "_ObjectType"),
288291
("py:class", "_PredicateType"),
292+
("py:class", "_QuadSelectorType"),
289293
("py:class", "_SubjectType"),
290-
("py:class", "_ContextType"),
291-
("py:class", "_ContextIdentifierType"),
294+
("py:class", "_TripleOrPathTripleType"),
295+
("py:class", "_TripleOrQuadPathPatternType"),
296+
("py:class", "_TripleOrQuadPatternType"),
297+
("py:class", "_TriplePathPatternType"),
298+
("py:class", "_TriplePathType"),
299+
("py:class", "_TriplePatternType"),
300+
("py:class", "_TripleSelectorType"),
301+
("py:class", "_TripleType"),
302+
("py:class", "_TripleOrTriplePathType"),
292303
("py:class", "TextIO"),
293304
]
294305
)

0 commit comments

Comments
 (0)