From ef842b2a107ab6f53ba6fde09264eabb2536df28 Mon Sep 17 00:00:00 2001 From: Lukas Plank Date: Tue, 3 Dec 2024 10:53:34 +0100 Subject: [PATCH 1/3] feat: implement rdfproxy.ConfigDict extension for model configs Currently, the rdfproxy custom settings for pydantic.ConfigDict are rightfully flagged by static checkers. Subclassing pydantic.ConfigDict and providing rdfproxy-specific config options solves this problem and provides better IDE support for instantiating model configs. Closes #159. --- rdfproxy/__init__.py | 2 +- rdfproxy/utils/_types.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/rdfproxy/__init__.py b/rdfproxy/__init__.py index 6b95c77..984bb66 100644 --- a/rdfproxy/__init__.py +++ b/rdfproxy/__init__.py @@ -1,4 +1,4 @@ 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._types import ConfigDict, SPARQLBinding # noqa: F401 from rdfproxy.utils.models import Page, QueryParameters # noqa: F401 diff --git a/rdfproxy/utils/_types.py b/rdfproxy/utils/_types.py index 93b59d1..8969353 100644 --- a/rdfproxy/utils/_types.py +++ b/rdfproxy/utils/_types.py @@ -3,7 +3,7 @@ from collections.abc import Iterable from typing import Protocol, TypeAlias, TypeVar, runtime_checkable -from pydantic import BaseModel +from pydantic import BaseModel, ConfigDict as PydanticConfigDict _TModelInstance = TypeVar("_TModelInstance", bound=BaseModel) @@ -42,3 +42,10 @@ def __call__(self, model: BaseModel) -> bool: ... _TModelBoolValue: TypeAlias = ModelBoolPredicate | str | Iterable[str] + + +class ConfigDict(PydanticConfigDict, total=False): + """pydantic.ConfigDict extension for RDFProxy model_config options.""" + + group_by: str + model_bool: _TModelBoolValue From 89e70d37652c75a1b54a1dd61e7d918920c10247 Mon Sep 17 00:00:00 2001 From: Lukas Plank Date: Tue, 3 Dec 2024 11:06:51 +0100 Subject: [PATCH 2/3] docs: adapt readme and exmaples to rdfproxy.ConfigDict --- README.md | 4 ++-- examples/full_static_fastapi_example.py | 10 ++++++++-- examples/wikidata_grouped_person_fastapi_example.py | 10 ++++++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b63f5a1..b97c232 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,8 @@ The result set can be mapped to a nested Pydantic model like so: from typing import Annotated from fastapi import FastAPI, Query -from pydantic import BaseModel, ConfigDict -from rdfproxy import Page, QueryParameters, SPARQLBinding, SPARQLModelAdapter +from pydantic import BaseModel +from rdfproxy import ConfigDict, Page, QueryParameters, SPARQLBinding, SPARQLModelAdapter class Work(BaseModel): model_config = ConfigDict(group_by="name") diff --git a/examples/full_static_fastapi_example.py b/examples/full_static_fastapi_example.py index 38203ea..eb3231a 100644 --- a/examples/full_static_fastapi_example.py +++ b/examples/full_static_fastapi_example.py @@ -3,8 +3,14 @@ from typing import Annotated from fastapi import FastAPI, Query -from pydantic import BaseModel, ConfigDict -from rdfproxy import Page, QueryParameters, SPARQLBinding, SPARQLModelAdapter +from pydantic import BaseModel +from rdfproxy import ( + ConfigDict, + Page, + QueryParameters, + SPARQLBinding, + SPARQLModelAdapter, +) query = """ diff --git a/examples/wikidata_grouped_person_fastapi_example.py b/examples/wikidata_grouped_person_fastapi_example.py index 0e06def..951cd06 100644 --- a/examples/wikidata_grouped_person_fastapi_example.py +++ b/examples/wikidata_grouped_person_fastapi_example.py @@ -3,8 +3,14 @@ from typing import Annotated from fastapi import FastAPI, Query -from pydantic import BaseModel, ConfigDict -from rdfproxy import Page, QueryParameters, SPARQLBinding, SPARQLModelAdapter +from pydantic import BaseModel +from rdfproxy import ( + ConfigDict, + Page, + QueryParameters, + SPARQLBinding, + SPARQLModelAdapter, +) query = """ From fda47b50f9ae756e10986637d3b17af72fcf9c67 Mon Sep 17 00:00:00 2001 From: Lukas Plank Date: Thu, 5 Dec 2024 14:11:27 +0100 Subject: [PATCH 3/3] test: adapt tests to rdfproxy.ConfigDict --- tests/data/models/author_array_collection_model.py | 3 ++- tests/data/models/author_work_title_model.py | 3 ++- tests/data/models/dummy_model.py | 3 ++- tests/data/models/grouping_model.py | 3 ++- tests/data/models/nested_grouping_model.py | 3 ++- .../model_bindings_mapper_model_bool_parameters.py | 3 ++- tests/tests_adapter/test_adapter_grouped_pagination.py | 10 ++++++++-- .../test_sad_path_mapper_grouped_models.py | 4 ++-- 8 files changed, 22 insertions(+), 10 deletions(-) diff --git a/tests/data/models/author_array_collection_model.py b/tests/data/models/author_array_collection_model.py index 131a957..cebf19e 100644 --- a/tests/data/models/author_array_collection_model.py +++ b/tests/data/models/author_array_collection_model.py @@ -1,6 +1,7 @@ from typing import Annotated -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel +from rdfproxy import ConfigDict from rdfproxy.utils._types import SPARQLBinding diff --git a/tests/data/models/author_work_title_model.py b/tests/data/models/author_work_title_model.py index f934314..114bb86 100644 --- a/tests/data/models/author_work_title_model.py +++ b/tests/data/models/author_work_title_model.py @@ -2,7 +2,8 @@ from typing import Annotated -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel +from rdfproxy import ConfigDict from rdfproxy.utils._types import SPARQLBinding diff --git a/tests/data/models/dummy_model.py b/tests/data/models/dummy_model.py index d9de028..041bf4d 100644 --- a/tests/data/models/dummy_model.py +++ b/tests/data/models/dummy_model.py @@ -1,6 +1,7 @@ """Simple dummy models e.g. for count query constructor testing.""" -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel +from rdfproxy import ConfigDict class Dummy(BaseModel): diff --git a/tests/data/models/grouping_model.py b/tests/data/models/grouping_model.py index 9289f92..f3bd171 100644 --- a/tests/data/models/grouping_model.py +++ b/tests/data/models/grouping_model.py @@ -1,6 +1,7 @@ """Grouping model for RDFProxy testing.""" -from pydantic import BaseModel, ConfigDict, Field +from pydantic import BaseModel, Field +from rdfproxy import ConfigDict class GroupingSimpleModel(BaseModel): diff --git a/tests/data/models/nested_grouping_model.py b/tests/data/models/nested_grouping_model.py index 89fd0d3..ed89d11 100644 --- a/tests/data/models/nested_grouping_model.py +++ b/tests/data/models/nested_grouping_model.py @@ -1,6 +1,7 @@ """Nested grouping model for RDFProxy testing.""" -from pydantic import BaseModel, ConfigDict, Field +from pydantic import BaseModel, Field +from rdfproxy import ConfigDict class NestedGroupingSimpleModel(BaseModel): diff --git a/tests/data/parameters/model_bindings_mapper_model_bool_parameters.py b/tests/data/parameters/model_bindings_mapper_model_bool_parameters.py index 2b31d5a..5721292 100644 --- a/tests/data/parameters/model_bindings_mapper_model_bool_parameters.py +++ b/tests/data/parameters/model_bindings_mapper_model_bool_parameters.py @@ -3,7 +3,8 @@ The test cover all cases discussed in https://github.com/acdh-oeaw/rdfproxy/issues/110. """ -from pydantic import BaseModel, ConfigDict, Field, create_model +from pydantic import BaseModel, Field, create_model +from rdfproxy import ConfigDict from tests.utils._types import ModelBindingsMapperParameter diff --git a/tests/tests_adapter/test_adapter_grouped_pagination.py b/tests/tests_adapter/test_adapter_grouped_pagination.py index 4240f87..7fd68a0 100644 --- a/tests/tests_adapter/test_adapter_grouped_pagination.py +++ b/tests/tests_adapter/test_adapter_grouped_pagination.py @@ -4,8 +4,14 @@ import pytest -from pydantic import BaseModel, ConfigDict -from rdfproxy import Page, QueryParameters, SPARQLBinding, SPARQLModelAdapter +from pydantic import BaseModel +from rdfproxy import ( + ConfigDict, + Page, + QueryParameters, + SPARQLBinding, + SPARQLModelAdapter, +) binding_query = """ diff --git a/tests/tests_mapper/test_sad_path_mapper_grouped_models.py b/tests/tests_mapper/test_sad_path_mapper_grouped_models.py index 07f0e90..2a5beb5 100644 --- a/tests/tests_mapper/test_sad_path_mapper_grouped_models.py +++ b/tests/tests_mapper/test_sad_path_mapper_grouped_models.py @@ -1,6 +1,6 @@ -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel import pytest -from rdfproxy import ModelBindingsMapper +from rdfproxy import ConfigDict, ModelBindingsMapper from rdfproxy.utils._exceptions import ( InvalidGroupingKeyException, MissingModelConfigException,