diff options
Diffstat (limited to 'rdflib/extras/external_graph_libs.py')
-rw-r--r-- | rdflib/extras/external_graph_libs.py | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/rdflib/extras/external_graph_libs.py b/rdflib/extras/external_graph_libs.py index 11d2ca1e..f50994b5 100644 --- a/rdflib/extras/external_graph_libs.py +++ b/rdflib/extras/external_graph_libs.py @@ -1,5 +1,6 @@ #!/usr/bin/env python # encoding: utf-8 +from __future__ import annotations """Convert (to and) from rdflib graphs to other well known graph libraries. @@ -13,6 +14,10 @@ see ../../test/test_extras_external_graph_libs.py for conditional tests """ import logging +from typing import TYPE_CHECKING, Any, Dict, List + +if TYPE_CHECKING: + from rdflib.graph import Graph logger = logging.getLogger(__name__) @@ -22,9 +27,9 @@ def _identity(x): def _rdflib_to_networkx_graph( - graph, + graph: Graph, nxgraph, - calc_weights, + calc_weights: bool, edge_attrs, transform_s=_identity, transform_o=_identity, @@ -70,7 +75,7 @@ def _rdflib_to_networkx_graph( def rdflib_to_networkx_multidigraph( - graph, edge_attrs=lambda s, p, o: {"key": p}, **kwds + graph: Graph, edge_attrs=lambda s, p, o: {"key": p}, **kwds ): """Converts the given graph into a networkx.MultiDiGraph. @@ -124,8 +129,8 @@ def rdflib_to_networkx_multidigraph( def rdflib_to_networkx_digraph( - graph, - calc_weights=True, + graph: Graph, + calc_weights: bool = True, edge_attrs=lambda s, p, o: {"triples": [(s, p, o)]}, **kwds, ): @@ -187,8 +192,8 @@ def rdflib_to_networkx_digraph( def rdflib_to_networkx_graph( - graph, - calc_weights=True, + graph: Graph, + calc_weights: bool = True, edge_attrs=lambda s, p, o: {"triples": [(s, p, o)]}, **kwds, ): @@ -250,9 +255,9 @@ def rdflib_to_networkx_graph( def rdflib_to_graphtool( - graph, - v_prop_names=[str("term")], - e_prop_names=[str("term")], + graph: Graph, + v_prop_names: List[str] = [str("term")], + e_prop_names: List[str] = [str("term")], transform_s=lambda s, p, o: {str("term"): s}, transform_p=lambda s, p, o: {str("term"): p}, transform_o=lambda s, p, o: {str("term"): o}, @@ -313,7 +318,8 @@ def rdflib_to_graphtool( True """ - import graph_tool as gt + # pytype error: Can't find module 'graph_tool'. + import graph_tool as gt # pytype: disable=import-error g = gt.Graph() @@ -323,7 +329,7 @@ def rdflib_to_graphtool( eprops = [(epn, g.new_edge_property("object")) for epn in e_prop_names] for epn, eprop in eprops: g.edge_properties[epn] = eprop - node_to_vertex = {} + node_to_vertex: Dict[Any, Any] = {} for s, p, o in graph: sv = node_to_vertex.get(s) if sv is None: |