diff --git a/rdflib/graph.py b/rdflib/graph.py index 80ccc3fa8..b2b9d410c 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -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: """ @@ -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]] + ) -> 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: diff --git a/test/test_graph/test_graph.py b/test/test_graph/test_graph.py index 639aa710c..ebaa1d759 100644 --- a/test/test_graph/test_graph.py +++ b/test/test_graph/test_graph.py @@ -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.