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

Draft: Dataset add named graph #2592

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,14 @@ def store(self) -> Store:
def identifier(self) -> _ContextIdentifierType:
return self.__identifier

@identifier.setter
def identifier(self, identifier: IdentifiedNode):
if not isinstance(identifier, IdentifiedNode):
raise TypeError(
"The identifier for a Graph must be an IdentifiedNode"
)
self.__identifier = identifier

@property
def namespace_manager(self) -> NamespaceManager:
"""
Expand Down Expand Up @@ -2535,6 +2543,13 @@ def add_graph(
"""alias of graph for consistency"""
return self.graph(g)

def add_named_graph(
self, named_graph_identifier: IdentifiedNode, g: Optional[Union[_ContextIdentifierType, _ContextType, str]]
Copy link
Member

@aucampia aucampia Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self, named_graph_identifier: IdentifiedNode, g: Optional[Union[_ContextIdentifierType, _ContextType, str]]
self, identifier: IdentifiedNode, graph: Optional[Union[_ContextIdentifierType, _ContextType, str]]

I'm not sure why g/graph is not of type Graph though, which it ideally should be. It could also maybe be worth it to take Iterable[_TripleType] as type.

) -> Graph:
new_g = self.graph(g)
new_g.identifier = named_graph_identifier
return new_g

def remove_graph(
self: _DatasetT, g: Optional[Union[_ContextIdentifierType, _ContextType, str]]
) -> _DatasetT:
Expand Down
11 changes: 11 additions & 0 deletions test/test_graph/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ def test_property_identifier() -> None:
assert id == graph.identifier


def test_property_setter_identifier() -> None:
"""
The ``identifier`` property setter works correctly.
"""
id = URIRef("example:a")
new_id = URIRef("example:b")
graph = Graph(identifier=id)
assert id == graph.identifier
graph.identifier = new_id
assert new_id == graph.identifier

def test_property_namespace_manager() -> None:
"""
The ``namespace_manager`` property works correctly.
Expand Down
Loading