From c2767d27ed824db4af3d4b30ce063436873a6191 Mon Sep 17 00:00:00 2001 From: Nicolas Drebenstedt Date: Wed, 14 Feb 2024 11:45:16 +0100 Subject: [PATCH] Move stableTargetId typehint to extractedData --- mex/common/models/base.py | 17 ++--------------- mex/common/models/extracted_data.py | 16 +++++++++++++++- mex/common/public_api/transform.py | 7 ++++++- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/mex/common/models/base.py b/mex/common/models/base.py index 67664e73..2ef920d6 100644 --- a/mex/common/models/base.py +++ b/mex/common/models/base.py @@ -214,26 +214,13 @@ def __str__(self) -> str: class MExModel(BaseModel): """Abstract base model for extracted data, merged item and rule set classes. - This class defines an `identifier` field and gives type hints for `stableTargetId` - and the class variable `entityType`. + This class defines an `identifier` field and gives a type hint for the frozen class + variable `entityType`. """ model_config = ConfigDict(extra="forbid") if TYPE_CHECKING: - # Sometimes multiple primary sources describe the same activity, resource, etc. - # and a complete metadata item can only be created by merging these fragments. - # The `stableTargetID` is part of all models in `mex.common.models` to allow - # MEx to identify which extracted items describe the same thing and should be - # merged to create a complete metadata item. - # The name might be a bit misleading (also due to historical reasons), but the - # "stability" is only guaranteed for one "real world" or "digital world" thing - # having the same ID in MEx over time. But not as a guarantee, that the same - # metadata sources contribute to the complete metadata item. - # Because we anticipate that items have to be merged, the `stableTargetID` is - # also used as the foreign key for all fields containing references. - stableTargetId: Any - # We add the entityType as a final class variable to all `MExModel` subclasses. # This helps with assigning the correct class when reading raw JSON entities. # Simple duck-typing would not work, because some entity types have overlapping diff --git a/mex/common/models/extracted_data.py b/mex/common/models/extracted_data.py index 1a6b50f2..7450ad77 100644 --- a/mex/common/models/extracted_data.py +++ b/mex/common/models/extracted_data.py @@ -1,4 +1,4 @@ -from typing import Annotated, Any +from typing import TYPE_CHECKING, Annotated, Any from pydantic import Field, model_validator, validate_call @@ -23,6 +23,20 @@ class ExtractedData(MExModel): See below, for a full description. """ + if TYPE_CHECKING: + # Sometimes multiple primary sources describe the same activity, resource, etc. + # and a complete metadata item can only be created by merging these fragments. + # The `stableTargetID` is part of all models in `mex.common.models` to allow + # MEx to identify which extracted items describe the same thing and should be + # merged to create a complete metadata item. + # The name might be a bit misleading (also due to historical reasons), but the + # "stability" is only guaranteed for one "real world" or "digital world" thing + # having the same ID in MEx over time. But not as a guarantee, that the same + # metadata sources contribute to the complete metadata item. + # Because we anticipate that items have to be merged, the `stableTargetID` is + # also used as the foreign key for all fields containing references. + stableTargetId: Any + hadPrimarySource: Annotated[ PrimarySourceID, Field( diff --git a/mex/common/public_api/transform.py b/mex/common/public_api/transform.py index 0d2bbdbe..4ec52321 100644 --- a/mex/common/public_api/transform.py +++ b/mex/common/public_api/transform.py @@ -6,6 +6,7 @@ MERGED_MODEL_CLASSES_BY_NAME, MExModel, ) +from mex.common.models.extracted_data import ExtractedData from mex.common.public_api.models import ( PublicApiField, PublicApiFieldValueTypes, @@ -54,7 +55,11 @@ def transform_mex_model_to_public_api_item(model: MExModel) -> PublicApiItem: ) return PublicApiItem( entityType=model.__class__.__name__, - businessId=model.stableTargetId, + businessId=( + model.stableTargetId + if isinstance(model, ExtractedData) + else model.identifier + ), values=api_values, )