Skip to content

Commit 4d9935c

Browse files
slahnnicholascaredmondchuc
authored
Specify Optional parameters in Graph.triples_choices (#3075)
* 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. * style: remove unused imports --------- Co-authored-by: Nicholas Car <[email protected]> Co-authored-by: Edmond Chuc <[email protected]> Co-authored-by: Edmond Chuc <[email protected]>
1 parent 14071b0 commit 4d9935c

File tree

3 files changed

+12
-29
lines changed

3 files changed

+12
-29
lines changed

rdflib/graph.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@
355355
_TripleOrQuadSelectorType = Union["_TripleSelectorType", "_QuadSelectorType"]
356356
_TriplePathType = Tuple["_SubjectType", Path, "_ObjectType"]
357357
_TripleOrTriplePathType = Union["_TripleType", "_TriplePathType"]
358+
_TripleChoiceType = Union[
359+
Tuple[List[_SubjectType], Optional[_PredicateType], Optional[_ObjectType]],
360+
Tuple[Optional[_SubjectType], List[_PredicateType], Optional[_ObjectType]],
361+
Tuple[Optional[_SubjectType], Optional[_PredicateType], List[_ObjectType]],
362+
]
358363

359364
_GraphT = TypeVar("_GraphT", bound="Graph")
360365
_ConjunctiveGraphT = TypeVar("_ConjunctiveGraphT", bound="ConjunctiveGraph")
@@ -994,11 +999,7 @@ def predicate_objects(
994999

9951000
def triples_choices(
9961001
self,
997-
triple: Union[
998-
Tuple[List[_SubjectType], _PredicateType, _ObjectType],
999-
Tuple[_SubjectType, List[_PredicateType], _ObjectType],
1000-
Tuple[_SubjectType, _PredicateType, List[_ObjectType]],
1001-
],
1002+
triple: _TripleChoiceType,
10021003
context: Optional[_ContextType] = None,
10031004
) -> Generator[_TripleType, None, None]:
10041005
subject, predicate, object_ = triple
@@ -2196,11 +2197,7 @@ def quads(
21962197

21972198
def triples_choices(
21982199
self,
2199-
triple: Union[
2200-
Tuple[List[_SubjectType], _PredicateType, _ObjectType],
2201-
Tuple[_SubjectType, List[_PredicateType], _ObjectType],
2202-
Tuple[_SubjectType, _PredicateType, List[_ObjectType]],
2203-
],
2200+
triple: _TripleChoiceType,
22042201
context: Optional[_ContextType] = None,
22052202
) -> Generator[_TripleType, None, None]:
22062203
"""Iterate over all the triples in the entire conjunctive graph"""
@@ -2946,11 +2943,7 @@ def __isub__(self: _GraphT, other: Iterable[_TripleType]) -> NoReturn:
29462943

29472944
def triples_choices(
29482945
self,
2949-
triple: Union[
2950-
Tuple[List[_SubjectType], _PredicateType, _ObjectType],
2951-
Tuple[_SubjectType, List[_PredicateType], _ObjectType],
2952-
Tuple[_SubjectType, _PredicateType, List[_ObjectType]],
2953-
],
2946+
triple: _TripleChoiceType,
29542947
context: Optional[_ContextType] = None,
29552948
) -> Generator[_TripleType, None, None]:
29562949
subject, predicate, object_ = triple

rdflib/plugins/stores/sparqlstore.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
_TripleType,
3636
_ContextType,
3737
_QuadType,
38+
_TripleChoiceType,
3839
_TriplePatternType,
3940
_SubjectType,
4041
_PredicateType,
@@ -367,11 +368,7 @@ def triples( # type: ignore[override]
367368

368369
def triples_choices(
369370
self,
370-
_: Tuple[
371-
Union[_SubjectType, List[_SubjectType]],
372-
Union[_PredicateType, List[_PredicateType]],
373-
Union[_ObjectType, List[_ObjectType]],
374-
],
371+
_: _TripleChoiceType,
375372
context: Optional[_ContextType] = None,
376373
) -> Generator[
377374
Tuple[

rdflib/store.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
Generator,
3737
Iterable,
3838
Iterator,
39-
List,
4039
Mapping,
4140
Optional,
4241
Tuple,
@@ -49,10 +48,8 @@
4948
from rdflib.graph import (
5049
Graph,
5150
_ContextType,
52-
_ObjectType,
53-
_PredicateType,
5451
_QuadType,
55-
_SubjectType,
52+
_TripleChoiceType,
5653
_TriplePatternType,
5754
_TripleType,
5855
)
@@ -281,11 +278,7 @@ def remove(
281278

282279
def triples_choices(
283280
self,
284-
triple: Union[
285-
Tuple[List[_SubjectType], _PredicateType, _ObjectType],
286-
Tuple[_SubjectType, List[_PredicateType], _ObjectType],
287-
Tuple[_SubjectType, _PredicateType, List[_ObjectType]],
288-
],
281+
triple: _TripleChoiceType,
289282
context: Optional[_ContextType] = None,
290283
) -> Generator[
291284
Tuple[

0 commit comments

Comments
 (0)