Skip to content

Commit

Permalink
fix: Type hint cleanup
Browse files Browse the repository at this point in the history
Replaced ModelInstance type with
rdfproxy.utils._types._TMOdelInstance. ModelInstance was defined using
PEP 695 before but the type will be used all over the code base and
should have a central definition.

Fixed _TModelConstructorCallable type to be a Callable expecting a
SPARQLWrapper.QueryResult parameter and returning an Iterable of
Pydantic model instances.

closes: #10
  • Loading branch information
lu-pl committed Jul 25, 2024
1 parent e8a16eb commit b73fc95
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 14 additions & 6 deletions rdfproxy/adapter.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"""SPARQLModelAdapter class for QueryResult to Pydantic model conversions."""

from collections.abc import Sequence
from collections.abc import Iterable
from typing import cast

from SPARQLWrapper import JSON, QueryResult, SPARQLWrapper
from pydantic import BaseModel
from rdfproxy.utils._types import _TModelConstructorCallable

from rdfproxy.utils._types import _TModelConstructorCallable, _TModelInstance
from rdfproxy.utils.utils import (
get_bindings_from_query_result,
instantiate_model_from_kwargs,
)


class SPARQLModelAdapter[ModelType: BaseModel]:
class SPARQLModelAdapter:
"""Adapter/Mapper for QueryResult to Pydantic model conversions."""

def __init__(self, sparql_wrapper: SPARQLWrapper) -> None:
Expand All @@ -20,19 +22,25 @@ def __init__(self, sparql_wrapper: SPARQLWrapper) -> None:
if self.sparql_wrapper.returnFormat != "json":
self.sparql_wrapper.setReturnFormat(JSON)

def __call__(self, query: str, model_constructor) -> Sequence[ModelType]:
def __call__(
self,
query: str,
model_constructor: type[_TModelInstance] | _TModelConstructorCallable,
) -> Iterable[_TModelInstance]:
self.sparql_wrapper.setQuery(query)
query_result: QueryResult = self.sparql_wrapper.query()

if isinstance(model_constructor, type(BaseModel)):
model_constructor = cast(type[_TModelInstance], model_constructor)

bindings = get_bindings_from_query_result(query_result)
models: list[ModelType] = [
models: list[_TModelInstance] = [
instantiate_model_from_kwargs(model_constructor, **binding)
for binding in bindings
]

elif isinstance(model_constructor, _TModelConstructorCallable):
models: list[ModelType] = model_constructor(query_result)
models: Iterable[_TModelInstance] = model_constructor(query_result)

else:
raise TypeError(
Expand Down
8 changes: 4 additions & 4 deletions rdfproxy/utils/_types.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"""Type definitions for rdfproxy."""

from collections.abc import Iterable
from typing import Annotated, Protocol, TypeVar, runtime_checkable

from SPARQLWrapper import QueryResult
from pydantic import BaseModel


_TModelInstance: Annotated[TypeVar, "Type defintion for Pydantic model instances."] = (
TypeVar("_TModelInstance", bound=BaseModel)
)
_TModelInstance = TypeVar("_TModelInstance", bound=BaseModel)


@runtime_checkable
class _TModelConstructorCallable[ModelType: BaseModel](Protocol):
"""Callback protocol for model constructor callables."""

def __call__(self, **kwargs) -> ModelType: ...
def __call__(self, query_result: QueryResult) -> Iterable[ModelType]: ...

0 comments on commit b73fc95

Please sign in to comment.