From 51f94470067830ea8a3435d6117ecd7932214715 Mon Sep 17 00:00:00 2001 From: Sigmund Lahn Date: Fri, 21 Feb 2025 12:32:49 +0100 Subject: [PATCH] Specify `Optional` parameters in `Graph.triples_choices` The two non-list parameters can be `None`, but this is not reflected in the type hint. Also introduces a type alias to simplify method signatures. --- rdflib/graph.py | 23 ++++++++--------------- rdflib/plugins/stores/sparqlstore.py | 7 ++----- rdflib/store.py | 7 ++----- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/rdflib/graph.py b/rdflib/graph.py index d74dd85cf..972f8aee4 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -355,6 +355,11 @@ _TripleOrQuadSelectorType = Union["_TripleSelectorType", "_QuadSelectorType"] _TriplePathType = Tuple["_SubjectType", Path, "_ObjectType"] _TripleOrTriplePathType = Union["_TripleType", "_TriplePathType"] +_TripleChoiceType = Union[ + Tuple[List[_SubjectType], Optional[_PredicateType], Optional[_ObjectType]], + Tuple[Optional[_SubjectType], List[_PredicateType], Optional[_ObjectType]], + Tuple[Optional[_SubjectType], Optional[_PredicateType], List[_ObjectType]], +] _GraphT = TypeVar("_GraphT", bound="Graph") _ConjunctiveGraphT = TypeVar("_ConjunctiveGraphT", bound="ConjunctiveGraph") @@ -983,11 +988,7 @@ def predicate_objects( def triples_choices( self, - triple: Union[ - Tuple[List[_SubjectType], _PredicateType, _ObjectType], - Tuple[_SubjectType, List[_PredicateType], _ObjectType], - Tuple[_SubjectType, _PredicateType, List[_ObjectType]], - ], + triple: _TripleChoiceType, context: Optional[_ContextType] = None, ) -> Generator[_TripleType, None, None]: subject, predicate, object_ = triple @@ -2185,11 +2186,7 @@ def quads( def triples_choices( self, - triple: Union[ - Tuple[List[_SubjectType], _PredicateType, _ObjectType], - Tuple[_SubjectType, List[_PredicateType], _ObjectType], - Tuple[_SubjectType, _PredicateType, List[_ObjectType]], - ], + triple: _TripleChoiceType, context: Optional[_ContextType] = None, ) -> Generator[_TripleType, None, None]: """Iterate over all the triples in the entire conjunctive graph""" @@ -2935,11 +2932,7 @@ def __isub__(self: _GraphT, other: Iterable[_TripleType]) -> NoReturn: def triples_choices( self, - triple: Union[ - Tuple[List[_SubjectType], _PredicateType, _ObjectType], - Tuple[_SubjectType, List[_PredicateType], _ObjectType], - Tuple[_SubjectType, _PredicateType, List[_ObjectType]], - ], + triple: _TripleChoiceType, context: Optional[_ContextType] = None, ) -> Generator[_TripleType, None, None]: subject, predicate, object_ = triple diff --git a/rdflib/plugins/stores/sparqlstore.py b/rdflib/plugins/stores/sparqlstore.py index f9827cf94..e7a9723e8 100644 --- a/rdflib/plugins/stores/sparqlstore.py +++ b/rdflib/plugins/stores/sparqlstore.py @@ -35,6 +35,7 @@ _TripleType, _ContextType, _QuadType, + _TripleChoiceType, _TriplePatternType, _SubjectType, _PredicateType, @@ -367,11 +368,7 @@ def triples( # type: ignore[override] def triples_choices( self, - _: Tuple[ - Union[_SubjectType, List[_SubjectType]], - Union[_PredicateType, List[_PredicateType]], - Union[_ObjectType, List[_ObjectType]], - ], + _: _TripleChoiceType, context: Optional[_ContextType] = None, ) -> Generator[ Tuple[ diff --git a/rdflib/store.py b/rdflib/store.py index 2ca03529a..02774bb56 100644 --- a/rdflib/store.py +++ b/rdflib/store.py @@ -53,6 +53,7 @@ _PredicateType, _QuadType, _SubjectType, + _TripleChoiceType, _TriplePatternType, _TripleType, ) @@ -281,11 +282,7 @@ def remove( def triples_choices( self, - triple: Union[ - Tuple[List[_SubjectType], _PredicateType, _ObjectType], - Tuple[_SubjectType, List[_PredicateType], _ObjectType], - Tuple[_SubjectType, _PredicateType, List[_ObjectType]], - ], + triple: _TripleChoiceType, context: Optional[_ContextType] = None, ) -> Generator[ Tuple[