diff --git a/rdflib/graph.py b/rdflib/graph.py index 8234f597f..d4f6037a3 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -1515,17 +1515,13 @@ def parse( def query( self, - query_object: Union[None, str, Query], + query_object: Union[str, Query], processor: Union[str, query.Processor] = "sparql", result: Union[str, Type[query.Result]] = "sparql", initNs: Optional[Mapping[str, Any]] = None, # noqa: N803 initBindings: Optional[Mapping[str, Identifier]] = None, # noqa: N803 use_store_provided: bool = True, *args: Any, - ask_query: Optional[AskQuery] = None, - construct_query: Optional[ConstructQuery] = None, - describe_query: Optional[DescribeQuery] = None, - select_query: Optional[SelectQuery] = None, **kwargs: Any, ) -> query.Result: """ @@ -1555,14 +1551,6 @@ def query( """ - # Requirement: Exactly one of the query arguments is non-null. - populated_query_arguments: List[Union[str, Query]] = [x for x in [query_object, ask_query, construct_query, describe_query, select_query] if x is not None] - if len(populated_query_arguments) == 0: - raise ValueError("No query argument was provided.") - elif len(populated_query_arguments) > 1: - raise ValueError("Multiple query arguments were provided.") - passing_query_object: Union[str, Query] = populated_query_arguments[0] - initBindings = initBindings or {} # noqa: N806 initNs = initNs or dict(self.namespaces()) # noqa: N806 @@ -1575,7 +1563,7 @@ def query( if hasattr(self.store, "query") and use_store_provided: try: return self.store.query( - passing_query_object, + query_object, initNs, initBindings, query_graph, @@ -1590,7 +1578,7 @@ def query( processor = plugin.get(processor, query.Processor)(self) # type error: Argument 1 to "Result" has incompatible type "Mapping[str, Any]"; expected "str" - return result(processor.query(passing_query_object, initBindings, initNs, **kwargs)) # type: ignore[arg-type] + return result(processor.query(query_object, initBindings, initNs, **kwargs)) # type: ignore[arg-type] def update( self, diff --git a/rdflib/plugins/sparql/sparql.py b/rdflib/plugins/sparql/sparql.py index c466e018a..92cf7684e 100644 --- a/rdflib/plugins/sparql/sparql.py +++ b/rdflib/plugins/sparql/sparql.py @@ -5,6 +5,7 @@ import itertools import typing as t from collections.abc import Mapping, MutableMapping +# TODO - import Self from typing_extensions when Python < 3.11. from typing import ( TYPE_CHECKING, Any, @@ -14,6 +15,7 @@ Iterable, List, Optional, + Self, Tuple, TypeVar, Union, @@ -25,6 +27,7 @@ from rdflib.graph import ConjunctiveGraph, Dataset, Graph from rdflib.namespace import NamespaceManager from rdflib.plugins.sparql.parserutils import CompValue +from rdflib.plugins.sparql.processor import prepareQuery from rdflib.term import BNode, Identifier, Literal, Node, URIRef, Variable if TYPE_CHECKING: @@ -489,6 +492,17 @@ def __init__(self, prologue: Prologue, algebra: CompValue): self.algebra = algebra self._original_args: Tuple[str, Mapping[str, str], Optional[str]] + @classmethod + def prepare( + cls, + queryString: str, + initNs: Optional[Mapping[str, Any]] = None, + base: Optional[str] = None, + ) -> Self: + result = prepareQuery(queryString, initNs, base) + assert isinstance(result, cls) + return cls(result.prologue, result.algebra) + class AskQuery(Query): pass