diff --git a/src/metldata/builtin_transformations/add_accessions/config.py b/src/metldata/builtin_transformations/add_accessions/config.py index 1ec2f0e5..38ca599e 100644 --- a/src/metldata/builtin_transformations/add_accessions/config.py +++ b/src/metldata/builtin_transformations/add_accessions/config.py @@ -17,12 +17,14 @@ """Config parameters and their defaults.""" from pydantic import Field -from pydantic_settings import BaseSettings +from pydantic_settings import BaseSettings, SettingsConfigDict class AccessionAdditionConfig(BaseSettings): """Config to add accessions to a model and associated metadata.""" + model_config = SettingsConfigDict(extra="forbid") + accession_slot_name: str = Field( "accession", description="The name of the slot to contain the accessions to." ) @@ -30,8 +32,3 @@ class AccessionAdditionConfig(BaseSettings): "The accession for an entity.", description="The description of the slot to contain the accessions to.", ) - - class Config: - """Pydantic config.""" - - extra = "forbid" diff --git a/src/metldata/builtin_transformations/custom_embeddings/config.py b/src/metldata/builtin_transformations/custom_embeddings/config.py index 135c104f..c527ad8b 100644 --- a/src/metldata/builtin_transformations/custom_embeddings/config.py +++ b/src/metldata/builtin_transformations/custom_embeddings/config.py @@ -17,7 +17,7 @@ """Models used to describe embedding profiles.""" from pydantic import Field, field_validator -from pydantic_settings import BaseSettings +from pydantic_settings import BaseSettings, SettingsConfigDict from metldata.builtin_transformations.custom_embeddings.embedding_profile import ( EmbeddingProfile, @@ -43,6 +43,8 @@ class CustomEmbeddingConfig(BaseSettings): model. """ + model_config = SettingsConfigDict(extra="forbid") + embedding_profiles: list[EmbeddingProfile] = Field( ..., description=( @@ -67,8 +69,3 @@ def check_embedding_profiles_unique( raise ValueError("Names for embedded classes must be unique.") return value - - class Config: - """Pydantic config.""" - - extra = "forbid" diff --git a/src/metldata/builtin_transformations/delete_slots/config.py b/src/metldata/builtin_transformations/delete_slots/config.py index 0c97f04e..09679b6a 100644 --- a/src/metldata/builtin_transformations/delete_slots/config.py +++ b/src/metldata/builtin_transformations/delete_slots/config.py @@ -17,12 +17,14 @@ """Config parameters and their defaults.""" from pydantic import Field -from pydantic_settings import BaseSettings +from pydantic_settings import BaseSettings, SettingsConfigDict class SlotDeletionConfig(BaseSettings): """Config containing slots to be deleted from models and associated metadata.""" + model_config = SettingsConfigDict(extra="forbid") + slots_to_delete: dict[str, list[str]] = Field( ..., description=( @@ -38,8 +40,3 @@ class SlotDeletionConfig(BaseSettings): } ], ) - - class Config: - """Pydantic config.""" - - extra = "forbid" diff --git a/src/metldata/builtin_transformations/infer_references/config.py b/src/metldata/builtin_transformations/infer_references/config.py index 06bd85fa..9362b17a 100644 --- a/src/metldata/builtin_transformations/infer_references/config.py +++ b/src/metldata/builtin_transformations/infer_references/config.py @@ -17,7 +17,7 @@ """Models used to describe all inferred references based on existing references.""" from pydantic import Field -from pydantic_settings import BaseSettings +from pydantic_settings import BaseSettings, SettingsConfigDict from metldata.builtin_transformations.infer_references.reference import ( InferredReference, @@ -31,6 +31,8 @@ class ReferenceInferenceConfig(BaseSettings): a list of InferredReferences. """ + model_config = SettingsConfigDict(extra="forbid") + inferred_ref_map: dict[str, dict[str, ReferenceDetails]] = Field( ..., description=( @@ -81,8 +83,3 @@ def inferred_references(self) -> list[InferredReference]: ) return inferred_refs - - class Config: - """Pydantic config.""" - - extra = "forbid" diff --git a/src/metldata/builtin_transformations/infer_references/reference.py b/src/metldata/builtin_transformations/infer_references/reference.py index 62cc94c4..f1b15858 100644 --- a/src/metldata/builtin_transformations/infer_references/reference.py +++ b/src/metldata/builtin_transformations/infer_references/reference.py @@ -16,7 +16,7 @@ """Reference models.""" -from pydantic import BaseModel, Field, model_validator +from pydantic import BaseModel, ConfigDict, Field, model_validator from metldata.builtin_transformations.infer_references.path.path import ReferencePath @@ -40,6 +40,8 @@ class InferredReference(ReferenceDetails): references. """ + model_config = ConfigDict(frozen=True) + source: str = Field( ..., description="The source class to which this reference should be added." ) @@ -66,8 +68,3 @@ def validate_source_and_target(cls, values): ) return values - - class Config: - """Config for this model.""" - - frozen = True diff --git a/src/metldata/builtin_transformations/merge_slots/config.py b/src/metldata/builtin_transformations/merge_slots/config.py index 96598cfe..b54eaebd 100644 --- a/src/metldata/builtin_transformations/merge_slots/config.py +++ b/src/metldata/builtin_transformations/merge_slots/config.py @@ -17,7 +17,7 @@ """Config parameters and their defaults.""" from pydantic import Field, field_validator -from pydantic_settings import BaseSettings +from pydantic_settings import BaseSettings, SettingsConfigDict from metldata.builtin_transformations.merge_slots.models import SlotMergeInstruction @@ -25,6 +25,8 @@ class SlotMergingConfig(BaseSettings): """Config containing slots to be deleted from models and associated metadata.""" + model_config = SettingsConfigDict(extra="forbid") + merge_instructions: list[SlotMergeInstruction] = Field( ..., description=( @@ -83,8 +85,3 @@ def validate_merge_instructions( ) return filtered_merge_instructions - - class Config: - """Pydantic config.""" - - extra = "forbid" diff --git a/src/metldata/model_utils/anchors.py b/src/metldata/model_utils/anchors.py index 10ab524a..3834ae12 100644 --- a/src/metldata/model_utils/anchors.py +++ b/src/metldata/model_utils/anchors.py @@ -21,7 +21,7 @@ from linkml_runtime import SchemaView from linkml_runtime.linkml_model import SlotDefinition -from pydantic import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field from metldata.model_utils.essentials import ROOT_CLASS, MetadataModel from metldata.model_utils.identifiers import get_class_identifiers @@ -42,6 +42,8 @@ class ClassNotAnchoredError(RuntimeError): class AnchorPoint(BaseModel): """A model for describing an anchor point for the specified target class.""" + model_config = ConfigDict(frozen=True) + target_class: str = Field(..., description="The name of the class to be targeted.") identifier_slot: str = Field( ..., @@ -56,11 +58,6 @@ class AnchorPoint(BaseModel): ), ) - class Config: - """Pydantic Configs.""" - - frozen = True - def check_root_slot(slot: SlotDefinition): """Make sure that the given root slot is a valid anchor point. Validates that the diff --git a/src/metldata/transform/base.py b/src/metldata/transform/base.py index d8a62092..a91a5bf8 100644 --- a/src/metldata/transform/base.py +++ b/src/metldata/transform/base.py @@ -22,7 +22,14 @@ from graphlib import CycleError, TopologicalSorter from typing import Callable, Generic, Optional, TypeVar -from pydantic import BaseModel, Field, create_model, field_validator, model_validator +from pydantic import ( + BaseModel, + ConfigDict, + Field, + create_model, + field_validator, + model_validator, +) from metldata.custom_types import Json from metldata.event_handling.models import SubmissionAnnotation @@ -114,6 +121,7 @@ class WorkflowConfig(BaseModel, ABC): class WorkflowStepBase(BaseModel, ABC): """A base class for workflow steps.""" + model_config = ConfigDict(frozen=True) description: str = Field(..., description="A description of the step.") input: Optional[str] = Field( ..., @@ -123,11 +131,6 @@ class WorkflowStepBase(BaseModel, ABC): ), ) - class Config: - """Config for the workflow step.""" - - frozen = True - class WorkflowStep(WorkflowStepBase): """A single step in a transformation workflow.""" @@ -141,6 +144,7 @@ class WorkflowStep(WorkflowStepBase): class WorkflowDefinition(BaseModel): """A definition of a transformation workflow.""" + model_config = ConfigDict(frozen=True) description: str = Field(..., description="A description of the workflow.") steps: dict[str, WorkflowStep] = Field( ..., @@ -240,8 +244,3 @@ def step_order(self) -> list[str]: return list(topological_sorter.static_order()) except CycleError as exc: raise RuntimeError("Step definitions imply a circular dependency.") from exc - - class Config: - """Config for the workflow step.""" - - frozen = True diff --git a/src/metldata/transform/handling.py b/src/metldata/transform/handling.py index b2f81150..cf4dda9d 100644 --- a/src/metldata/transform/handling.py +++ b/src/metldata/transform/handling.py @@ -16,7 +16,7 @@ """Logic for handling Transformation.""" -from pydantic import BaseModel +from pydantic import BaseModel, ConfigDict from metldata.custom_types import Json from metldata.event_handling.models import SubmissionAnnotation @@ -115,13 +115,9 @@ def transform_metadata( class ResolvedWorkflowStep(WorkflowStepBase): """A resolved workflow step contains a transformation handler.""" + model_config = ConfigDict(arbitrary_types_allowed=True) transformation_handler: TransformationHandler - class Config: - """Config for ResolvedWorkflowStep.""" - - arbitrary_types_allowed = True - class ResolvedWorkflow(WorkflowDefinition): """A resolved workflow contains a list of resolved workflow steps.""" diff --git a/tests/test_event_handling.py b/tests/test_event_handling.py index 5499dfb0..4548613d 100644 --- a/tests/test_event_handling.py +++ b/tests/test_event_handling.py @@ -21,7 +21,7 @@ import pytest from hexkit.custom_types import Ascii, JsonObject from hexkit.protocols.eventsub import EventSubscriberProtocol -from pydantic import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field from metldata.event_handling.event_handling import FileSystemEventSubscriber from tests.fixtures.event_handling import ( @@ -44,15 +44,11 @@ class ConsumedEvent(BaseModel): """Consumed event without the key.""" + model_config = ConfigDict(frozen=True) topic: str type_: str payload: str = Field(..., description="JSON string of the event payload.") - class Config: - """Pydantic model configuration.""" - - frozen = True - @pytest.mark.asyncio async def test_pub_sub_workflow(