Skip to content

Commit 71a1446

Browse files
committed
More type hints for rdflib.graph and related
This patch primarily adds more type hints for `rdflib.graph`, but also adds type hints to some related modules in order to work with the new type hints for `rdflib.graph`. I'm mainly doing this as a baseline for adding type hints to `rdflib.store`. I have created type aliases to make it easier to type everything cosnsitently and to make type hints easier easier to change in the future. The type aliases are private however (i.e. `_`-prefixed) and should be kept as such for now. This patch only contains typing changes and does not change runtime behaviour. Broken off from RDFLib#1850
1 parent e30e386 commit 71a1446

File tree

10 files changed

+142
-85
lines changed

10 files changed

+142
-85
lines changed

rdflib/compare.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
Union,
105105
)
106106

107-
from rdflib.graph import ConjunctiveGraph, Graph, ReadOnlyGraphAggregate
107+
from rdflib.graph import ConjunctiveGraph, Graph, ReadOnlyGraphAggregate, _TripleType
108108
from rdflib.term import BNode, IdentifiedNode, Node, URIRef
109109

110110
if TYPE_CHECKING:
@@ -281,7 +281,6 @@ def copy(self):
281281
)
282282

283283

284-
_TripleT = List[Node]
285284
_HashT = Callable[[], "HASH"]
286285

287286

@@ -326,7 +325,9 @@ def _initial_color(self) -> List[Color]:
326325
self._neighbors[p].add(p)
327326
if len(bnodes) > 0:
328327
return [Color(list(bnodes), self.hashfunc, hash_cache=self._hash_cache)] + [
329-
Color([x], self.hashfunc, x, hash_cache=self._hash_cache)
328+
# type error: List item 0 has incompatible type "Union[IdentifiedNode, Literal]"; expected "IdentifiedNode"
329+
# type error: Argument 3 to "Color" has incompatible type "Union[IdentifiedNode, Literal]"; expected "Tuple[Tuple[Union[int, str], URIRef, Union[int, str]], ...]"
330+
Color([x], self.hashfunc, x, hash_cache=self._hash_cache) # type: ignore[list-item, arg-type]
330331
for x in others
331332
]
332333
else:
@@ -521,7 +522,7 @@ def canonical_triples(self, stats: Optional[Stats] = None):
521522

522523
def _canonicalize_bnodes(
523524
self,
524-
triple: Tuple[IdentifiedNode, IdentifiedNode, Node],
525+
triple: "_TripleType",
525526
labels: Dict[Node, str],
526527
):
527528
for term in triple:
@@ -531,7 +532,7 @@ def _canonicalize_bnodes(
531532
yield term
532533

533534

534-
def to_isomorphic(graph):
535+
def to_isomorphic(graph: Graph) -> IsomorphicGraph:
535536
if isinstance(graph, IsomorphicGraph):
536537
return graph
537538
result = IsomorphicGraph()
@@ -541,7 +542,7 @@ def to_isomorphic(graph):
541542
return result
542543

543544

544-
def isomorphic(graph1, graph2):
545+
def isomorphic(graph1: Graph, graph2: Graph) -> bool:
545546
"""Compare graph for equality.
546547
547548
Uses an algorithm to compute unique hashes which takes bnodes into account.
@@ -577,7 +578,9 @@ def isomorphic(graph1, graph2):
577578
return gd1 == gd2
578579

579580

580-
def to_canonical_graph(g1, stats=None):
581+
def to_canonical_graph(
582+
g1: Graph, stats: Optional[Stats] = None
583+
) -> ReadOnlyGraphAggregate:
581584
"""Creates a canonical, read-only graph.
582585
583586
Creates a canonical, read-only graph where all bnode id:s are based on
@@ -588,7 +591,7 @@ def to_canonical_graph(g1, stats=None):
588591
return ReadOnlyGraphAggregate([graph])
589592

590593

591-
def graph_diff(g1, g2):
594+
def graph_diff(g1: Graph, g2: Graph) -> Tuple[Graph, Graph, Graph]:
592595
"""Returns three sets of triples: "in both", "in first" and "in second"."""
593596
# bnodes have deterministic values in canonical graphs:
594597
cg1 = to_canonical_graph(g1)
@@ -602,7 +605,7 @@ def graph_diff(g1, g2):
602605
_MOCK_BNODE = BNode()
603606

604607

605-
def similar(g1, g2):
608+
def similar(g1: Graph, g2: Graph):
606609
"""Checks if the two graphs are "similar".
607610
608611
Checks if the two graphs are "similar", by comparing sorted triples where
@@ -615,12 +618,12 @@ def similar(g1, g2):
615618
return all(t1 == t2 for (t1, t2) in _squashed_graphs_triples(g1, g2))
616619

617620

618-
def _squashed_graphs_triples(g1, g2):
621+
def _squashed_graphs_triples(g1: Graph, g2: Graph):
619622
for (t1, t2) in zip(sorted(_squash_graph(g1)), sorted(_squash_graph(g2))):
620623
yield t1, t2
621624

622625

623-
def _squash_graph(graph):
626+
def _squash_graph(graph: Graph):
624627
return (_squash_bnodes(triple) for triple in graph)
625628

626629

0 commit comments

Comments
 (0)