Skip to content

Commit 16455d3

Browse files
committed
More typing for rdflib.graph
This PR adds a bunch of typing for rdflib.graph, mainly as a baseline for adding typing to rdflib.store which I will do next. I have resorted to using type aliases for typing all the functions, in part because it makes it easier to change in the future, however autodoc type hints do not work particularly well with this which is why I will switch to https://github.com/tox-dev/sphinx-autodoc-typehints in a separate patch. This patch only contains typing changes and does not change runtime behaviour. Broken off from RDFLib#1850
1 parent 8a92d35 commit 16455d3

File tree

10 files changed

+150
-86
lines changed

10 files changed

+150
-86
lines changed

rdflib/compare.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
if TYPE_CHECKING:
111111
from _hashlib import HASH
112112

113+
from rdflib._typing import _TripleType
114+
113115

114116
def _total_seconds(td):
115117
result = td.days * 24 * 60 * 60
@@ -281,7 +283,6 @@ def copy(self):
281283
)
282284

283285

284-
_TripleT = List[Node]
285286
_HashT = Callable[[], "HASH"]
286287

287288

@@ -326,7 +327,9 @@ def _initial_color(self) -> List[Color]:
326327
self._neighbors[p].add(p)
327328
if len(bnodes) > 0:
328329
return [Color(list(bnodes), self.hashfunc, hash_cache=self._hash_cache)] + [
329-
Color([x], self.hashfunc, x, hash_cache=self._hash_cache)
330+
# type error: List item 0 has incompatible type "Union[IdentifiedNode, Literal]"; expected "IdentifiedNode"
331+
# type error: Argument 3 to "Color" has incompatible type "Union[IdentifiedNode, Literal]"; expected "Tuple[Tuple[Union[int, str], URIRef, Union[int, str]], ...]"
332+
Color([x], self.hashfunc, x, hash_cache=self._hash_cache) # type: ignore[list-item, arg-type]
330333
for x in others
331334
]
332335
else:
@@ -521,7 +524,7 @@ def canonical_triples(self, stats: Optional[Stats] = None):
521524

522525
def _canonicalize_bnodes(
523526
self,
524-
triple: Tuple[IdentifiedNode, IdentifiedNode, Node],
527+
triple: "_TripleType",
525528
labels: Dict[Node, str],
526529
):
527530
for term in triple:
@@ -531,7 +534,7 @@ def _canonicalize_bnodes(
531534
yield term
532535

533536

534-
def to_isomorphic(graph):
537+
def to_isomorphic(graph: Graph) -> IsomorphicGraph:
535538
if isinstance(graph, IsomorphicGraph):
536539
return graph
537540
result = IsomorphicGraph()
@@ -541,7 +544,7 @@ def to_isomorphic(graph):
541544
return result
542545

543546

544-
def isomorphic(graph1, graph2):
547+
def isomorphic(graph1: Graph, graph2: Graph) -> bool:
545548
"""Compare graph for equality.
546549
547550
Uses an algorithm to compute unique hashes which takes bnodes into account.
@@ -577,7 +580,9 @@ def isomorphic(graph1, graph2):
577580
return gd1 == gd2
578581

579582

580-
def to_canonical_graph(g1, stats=None):
583+
def to_canonical_graph(
584+
g1: Graph, stats: Optional[Stats] = None
585+
) -> ReadOnlyGraphAggregate:
581586
"""Creates a canonical, read-only graph.
582587
583588
Creates a canonical, read-only graph where all bnode id:s are based on
@@ -588,7 +593,10 @@ def to_canonical_graph(g1, stats=None):
588593
return ReadOnlyGraphAggregate([graph])
589594

590595

591-
def graph_diff(g1, g2):
596+
# _GraphT = TypeVar("_GraphT", bound=Graph)
597+
598+
599+
def graph_diff(g1: Graph, g2: Graph) -> Tuple[Graph, Graph, Graph]:
592600
"""Returns three sets of triples: "in both", "in first" and "in second"."""
593601
# bnodes have deterministic values in canonical graphs:
594602
cg1 = to_canonical_graph(g1)
@@ -602,7 +610,7 @@ def graph_diff(g1, g2):
602610
_MOCK_BNODE = BNode()
603611

604612

605-
def similar(g1, g2):
613+
def similar(g1: Graph, g2: Graph):
606614
"""Checks if the two graphs are "similar".
607615
608616
Checks if the two graphs are "similar", by comparing sorted triples where
@@ -615,12 +623,12 @@ def similar(g1, g2):
615623
return all(t1 == t2 for (t1, t2) in _squashed_graphs_triples(g1, g2))
616624

617625

618-
def _squashed_graphs_triples(g1, g2):
626+
def _squashed_graphs_triples(g1: Graph, g2: Graph):
619627
for (t1, t2) in zip(sorted(_squash_graph(g1)), sorted(_squash_graph(g2))):
620628
yield t1, t2
621629

622630

623-
def _squash_graph(graph):
631+
def _squash_graph(graph: Graph):
624632
return (_squash_bnodes(triple) for triple in graph)
625633

626634

0 commit comments

Comments
 (0)