Skip to content

Commit

Permalink
fix: widen Graph.__contains__ type-hints to accept Path values (#…
Browse files Browse the repository at this point in the history
…2323)

Change the type-hints for `Graph.__contains__` to also accept `Path`
values as the parameter is passed to the `Graph.triples` function,
which accepts `Path` values.
  • Loading branch information
EFord36 committed Apr 3, 2023
1 parent 081a974 commit 1c45ec4
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@
_TripleOrQuadSelectorType = Union["_TripleSelectorType", "_QuadSelectorType"]
_TriplePathType = Tuple["_SubjectType", Path, "_ObjectType"]
_TripleOrTriplePathType = Union["_TripleType", "_TriplePathType"]
# _QuadPathType = Tuple["_SubjectType", Path, "_ObjectType", "_ContextType"]
# _QuadOrQuadPathType = Union["_QuadType", "_QuadPathType"]

_GraphT = TypeVar("_GraphT", bound="Graph")
_ConjunctiveGraphT = TypeVar("_ConjunctiveGraphT", bound="ConjunctiveGraph")
Expand Down Expand Up @@ -677,7 +675,7 @@ def __iter__(self) -> Generator["_TripleType", None, None]:
"""Iterates over all triples in the store"""
return self.triples((None, None, None))

def __contains__(self, triple: _TriplePatternType) -> bool:
def __contains__(self, triple: _TripleSelectorType) -> bool:
"""Support for 'triple in graph' syntax"""
for triple in self.triples(triple):
return True
Expand Down Expand Up @@ -1979,7 +1977,7 @@ def _spoc(
c = self._graph(c)
return s, p, o, c

def __contains__(self, triple_or_quad: _TripleOrQuadPatternType) -> bool:
def __contains__(self, triple_or_quad: _TripleOrQuadSelectorType) -> bool:
"""Support for 'triple/quad in graph' syntax"""
s, p, o, c = self._spoc(triple_or_quad)
for t in self.triples((s, p, o), context=c):
Expand Down Expand Up @@ -2753,7 +2751,7 @@ def triples(
for s1, p1, o1 in graph.triples((s, p, o)):
yield s1, p1, o1

def __contains__(self, triple_or_quad: _TripleOrQuadPatternType) -> bool:
def __contains__(self, triple_or_quad: _TripleOrQuadSelectorType) -> bool:
context = None
if len(triple_or_quad) == 4:
# type error: Tuple index out of range
Expand Down

0 comments on commit 1c45ec4

Please sign in to comment.