Skip to content

Commit

Permalink
fix: make rdflib.paths.Path abstract (#2516)
Browse files Browse the repository at this point in the history
Instances of this class should not exist as it has methods that have no
implementation. This change makes it an abstract base class with two
abstract methods, `eval` and `n3`.

- Fixes <#2509>.
  • Loading branch information
mielvds authored Aug 2, 2023
1 parent 9fcc3ab commit 0595b68
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions rdflib/paths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod

__doc__ = r"""
This module implements the SPARQL 1.1 Property path operators, as
Expand Down Expand Up @@ -216,27 +218,31 @@
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]
return arg.n3(namespace_manager)


@total_ordering
class Path:
class Path(ABC):
__or__: Callable[["Path", Union["URIRef", "Path"]], "AlternativePath"]
__invert__: Callable[["Path"], "InvPath"]
__neg__: Callable[["Path"], "NegatedPath"]
__truediv__: Callable[["Path", Union["URIRef", "Path"]], "SequencePath"]
__mul__: Callable[["Path", str], "MulPath"]

@abstractmethod
def eval(
self,
graph: "Graph",
subj: Optional["_SubjectType"] = None,
obj: Optional["_ObjectType"] = None,
) -> Iterator[Tuple["_SubjectType", "_ObjectType"]]:
raise NotImplementedError()
...

@abstractmethod
def n3(self, namespace_manager: Optional["NamespaceManager"] = None) -> str:
...

def __hash__(self):
return hash(repr(self))
Expand Down

0 comments on commit 0595b68

Please sign in to comment.