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

Corrected n3 method of path objects (fixes #2503) #2504

Merged
merged 3 commits into from
Jul 31, 2023
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
24 changes: 14 additions & 10 deletions rdflib/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@
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"]
Expand Down Expand Up @@ -260,8 +269,7 @@ def __repr__(self) -> str:
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):
Expand Down Expand Up @@ -318,8 +326,7 @@ def __repr__(self) -> str:
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):
Expand All @@ -345,8 +352,7 @@ def __repr__(self) -> str:
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):
Expand Down Expand Up @@ -470,8 +476,7 @@ def __repr__(self) -> str:
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):
Expand Down Expand Up @@ -505,8 +510,7 @@ def __repr__(self) -> str:
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):
Expand Down
27 changes: 26 additions & 1 deletion test/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,40 @@
"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(
Expand Down