Repository: RDFLib/rdflib

Issue:
N3 method for path objects is bugged
The n3 method for path objects returns invalid/unexpected paths for non-trivial paths.

Example:
```
import rdflib
import rdflib.paths

path = rdflib.RDF.type / rdflib.paths.AlternativePath(rdflib.SKOS.note, rdflib.RDFS.comment)
```
Calling `path.n3()` here returns `<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>/<http://www.w3.org/2004/02/skos/core#note>|<http://www.w3.org/2000/01/rdf-schema#comment>` (essentially `rdf:type/skos:note|rdfs:comment`).
However, it the method should rather return `<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>/(<http://www.w3.org/2004/02/skos/core#note>|<http://www.w3.org/2000/01/rdf-schema#comment>)` (thus `rdf:type/(skos:note|rdfs:comment)`).

The n3 method ignores all groups as it never introduces any brackets.

Related to https://github.com/RDFLib/rdflib/issues/421

Comments:

Patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 5f0c147c..e0f9d821 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -18,7 +18,7 @@ repos:
   - repo: https://github.com/psf/black
     # WARNING: version should be the same as in `pyproject.toml`
     # Using git ref spec because of https://github.com/psf/black/issues/2493
-    rev: 'refs/tags/23.3.0:refs/tags/23.3.0'
+    rev: 'refs/tags/23.7.0:refs/tags/23.7.0'
     hooks:
     - id: black
       pass_filenames: false
diff --git a/CITATION.cff b/CITATION.cff
new file mode 100644
index 00000000..a9046a1c
--- /dev/null
+++ b/CITATION.cff
@@ -0,0 +1,75 @@
+cff-version: 1.2.0
+message: "If you use this software, please cite it as below."
+authors:
+- family-names: "Krech"
+  given-names: "Daniel"
+- family-names: "Grimnes"
+  given-names: "Gunnar AAstrand"
+- family-names: "Higgins"
+  given-names: "Graham"
+- family-names: "Hees"
+  given-names: "Jörn"
+  orcid: "https://orcid.org/0000-0002-0084-8998"
+- family-names: "Aucamp"
+  given-names: "Iwan"
+  orcid: "https://orcid.org/0000-0002-7325-3231"
+- family-names: "Lindström"
+  given-names: "Niklas"
+- family-names: "Arndt"
+  given-names: "Natanael"
+  orcid: "https://orcid.org/0000-0002-8130-8677"
+- family-names: "Sommer"
+  given-names: "Ashley"
+  orcid: "https://orcid.org/0000-0003-0590-0131"
+- family-names: "Chuc"
+  given-names: "Edmond"
+  orcid: "https://orcid.org/0000-0002-6047-9864"
+- family-names: "Herman"
+  given-names: "Ivan"
+  orcid: "https://orcid.org/0000-0003-0782-2704"
+- family-names: "Nelson"
+  given-names: "Alex"
+- family-names: "McCusker"
+  given-names: "Jamie"
+  orcid: "https://orcid.org/0000-0003-1085-6059"
+- family-names: "Gillespie"
+  given-names: "Tom"
+- family-names: "Kluyver"
+  given-names: "Thomas"
+  orcid: "https://orcid.org/0000-0003-4020-6364"
+- family-names: "Ludwig"
+  given-names: "Florian"
+- family-names: "Champin"
+  given-names: "Pierre-Antoine"
+  orcid: "https://orcid.org/0000-0001-7046-4474"
+- family-names: "Watts"
+  given-names: "Mark"
+- family-names: "Holzer"
+  given-names: "Urs"
+- family-names: "Summers"
+  given-names: "Ed"
+- family-names: "Morriss"
+  given-names: "Whit"
+- family-names: "Winston"
+  given-names: "Donny"
+- family-names: "Perttula"
+  given-names: "Drew"
+- family-names: "Kovacevic"
+  given-names: "Filip"
+  orcid: "https://orcid.org/0000-0002-2854-0434"
+- family-names: "Chateauneu"
+  given-names: "Remi"
+  orcid: "https://orcid.org/0000-0002-7505-8149"
+- family-names: "Solbrig"
+  given-names: "Harold"
+  orcid: "https://orcid.org/0000-0002-5928-3071"
+- family-names: "Cogrel"
+  given-names: "Benjamin"
+  orcid: "https://orcid.org/0000-0002-7566-4077"
+- family-names: "Stuart"
+  given-names: "Veyndan"
+title: "RDFLib"
+version: 6.3.2
+date-released: 2023-03-26
+url: "https://github.com/RDFLib/rdflib"
+doi: 10.5281/zenodo.6845245
diff --git a/docker/unstable/Dockerfile b/docker/unstable/Dockerfile
index 183c1190..7a2671db 100644
--- a/docker/unstable/Dockerfile
+++ b/docker/unstable/Dockerfile
@@ -1,4 +1,4 @@
-FROM docker.io/library/python:3.11.4-slim@sha256:364ee1a9e029fb7b60102ae56ff52153ccc929ceab9aa387402fe738432d24cc
+FROM docker.io/library/python:3.11.4-slim@sha256:36b544be6e796eb5caa0bf1ab75a17d2e20211cad7f66f04f6f5c9eeda930ef5
 
 # This file is generated from docker:unstable in Taskfile.yml
 COPY var/requirements.txt /var/tmp/build/
diff --git a/poetry.lock b/poetry.lock
index 0d4d5250..ebc588c5 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -37,36 +37,33 @@ files = [
 
 [[package]]
 name = "black"
-version = "23.3.0"
+version = "23.7.0"
 description = "The uncompromising code formatter."
 optional = false
-python-versions = ">=3.7"
+python-versions = ">=3.8"
 files = [
-    {file = "black-23.3.0-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:0945e13506be58bf7db93ee5853243eb368ace1c08a24c65ce108986eac65915"},
-    {file = "black-23.3.0-cp310-cp310-macosx_10_16_universal2.whl", hash = "sha256:67de8d0c209eb5b330cce2469503de11bca4085880d62f1628bd9972cc3366b9"},
-    {file = "black-23.3.0-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:7c3eb7cea23904399866c55826b31c1f55bbcd3890ce22ff70466b907b6775c2"},
-    {file = "black-23.3.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:32daa9783106c28815d05b724238e30718f34155653d4d6e125dc7daec8e260c"},
-    {file = "black-23.3.0-cp310-cp310-win_amd64.whl", hash = "sha256:35d1381d7a22cc5b2be2f72c7dfdae4072a3336060635718cc7e1ede24221d6c"},
-    {file = "black-23.3.0-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:a8a968125d0a6a404842fa1bf0b349a568634f856aa08ffaff40ae0dfa52e7c6"},
-    {file = "black-23.3.0-cp311-cp311-macosx_10_16_universal2.whl", hash = "sha256:c7ab5790333c448903c4b721b59c0d80b11fe5e9803d8703e84dcb8da56fec1b"},
-    {file = "black-23.3.0-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:a6f6886c9869d4daae2d1715ce34a19bbc4b95006d20ed785ca00fa03cba312d"},
-    {file = "black-23.3.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6f3c333ea1dd6771b2d3777482429864f8e258899f6ff05826c3a4fcc5ce3f70"},
-    {file = "black-23.3.0-cp311-cp311-win_amd64.whl", hash = "sha256:11c410f71b876f961d1de77b9699ad19f939094c3a677323f43d7a29855fe326"},
-    {file = "black-23.3.0-cp37-cp37m-macosx_10_16_x86_64.whl", hash = "sha256:1d06691f1eb8de91cd1b322f21e3bfc9efe0c7ca1f0e1eb1db44ea367dff656b"},
-    {file = "black-23.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:50cb33cac881766a5cd9913e10ff75b1e8eb71babf4c7104f2e9c52da1fb7de2"},
-    {file = "black-23.3.0-cp37-cp37m-win_amd64.whl", hash = "sha256:e114420bf26b90d4b9daa597351337762b63039752bdf72bf361364c1aa05925"},
-    {file = "black-23.3.0-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:48f9d345675bb7fbc3dd85821b12487e1b9a75242028adad0333ce36ed2a6d27"},
-    {file = "black-23.3.0-cp38-cp38-macosx_10_16_universal2.whl", hash = "sha256:714290490c18fb0126baa0fca0a54ee795f7502b44177e1ce7624ba1c00f2331"},
-    {file = "black-23.3.0-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:064101748afa12ad2291c2b91c960be28b817c0c7eaa35bec09cc63aa56493c5"},
-    {file = "black-23.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:562bd3a70495facf56814293149e51aa1be9931567474993c7942ff7d3533961"},
-    {file = "black-23.3.0-cp38-cp38-win_amd64.whl", hash = "sha256:e198cf27888ad6f4ff331ca1c48ffc038848ea9f031a3b40ba36aced7e22f2c8"},
-    {file = "black-23.3.0-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:3238f2aacf827d18d26db07524e44741233ae09a584273aa059066d644ca7b30"},
-    {file = "black-23.3.0-cp39-cp39-macosx_10_16_universal2.whl", hash = "sha256:f0bd2f4a58d6666500542b26354978218a9babcdc972722f4bf90779524515f3"},
-    {file = "black-23.3.0-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:92c543f6854c28a3c7f39f4d9b7694f9a6eb9d3c5e2ece488c327b6e7ea9b266"},
-    {file = "black-23.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3a150542a204124ed00683f0db1f5cf1c2aaaa9cc3495b7a3b5976fb136090ab"},
-    {file = "black-23.3.0-cp39-cp39-win_amd64.whl", hash = "sha256:6b39abdfb402002b8a7d030ccc85cf5afff64ee90fa4c5aebc531e3ad0175ddb"},
-    {file = "black-23.3.0-py3-none-any.whl", hash = "sha256:ec751418022185b0c1bb7d7736e6933d40bbb14c14a0abcf9123d1b159f98dd4"},
-    {file = "black-23.3.0.tar.gz", hash = "sha256:1c7b8d606e728a41ea1ccbd7264677e494e87cf630e399262ced92d4a8dac940"},
+    {file = "black-23.7.0-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:5c4bc552ab52f6c1c506ccae05681fab58c3f72d59ae6e6639e8885e94fe2587"},
+    {file = "black-23.7.0-cp310-cp310-macosx_10_16_universal2.whl", hash = "sha256:552513d5cd5694590d7ef6f46e1767a4df9af168d449ff767b13b084c020e63f"},
+    {file = "black-23.7.0-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:86cee259349b4448adb4ef9b204bb4467aae74a386bce85d56ba4f5dc0da27be"},
+    {file = "black-23.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:501387a9edcb75d7ae8a4412bb8749900386eaef258f1aefab18adddea1936bc"},
+    {file = "black-23.7.0-cp310-cp310-win_amd64.whl", hash = "sha256:fb074d8b213749fa1d077d630db0d5f8cc3b2ae63587ad4116e8a436e9bbe995"},
+    {file = "black-23.7.0-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:b5b0ee6d96b345a8b420100b7d71ebfdd19fab5e8301aff48ec270042cd40ac2"},
+    {file = "black-23.7.0-cp311-cp311-macosx_10_16_universal2.whl", hash = "sha256:893695a76b140881531062d48476ebe4a48f5d1e9388177e175d76234ca247cd"},
+    {file = "black-23.7.0-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:c333286dc3ddca6fdff74670b911cccedacb4ef0a60b34e491b8a67c833b343a"},
+    {file = "black-23.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:831d8f54c3a8c8cf55f64d0422ee875eecac26f5f649fb6c1df65316b67c8926"},
+    {file = "black-23.7.0-cp311-cp311-win_amd64.whl", hash = "sha256:7f3bf2dec7d541b4619b8ce526bda74a6b0bffc480a163fed32eb8b3c9aed8ad"},
+    {file = "black-23.7.0-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:f9062af71c59c004cd519e2fb8f5d25d39e46d3af011b41ab43b9c74e27e236f"},
+    {file = "black-23.7.0-cp38-cp38-macosx_10_16_universal2.whl", hash = "sha256:01ede61aac8c154b55f35301fac3e730baf0c9cf8120f65a9cd61a81cfb4a0c3"},
+    {file = "black-23.7.0-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:327a8c2550ddc573b51e2c352adb88143464bb9d92c10416feb86b0f5aee5ff6"},
+    {file = "black-23.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6d1c6022b86f83b632d06f2b02774134def5d4d4f1dac8bef16d90cda18ba28a"},
+    {file = "black-23.7.0-cp38-cp38-win_amd64.whl", hash = "sha256:27eb7a0c71604d5de083757fbdb245b1a4fae60e9596514c6ec497eb63f95320"},
+    {file = "black-23.7.0-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:8417dbd2f57b5701492cd46edcecc4f9208dc75529bcf76c514864e48da867d9"},
+    {file = "black-23.7.0-cp39-cp39-macosx_10_16_universal2.whl", hash = "sha256:47e56d83aad53ca140da0af87678fb38e44fd6bc0af71eebab2d1f59b1acf1d3"},
+    {file = "black-23.7.0-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:25cc308838fe71f7065df53aedd20327969d05671bac95b38fdf37ebe70ac087"},
+    {file = "black-23.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:642496b675095d423f9b8448243336f8ec71c9d4d57ec17bf795b67f08132a91"},
+    {file = "black-23.7.0-cp39-cp39-win_amd64.whl", hash = "sha256:ad0014efc7acf0bd745792bd0d8857413652979200ab924fbf239062adc12491"},
+    {file = "black-23.7.0-py3-none-any.whl", hash = "sha256:9fd59d418c60c0348505f2ddf9609c1e1de8e7493eab96198fc89d9f865e7a96"},
+    {file = "black-23.7.0.tar.gz", hash = "sha256:022a582720b0d9480ed82576c920a8c1dde97cc38ff11d8d8859b3bd6ca9eedb"},
 ]
 
 [package.dependencies]
@@ -1143,22 +1140,22 @@ test = ["cython", "filelock", "html5lib", "pytest (>=4.6)"]
 
 [[package]]
 name = "sphinx-autodoc-typehints"
-version = "1.23.0"
+version = "1.24.0"
 description = "Type hints (PEP 484) support for the Sphinx autodoc extension"
 optional = false
-python-versions = ">=3.7"
+python-versions = ">=3.8"
 files = [
-    {file = "sphinx_autodoc_typehints-1.23.0-py3-none-any.whl", hash = "sha256:ac099057e66b09e51b698058ba7dd76e57e1fe696cd91b54e121d3dad188f91d"},
-    {file = "sphinx_autodoc_typehints-1.23.0.tar.gz", hash = "sha256:5d44e2996633cdada499b6d27a496ddf9dbc95dd1f0f09f7b37940249e61f6e9"},
+    {file = "sphinx_autodoc_typehints-1.24.0-py3-none-any.whl", hash = "sha256:6a73c0c61a9144ce2ed5ef2bed99d615254e5005c1cc32002017d72d69fb70e6"},
+    {file = "sphinx_autodoc_typehints-1.24.0.tar.gz", hash = "sha256:94e440066941bb237704bb880785e2d05e8ae5406c88674feefbb938ad0dc6af"},
 ]
 
 [package.dependencies]
-sphinx = ">=5.3"
+sphinx = ">=7.0.1"
 
 [package.extras]
-docs = ["furo (>=2022.12.7)", "sphinx (>=6.1.3)", "sphinx-autodoc-typehints (>=1.23.4)"]
-testing = ["covdefaults (>=2.2.2)", "coverage (>=7.2.2)", "diff-cover (>=7.5)", "nptyping (>=2.5)", "pytest (>=7.2.2)", "pytest-cov (>=4)", "sphobjinv (>=2.3.1)", "typing-extensions (>=4.5)"]
-type-comment = ["typed-ast (>=1.5.4)"]
+docs = ["furo (>=2023.5.20)", "sphinx (>=7.0.1)"]
+numpy = ["nptyping (>=2.5)"]
+testing = ["covdefaults (>=2.3)", "coverage (>=7.2.7)", "diff-cover (>=7.5)", "pytest (>=7.3.1)", "pytest-cov (>=4.1)", "sphobjinv (>=2.3.1)", "typing-extensions (>=4.6.3)"]
 
 [[package]]
 name = "sphinxcontrib-apidoc"
@@ -1288,13 +1285,13 @@ files = [
 
 [[package]]
 name = "types-setuptools"
-version = "68.0.0.2"
+version = "68.0.0.3"
 description = "Typing stubs for setuptools"
 optional = false
 python-versions = "*"
 files = [
-    {file = "types-setuptools-68.0.0.2.tar.gz", hash = "sha256:fede8b46862dd9fe68a12f11a8444c3d240d11178eba7d584d6f22ca3114b894"},
-    {file = "types_setuptools-68.0.0.2-py3-none-any.whl", hash = "sha256:311a14819416716029d1113c7452143e2fa857e6cc19186bb6830aff69379c48"},
+    {file = "types-setuptools-68.0.0.3.tar.gz", hash = "sha256:d57ae6076100b5704b3cc869fdefc671e1baf4c2cd6643f84265dfc0b955bf05"},
+    {file = "types_setuptools-68.0.0.3-py3-none-any.whl", hash = "sha256:fec09e5c18264c5c09351c00be01a34456fb7a88e457abe97401325f84ad9d36"},
 ]
 
 [[package]]
@@ -1359,4 +1356,4 @@ networkx = ["networkx"]
 [metadata]
 lock-version = "2.0"
 python-versions = "^3.8.1"
-content-hash = "2976433b559075bf2b607420b7e80e07b248d630aad915ef0a1ba39bf13e4c8e"
+content-hash = "99e9e88546065df26605b1240b641588740efe954ade7c7b82c20e0b4f38686d"
diff --git a/pyproject.toml b/pyproject.toml
index 29fee25b..dae4e35f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -47,7 +47,7 @@ html5lib = {version = "^1.0", optional = true}
 lxml = {version = "^4.3.0", optional = true}
 
 [tool.poetry.group.dev.dependencies]
-black = "23.3.0"
+black = "23.7.0"
 isort = "^5.10.0"
 mypy = "^1.1.0"
 lxml-stubs = "^0.4.0"
@@ -120,7 +120,7 @@ pyflakes = ["-F821"]
 
 
 [tool.black]
-required-version = "23.3.0"
+required-version = "23.7.0"
 line-length = "88"
 target-version = ['py37']
 include = '\.pyi?$'
diff --git a/rdflib/paths.py b/rdflib/paths.py
index df713617..9f953878 100644
--- a/rdflib/paths.py
+++ b/rdflib/paths.py
@@ -213,6 +213,15 @@ OneOrMore = "+"
 ZeroOrOne = "?"
 
 
+def _n3(
+    arg: Union["URIRef", "Path"], namespace_manager: Optional["NamespaceManager"] = None
+) -> str:
+    # type error: Item "Path" of "Union[Path, URIRef]" has no attribute "n3"  [union-attr]
+    if isinstance(arg, (SequencePath, AlternativePath)) and len(arg.args) > 1:
+        return "(%s)" % arg.n3(namespace_manager)
+    return arg.n3(namespace_manager)  # type: ignore[union-attr]
+
+
 @total_ordering
 class Path:
     __or__: Callable[["Path", Union["URIRef", "Path"]], "AlternativePath"]
@@ -260,8 +269,7 @@ class InvPath(Path):
         return "Path(~%s)" % (self.arg,)
 
     def n3(self, namespace_manager: Optional["NamespaceManager"] = None) -> str:
-        # type error: Item "Path" of "Union[Path, URIRef]" has no attribute "n3"  [union-attr]
-        return "^%s" % self.arg.n3(namespace_manager)  # type: ignore[union-attr]
+        return "^%s" % _n3(self.arg, namespace_manager)
 
 
 class SequencePath(Path):
@@ -318,8 +326,7 @@ class SequencePath(Path):
         return "Path(%s)" % " / ".join(str(x) for x in self.args)
 
     def n3(self, namespace_manager: Optional["NamespaceManager"] = None) -> str:
-        # type error: Item "Path" of "Union[Path, URIRef]" has no attribute "n3"  [union-attr]
-        return "/".join(a.n3(namespace_manager) for a in self.args)  # type: ignore[union-attr]
+        return "/".join(_n3(a, namespace_manager) for a in self.args)
 
 
 class AlternativePath(Path):
@@ -345,8 +352,7 @@ class AlternativePath(Path):
         return "Path(%s)" % " | ".join(str(x) for x in self.args)
 
     def n3(self, namespace_manager: Optional["NamespaceManager"] = None) -> str:
-        # type error: Item "Path" of "Union[Path, URIRef]" has no attribute "n3"  [union-attr]
-        return "|".join(a.n3(namespace_manager) for a in self.args)  # type: ignore[union-attr]
+        return "|".join(_n3(a, namespace_manager) for a in self.args)
 
 
 class MulPath(Path):
@@ -470,8 +476,7 @@ class MulPath(Path):
         return "Path(%s%s)" % (self.path, self.mod)
 
     def n3(self, namespace_manager: Optional["NamespaceManager"] = None) -> str:
-        # type error: Item "Path" of "Union[Path, URIRef]" has no attribute "n3"  [union-attr]
-        return "%s%s" % (self.path.n3(namespace_manager), self.mod)  # type: ignore[union-attr]
+        return "%s%s" % (_n3(self.path, namespace_manager), self.mod)
 
 
 class NegatedPath(Path):
@@ -505,8 +510,7 @@ class NegatedPath(Path):
         return "Path(! %s)" % ",".join(str(x) for x in self.args)
 
     def n3(self, namespace_manager: Optional["NamespaceManager"] = None) -> str:
-        # type error: Item "Path" of "Union[Path, URIRef]" has no attribute "n3"  [union-attr]
-        return "!(%s)" % ("|".join(arg.n3(namespace_manager) for arg in self.args))  # type: ignore[union-attr]
+        return "!(%s)" % ("|".join(_n3(arg, namespace_manager) for arg in self.args))
 
 
 class PathList(list):
diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py
index 18bee3f2..6f2aa50a 100644
--- a/rdflib/plugins/serializers/trig.py
+++ b/rdflib/plugins/serializers/trig.py
@@ -40,7 +40,8 @@ class TrigSerializer(TurtleSerializer):
             if len(context) == 0:
                 continue
             self.store = context
-            self.getQName(context.identifier)
+            # Don't generate a new prefix for a graph URI if one already exists
+            self.getQName(context.identifier, False)
             self._subjects = {}
 
             for triple in context:
@@ -97,7 +98,8 @@ class TrigSerializer(TurtleSerializer):
                 if isinstance(store.identifier, BNode):
                     iri = store.identifier.n3()
                 else:
-                    iri = self.getQName(store.identifier)
+                    # Show the full graph URI if a prefix for it doesn't already exist
+                    iri = self.getQName(store.identifier, False)
                     if iri is None:
                         # type error: "IdentifiedNode" has no attribute "n3"
                         iri = store.identifier.n3()  # type: ignore[attr-defined]


Test patch:
diff --git a/test/test_path.py b/test/test_path.py
index ad967849..97040523 100644
--- a/test/test_path.py
+++ b/test/test_path.py
@@ -54,15 +54,40 @@ nsm = g.namespace_manager
             "rdfs:subClassOf?",
         ),
         (
-            RDF.type / RDFS.subClassOf * "*",
+            RDF.type / MulPath(RDFS.subClassOf, "*"),
             f"<{RDF.type}>/<{RDFS.subClassOf}>*",
             "rdf:type/rdfs:subClassOf*",
         ),
+        (
+            RDF.type / ((SequencePath(RDFS.subClassOf)) * "*"),
+            f"<{RDF.type}>/<{RDFS.subClassOf}>*",
+            "rdf:type/rdfs:subClassOf*",
+        ),
+        (
+            RDF.type / RDFS.subClassOf * "*",
+            f"(<{RDF.type}>/<{RDFS.subClassOf}>)*",
+            "(rdf:type/rdfs:subClassOf)*",
+        ),
         (
             -(RDF.type | RDFS.subClassOf),
             f"!(<{RDF.type}>|<{RDFS.subClassOf}>)",
             "!(rdf:type|rdfs:subClassOf)",
         ),
+        (
+            -(RDF.type | ((SequencePath(RDFS.subClassOf)) * "*")),
+            f"!(<{RDF.type}>|<{RDFS.subClassOf}>*)",
+            "!(rdf:type|rdfs:subClassOf*)",
+        ),
+        (
+            SequencePath(RDFS.subClassOf),
+            f"<{RDFS.subClassOf}>",
+            "rdfs:subClassOf",
+        ),
+        (
+            AlternativePath(RDFS.subClassOf),
+            f"<{RDFS.subClassOf}>",
+            "rdfs:subClassOf",
+        ),
     ],
 )
 def test_paths_n3(
diff --git a/test/test_trig.py b/test/test_trig.py
index de5c2108..1c158fa8 100644
--- a/test/test_trig.py
+++ b/test/test_trig.py
@@ -60,15 +60,15 @@ def test_remember_namespace():
     # prefix for the graph but later serialize() calls would work.
     first_out = g.serialize(format="trig", encoding="latin-1")
     second_out = g.serialize(format="trig", encoding="latin-1")
-    assert b"@prefix ns1: <http://example.com/> ." in second_out
-    assert b"@prefix ns1: <http://example.com/> ." in first_out
+    assert b"@prefix ns1: <http://example.com/> ." not in second_out
+    assert b"@prefix ns1: <http://example.com/> ." not in first_out
 
 
 def test_graph_qname_syntax():
     g = rdflib.ConjunctiveGraph()
     g.add(TRIPLE + (rdflib.URIRef("http://example.com/graph1"),))
     out = g.serialize(format="trig", encoding="latin-1")
-    assert b"ns1:graph1 {" in out
+    assert b"ns1:graph1 {" not in out
 
 
 def test_graph_uri_syntax():
@@ -178,9 +178,9 @@ def test_prefixes():
     cg.parse(data=data, format="trig")
     data = cg.serialize(format="trig", encoding="latin-1")
 
-    assert "ns2: <http://ex.org/docs/".encode("latin-1") in data, data
+    assert "ns2: <http://ex.org/docs/".encode("latin-1") not in data, data
     assert "<ns2:document1>".encode("latin-1") not in data, data
-    assert "ns2:document1".encode("latin-1") in data, data
+    assert "ns2:document1".encode("latin-1") not in data, data
 
 
 def test_issue_2154():