Skip to content
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
25 changes: 14 additions & 11 deletions rdflib/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
Union,
)

from rdflib.graph import ConjunctiveGraph, Graph, ReadOnlyGraphAggregate
from rdflib.graph import ConjunctiveGraph, Graph, ReadOnlyGraphAggregate, _TripleType
from rdflib.term import BNode, IdentifiedNode, Node, URIRef

if TYPE_CHECKING:
Expand Down Expand Up @@ -281,7 +281,6 @@ def copy(self):
)


_TripleT = List[Node]
_HashT = Callable[[], "HASH"]


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

def _canonicalize_bnodes(
self,
triple: Tuple[IdentifiedNode, IdentifiedNode, Node],
triple: "_TripleType",
labels: Dict[Node, str],
):
for term in triple:
Expand All @@ -531,7 +532,7 @@ def _canonicalize_bnodes(
yield term


def to_isomorphic(graph):
def to_isomorphic(graph: Graph) -> IsomorphicGraph:
if isinstance(graph, IsomorphicGraph):
return graph
result = IsomorphicGraph()
Expand All @@ -541,7 +542,7 @@ def to_isomorphic(graph):
return result


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


def to_canonical_graph(g1, stats=None):
def to_canonical_graph(
g1: Graph, stats: Optional[Stats] = None
) -> ReadOnlyGraphAggregate:
"""Creates a canonical, read-only graph.
Creates a canonical, read-only graph where all bnode id:s are based on
Expand All @@ -588,7 +591,7 @@ def to_canonical_graph(g1, stats=None):
return ReadOnlyGraphAggregate([graph])


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


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


def _squashed_graphs_triples(g1, g2):
def _squashed_graphs_triples(g1: Graph, g2: Graph):
for (t1, t2) in zip(sorted(_squash_graph(g1)), sorted(_squash_graph(g2))):
yield t1, t2


def _squash_graph(graph):
def _squash_graph(graph: Graph):
return (_squash_bnodes(triple) for triple in graph)


Expand Down
Loading