From fe1a8f8a3e3d03c39552fedba44b59b13d23814e Mon Sep 17 00:00:00 2001 From: Iwan Aucamp Date: Tue, 21 Mar 2023 22:30:09 +0100 Subject: fix: add `__hash__` and `__eq__` back to `rdflib.paths.Path` (#2292) These methods were removed when `@total_ordering` was added, but `@total_ordering` does not add them, so removing them essentially removes functionality. This change adds the methods back and adds tests to ensure they work correctly. All path related tests are also moved into one file. - Closes . - Closes . --- rdflib/paths.py | 6 +++ test/test_mulpath_n3.py | 8 ---- test/test_path.py | 117 ++++++++++++++++++++++++++++++++++++++++++++++++ test/test_paths_n3.py | 73 ------------------------------ 4 files changed, 123 insertions(+), 81 deletions(-) delete mode 100644 test/test_mulpath_n3.py create mode 100644 test/test_path.py delete mode 100644 test/test_paths_n3.py diff --git a/rdflib/paths.py b/rdflib/paths.py index defd0e75..6ca42d74 100644 --- a/rdflib/paths.py +++ b/rdflib/paths.py @@ -229,6 +229,12 @@ class Path(object): ) -> Iterator[Tuple["_SubjectType", "_ObjectType"]]: raise NotImplementedError() + def __hash__(self): + return hash(repr(self)) + + def __eq__(self, other): + return repr(self) == repr(other) + def __lt__(self, other: Any) -> bool: if not isinstance(other, (Path, Node)): raise TypeError( diff --git a/test/test_mulpath_n3.py b/test/test_mulpath_n3.py deleted file mode 100644 index 41885361..00000000 --- a/test/test_mulpath_n3.py +++ /dev/null @@ -1,8 +0,0 @@ -from rdflib import URIRef -from rdflib.paths import ZeroOrMore - - -def test_mulpath_n3(): - uri = "http://example.com/foo" - n3 = (URIRef(uri) * ZeroOrMore).n3() - assert n3 == "<" + uri + ">*" diff --git a/test/test_path.py b/test/test_path.py new file mode 100644 index 00000000..ad967849 --- /dev/null +++ b/test/test_path.py @@ -0,0 +1,117 @@ +import logging +from typing import Union + +import pytest + +from rdflib import RDF, RDFS, Graph, URIRef +from rdflib.namespace import DCAT, DCTERMS +from rdflib.paths import ( + AlternativePath, + InvPath, + MulPath, + NegatedPath, + OneOrMore, + Path, + SequencePath, + ZeroOrMore, + ZeroOrOne, +) + +g = Graph() +nsm = g.namespace_manager + + +@pytest.mark.parametrize( + "path, no_nsm, with_nsm", + [ + (~RDF.type, f"^<{RDF.type}>", "^rdf:type"), + ( + 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, f"!(<{RDF.type}>)", "!(rdf:type)"), + # type errors: Unsupported operand types for * ("URIRef" and "str") + # note on type errors: The operator is defined but in an odd place + ( + RDFS.subClassOf * ZeroOrMore, # type: ignore[operator] + f"<{RDFS.subClassOf}>*", + "rdfs:subClassOf*", + ), + ( + RDFS.subClassOf * OneOrMore, # type: ignore[operator] + f"<{RDFS.subClassOf}>+", + "rdfs:subClassOf+", + ), + ( + RDFS.subClassOf * ZeroOrOne, # type: ignore[operator] + f"<{RDFS.subClassOf}>?", + "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)", + ), + ], +) +def test_paths_n3( + path: Union[InvPath, SequencePath, AlternativePath, MulPath, NegatedPath], + no_nsm: str, + with_nsm: str, +) -> None: + logging.debug("path = %s", path) + assert path.n3() == no_nsm + assert path.n3(nsm) == with_nsm + + +def test_mulpath_n3(): + uri = "http://example.com/foo" + n3 = (URIRef(uri) * ZeroOrMore).n3() + assert n3 == "<" + uri + ">*" + + +@pytest.mark.parametrize( + ["lhs", "rhs"], + [ + (DCTERMS.temporal / DCAT.endDate, DCTERMS.temporal / DCAT.endDate), + (SequencePath(DCTERMS.temporal, DCAT.endDate), DCTERMS.temporal / DCAT.endDate), + ], +) +def test_eq(lhs: Path, rhs: Path) -> None: + logging.debug("lhs = %s/%r, rhs = %s/%r", type(lhs), lhs, type(rhs), rhs) + assert lhs == rhs + + +@pytest.mark.parametrize( + ["lhs", "rhs"], + [ + (DCTERMS.temporal / DCAT.endDate, DCTERMS.temporal / DCAT.endDate), + (SequencePath(DCTERMS.temporal, DCAT.endDate), DCTERMS.temporal / DCAT.endDate), + ], +) +def test_hash(lhs: Path, rhs: Path) -> None: + logging.debug("lhs = %s/%r, rhs = %s/%r", type(lhs), lhs, type(rhs), rhs) + assert hash(lhs) == hash(rhs) + + +@pytest.mark.parametrize( + ["insert_path", "check_path"], + [ + (DCTERMS.temporal / DCAT.endDate, DCTERMS.temporal / DCAT.endDate), + (SequencePath(DCTERMS.temporal, DCAT.endDate), DCTERMS.temporal / DCAT.endDate), + ], +) +def test_dict_key(insert_path: Path, check_path: Path) -> None: + d = {insert_path: "foo"} + assert d[check_path] == "foo" diff --git a/test/test_paths_n3.py b/test/test_paths_n3.py deleted file mode 100644 index b7834721..00000000 --- a/test/test_paths_n3.py +++ /dev/null @@ -1,73 +0,0 @@ -import logging -from typing import Union - -import pytest - -from rdflib import RDF, RDFS, Graph -from rdflib.paths import ( - AlternativePath, - InvPath, - MulPath, - NegatedPath, - OneOrMore, - SequencePath, - ZeroOrMore, - ZeroOrOne, -) - -g = Graph() -nsm = g.namespace_manager - - -@pytest.mark.parametrize( - "path, no_nsm, with_nsm", - [ - (~RDF.type, f"^<{RDF.type}>", "^rdf:type"), - ( - 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, f"!(<{RDF.type}>)", "!(rdf:type)"), - # type errors: Unsupported operand types for * ("URIRef" and "str") - # note on type errors: The operator is defined but in an odd place - ( - RDFS.subClassOf * ZeroOrMore, # type: ignore[operator] - f"<{RDFS.subClassOf}>*", - "rdfs:subClassOf*", - ), - ( - RDFS.subClassOf * OneOrMore, # type: ignore[operator] - f"<{RDFS.subClassOf}>+", - "rdfs:subClassOf+", - ), - ( - RDFS.subClassOf * ZeroOrOne, # type: ignore[operator] - f"<{RDFS.subClassOf}>?", - "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)", - ), - ], -) -def test_paths_n3( - path: Union[InvPath, SequencePath, AlternativePath, MulPath, NegatedPath], - no_nsm: str, - with_nsm: str, -) -> None: - logging.debug("path = %s", path) - assert path.n3() == no_nsm - assert path.n3(nsm) == with_nsm -- cgit v1.2.1