Skip to content

Commit

Permalink
More typing for rdflib.graph
Browse files Browse the repository at this point in the history
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 #1850
  • Loading branch information
aucampia committed Apr 18, 2022
1 parent 8a92d35 commit 16455d3
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 86 deletions.
28 changes: 18 additions & 10 deletions rdflib/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
if TYPE_CHECKING:
from _hashlib import HASH

from rdflib._typing import _TripleType


def _total_seconds(td):
result = td.days * 24 * 60 * 60
Expand Down Expand Up @@ -281,7 +283,6 @@ def copy(self):
)


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


Expand Down Expand Up @@ -326,7 +327,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 +524,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 +534,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 +544,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 +580,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 +593,10 @@ def to_canonical_graph(g1, stats=None):
return ReadOnlyGraphAggregate([graph])


def graph_diff(g1, g2):
# _GraphT = TypeVar("_GraphT", bound=Graph)


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 +610,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 +623,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

0 comments on commit 16455d3

Please sign in to comment.