Skip to content

Commit

Permalink
Switch to Query.prepare classmethod
Browse files Browse the repository at this point in the history
Potentially, adding an assert() addresses the subclass type review,
per `@classmethod` docs:

> ... If a class method is called for a derived class, the derived class
> object is passed as the implied first argument.

This patch also reverts the first-draft change to `Graph.query`.

References:
* https://docs.python.org/3/library/functions.html#classmethod
* #2283 (comment)

Suggested-by: Iwan Aucamp <[email protected]>
Signed-off-by: Alex Nelson <[email protected]>
  • Loading branch information
ajnelson-nist committed Sep 10, 2024
1 parent a24df3f commit 09f0bc8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
18 changes: 3 additions & 15 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions rdflib/plugins/sparql/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,6 +15,7 @@
Iterable,
List,
Optional,
Self,
Tuple,
TypeVar,
Union,
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 09f0bc8

Please sign in to comment.