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

feat: add beartype checking #153

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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: 19 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sparqlwrapper = "^2.0.0"
pydantic = "^2.9.2"


beartype = "^0.19.0"
[tool.poetry.group.dev.dependencies]
ruff = "^0.7.0"
deptry = "^0.20.0"
Expand Down
12 changes: 8 additions & 4 deletions rdfproxy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from rdfproxy.adapter import SPARQLModelAdapter # noqa: F401
from rdfproxy.mapper import ModelBindingsMapper # noqa: F401
from rdfproxy.utils._types import SPARQLBinding # noqa: F401
from rdfproxy.utils.models import Page # noqa: F401
from beartype.claw import beartype_this_package

beartype_this_package()

from rdfproxy.adapter import SPARQLModelAdapter # noqa: F401, E402
from rdfproxy.mapper import ModelBindingsMapper # noqa: F401, E402
from rdfproxy.utils._types import SPARQLBinding # noqa: F401, E402
from rdfproxy.utils.models import Page # noqa: F401, E402
1 change: 1 addition & 0 deletions rdfproxy/utils/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
_TModelInstance = TypeVar("_TModelInstance", bound=BaseModel)


@runtime_checkable
class ItemsQueryConstructor(Protocol):
def __call__(self, query: str, limit: int, offset: int) -> str: ...

Expand Down
15 changes: 10 additions & 5 deletions rdfproxy/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@
MissingModelConfigException,
UnboundGroupingKeyException,
)
from rdfproxy.utils._types import ModelBoolPredicate, SPARQLBinding, _TModelBoolValue
from rdfproxy.utils._types import (
ModelBoolPredicate,
SPARQLBinding,
_TModelBoolValue,
_TModelInstance,
)


def _is_type(obj: type | None, _type: type) -> bool:
def _is_type(obj: Any, _type: type) -> bool:
"""Check if an obj is type _type or a GenericAlias with origin _type."""
return (obj is _type) or (get_origin(obj) is _type)


def _is_list_type(obj: type | None) -> bool:
def _is_list_type(obj: Any) -> bool:
"""Check if obj is a list type."""
return _is_type(obj, list)


def _is_list_basemodel_type(obj: type | None) -> bool:
def _is_list_basemodel_type(obj: Any) -> bool:
"""Check if a type is list[pydantic.BaseModel]."""
return (get_origin(obj) is list) and all(
issubclass(cls, BaseModel) for cls in get_args(obj)
Expand Down Expand Up @@ -104,7 +109,7 @@ def _get_model_bool_predicate_from_config_value(
)


def get_model_bool_predicate(model: BaseModel) -> ModelBoolPredicate:
def get_model_bool_predicate(model: type[_TModelInstance]) -> ModelBoolPredicate:
"""Get the applicable model_bool predicate function given a model."""
if (model_bool_value := model.model_config.get("model_bool", None)) is None:
model_bool_predicate = default_model_bool_predicate
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/test_sad_path_get_bindings_from_query_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

import pytest

from SPARQLWrapper.Wrapper import QueryResult
from rdfproxy.utils.sparql_utils import get_bindings_from_query_result


def test_basic_sad_path_get_bindings_from_query_result():
with mock.patch("SPARQLWrapper.QueryResult") as mock_query_result:
mock_query_result.__class__ = QueryResult

mock_query_result.return_value.requestedFormat = "xml"
exception_message = (
"Only QueryResult objects with JSON format are currently supported."
Expand Down