summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIwan Aucamp <aucampia@gmail.com>2023-03-21 22:30:09 +0100
committerGitHub <noreply@github.com>2023-03-21 22:30:09 +0100
commitfe1a8f8a3e3d03c39552fedba44b59b13d23814e (patch)
tree58f6f17d7c00e5caac5878f62477979034418302
parentdd44ae186183b0da8fef63853c9da2be826eb643 (diff)
downloadrdflib-fe1a8f8a3e3d03c39552fedba44b59b13d23814e.tar.gz
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 <https://github.com/RDFLib/rdflib/issues/2281>. - Closes <https://github.com/RDFLib/rdflib/issues/2242>.
-rw-r--r--rdflib/paths.py6
-rw-r--r--test/test_mulpath_n3.py8
-rw-r--r--test/test_path.py (renamed from test/test_paths_n3.py)46
3 files changed, 51 insertions, 9 deletions
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_paths_n3.py b/test/test_path.py
index b7834721..ad967849 100644
--- a/test/test_paths_n3.py
+++ b/test/test_path.py
@@ -3,13 +3,15 @@ from typing import Union
import pytest
-from rdflib import RDF, RDFS, Graph
+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,
@@ -71,3 +73,45 @@ def test_paths_n3(
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"