Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: narrow the context identifier type from Node to IdentifiedNode #2069

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,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 @@ -39,7 +39,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 @@ -370,9 +370,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