Skip to content

Commit

Permalink
Use model_config instead of class Config
Browse files Browse the repository at this point in the history
  • Loading branch information
TheByronHimes committed Nov 16, 2023
1 parent 0375983 commit 5e4e66d
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 65 deletions.
9 changes: 3 additions & 6 deletions src/metldata/builtin_transformations/add_accessions/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
"""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."
)
accession_slot_description: str = Field(
"The accession for an entity.",
description="The description of the slot to contain the accessions to.",
)

class Config:
"""Pydantic config."""

extra = "forbid"
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -43,6 +43,8 @@ class CustomEmbeddingConfig(BaseSettings):
model.
"""

model_config = SettingsConfigDict(extra="forbid")

embedding_profiles: list[EmbeddingProfile] = Field(
...,
description=(
Expand All @@ -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"
9 changes: 3 additions & 6 deletions src/metldata/builtin_transformations/delete_slots/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=(
Expand All @@ -38,8 +40,3 @@ class SlotDeletionConfig(BaseSettings):
}
],
)

class Config:
"""Pydantic config."""

extra = "forbid"
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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=(
Expand Down Expand Up @@ -81,8 +83,3 @@ def inferred_references(self) -> list[InferredReference]:
)

return inferred_refs

class Config:
"""Pydantic config."""

extra = "forbid"
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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."
)
Expand All @@ -66,8 +68,3 @@ def validate_source_and_target(cls, values):
)

return values

class Config:
"""Config for this model."""

frozen = True
9 changes: 3 additions & 6 deletions src/metldata/builtin_transformations/merge_slots/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
"""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


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=(
Expand Down Expand Up @@ -83,8 +85,3 @@ def validate_merge_instructions(
)

return filtered_merge_instructions

class Config:
"""Pydantic config."""

extra = "forbid"
9 changes: 3 additions & 6 deletions src/metldata/model_utils/anchors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
...,
Expand All @@ -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
Expand Down
21 changes: 10 additions & 11 deletions src/metldata/transform/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
...,
Expand All @@ -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."""
Expand All @@ -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(
...,
Expand Down Expand Up @@ -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
8 changes: 2 additions & 6 deletions src/metldata/transform/handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
8 changes: 2 additions & 6 deletions tests/test_event_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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(
Expand Down

0 comments on commit 5e4e66d

Please sign in to comment.