Skip to content

Commit

Permalink
fix: narrow the context identifier type from Node to IdentifiedNode
Browse files Browse the repository at this point in the history
Narrow the context identifier type from `Node` to `IdentifiedNode` as
`Node` is too broad and no supported format (N3, RDF) allows using
anything other than `IdentifiedNode` as context identifiers.

The only change here that has a runtime impact is the change in
`Graph.__init__` to check isinstance against `Node` instead
of `IdentifiedNode`.
  • Loading branch information
aucampia committed Jul 31, 2022
1 parent 8d58fd0 commit 59f4734
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@ and will be removed for release.
<!-- -->
<!-- -->


<!-- -->
<!-- -->
<!-- CHANGE BARRIER: START PR #2069 -->
<!-- -->
<!-- -->

- Narrow the type of context-identifiers/graph-names from `rdflib.term.Node` to
`rdflib.term.IdentifiedNode` as no supported abstract syntax allows for other
types of context-identifiers.
[PR #2069](https://github.com/RDFLib/rdflib/pull/2069).

<!-- -->
<!-- -->
<!-- CHANGE BARRIER: END PR #2069 -->
<!-- -->
<!-- -->

<!-- -->
<!-- -->
<!-- CHANGE BARRIER: START -->
Expand Down
6 changes: 3 additions & 3 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
_SubjectType = Node
_PredicateType = Node
_ObjectType = Node
_ContextIdentifierType = Node
_ContextIdentifierType = IdentifiedNode

_TripleType = Tuple["_SubjectType", "_PredicateType", "_ObjectType"]
_QuadType = Tuple["_SubjectType", "_PredicateType", "_ObjectType", "_ContextType"]
Expand Down Expand Up @@ -373,9 +373,9 @@ def __init__(
):
super(Graph, self).__init__()
self.base = base
self.__identifier: Node
self.__identifier: _ContextIdentifierType
self.__identifier = identifier or BNode() # type: ignore[assignment]
if not isinstance(self.__identifier, Node):
if not isinstance(self.__identifier, IdentifiedNode):
self.__identifier = URIRef(self.__identifier) # type: ignore[unreachable]
self.__store: Store
if not isinstance(store, Store):
Expand Down
15 changes: 5 additions & 10 deletions rdflib/plugins/stores/auditable.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ def add(
lock = destructiveOpLocks["add"]
lock = lock if lock else threading.RLock()
with lock:
# type error: Argument 2 to "Graph" has incompatible type "Node"; expected "Union[IdentifiedNode, str, None]"
context = (
context.__class__(self.store, context.identifier) # type: ignore[arg-type]
context.__class__(self.store, context.identifier)
if context is not None
else None
)
Expand All @@ -104,9 +103,8 @@ def remove(
with lock:
# Need to determine which quads will be removed if any term is a
# wildcard
# type error: error: Argument 2 to "Graph" has incompatible type "Node"; expected "Union[IdentifiedNode, str, None]"
context = (
context.__class__(self.store, context.identifier) # type: ignore[arg-type]
context.__class__(self.store, context.identifier)
if context is not None
else None
)
Expand Down Expand Up @@ -144,19 +142,17 @@ def triples(
self, triple: "_TriplePatternType", context: Optional["_ContextType"] = None
) -> Iterator[Tuple["_TripleType", Iterator[Optional["_ContextType"]]]]:
(su, pr, ob) = triple
# type error: Argument 2 to "Graph" has incompatible type "Node"; expected "Union[IdentifiedNode, str, None]"
context = (
context.__class__(self.store, context.identifier) # type: ignore[arg-type]
context.__class__(self.store, context.identifier)
if context is not None
else None
)
for (s, p, o), cg in self.store.triples((su, pr, ob), context):
yield (s, p, o), cg

def __len__(self, context: Optional["_ContextType"] = None):
# type error: Argument 2 to "Graph" has incompatible type "Node"; expected "Union[IdentifiedNode, str, None]"
context = (
context.__class__(self.store, context.identifier) # type: ignore[arg-type]
context.__class__(self.store, context.identifier)
if context is not None
else None
)
Expand Down Expand Up @@ -194,9 +190,8 @@ def rollback(self) -> None:
(subject, predicate, obj), Graph(self.store, context) # type: ignore[arg-type]
)
else:
# type error: Argument 2 to "Graph" has incompatible type "Optional[Node]"; expected "Union[IdentifiedNode, str, None]"
self.store.remove(
(subject, predicate, obj), Graph(self.store, context) # type: ignore[arg-type]
(subject, predicate, obj), Graph(self.store, context)
)

self.reverseOps = []
3 changes: 1 addition & 2 deletions test/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ def quad_set(
gn_id = gn # type: ignore[unreachable]
elif isinstance(graph, ConjunctiveGraph):
assert isinstance(gn, Graph)
# type error: Incompatible types in assignment (expression has type "Node", variable has type "Identifier")
gn_id = gn.identifier # type: ignore[assignment]
gn_id = gn.identifier
else:
raise ValueError(f"invalid graph type {type(graph)}: {graph!r}")
s, p, o = cls.nodes((sn, pn, on), bnode_handling)
Expand Down

0 comments on commit 59f4734

Please sign in to comment.