Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Type hint cleanup #15

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]: ...
Loading