summaryrefslogtreecommitdiff
path: root/rdflib/util.py
diff options
context:
space:
mode:
authorIwan Aucamp <aucampia@gmail.com>2022-05-26 13:55:31 +0200
committerGitHub <noreply@github.com>2022-05-26 13:55:31 +0200
commit21e737dc25baa2b11a83355420348250607bb8c7 (patch)
tree2a73e78568c13be72ac9c955112d6e529f748787 /rdflib/util.py
parent958b9a11129f79523d76524802629f34ec649198 (diff)
downloadrdflib-21e737dc25baa2b11a83355420348250607bb8c7.tar.gz
More type hints for `rdflib.graph` and related (#1853)
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 consistently 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 behavior. Broken off from https://github.com/RDFLib/rdflib/pull/1850
Diffstat (limited to 'rdflib/util.py')
-rw-r--r--rdflib/util.py17
1 files changed, 8 insertions, 9 deletions
diff --git a/rdflib/util.py b/rdflib/util.py
index 328a8743..c3d04065 100644
--- a/rdflib/util.py
+++ b/rdflib/util.py
@@ -29,7 +29,7 @@ from typing import (
TYPE_CHECKING,
Any,
Callable,
- Iterable,
+ Iterator,
List,
Optional,
Set,
@@ -41,7 +41,7 @@ from urllib.parse import quote, urlsplit, urlunsplit
import rdflib.graph # avoid circular dependency
from rdflib.compat import sign
from rdflib.namespace import XSD, Namespace, NamespaceManager
-from rdflib.term import BNode, IdentifiedNode, Literal, Node, URIRef
+from rdflib.term import BNode, Literal, Node, URIRef
if TYPE_CHECKING:
from rdflib.graph import Graph
@@ -409,13 +409,13 @@ def find_roots(
def get_tree(
graph: "Graph",
- root: "IdentifiedNode",
+ root: "Node",
prop: "URIRef",
- mapper: Callable[["IdentifiedNode"], "IdentifiedNode"] = lambda x: x,
+ mapper: Callable[["Node"], "Node"] = lambda x: x,
sortkey: Optional[Callable[[Any], Any]] = None,
- done: Optional[Set["IdentifiedNode"]] = None,
+ done: Optional[Set["Node"]] = None,
dir: str = "down",
-) -> Optional[Tuple[IdentifiedNode, List[Any]]]:
+) -> Optional[Tuple[Node, List[Any]]]:
"""
Return a nested list/tuple structure representing the tree
built by the transitive property given, starting from the root given
@@ -442,12 +442,11 @@ def get_tree(
done.add(root)
tree = []
- branches: Iterable[IdentifiedNode]
+ branches: Iterator[Node]
if dir == "down":
branches = graph.subjects(prop, root)
else:
- # type error: Incompatible types in assignment (expression has type "Iterable[Node]", variable has type "Iterable[IdentifiedNode]")
- branches = graph.objects(root, prop) # type: ignore[assignment]
+ branches = graph.objects(root, prop)
for branch in branches:
t = get_tree(graph, branch, prop, mapper, sortkey, done, dir)