From d5264fff326f377032d7e8860a4dd2c04b1a73c5 Mon Sep 17 00:00:00 2001 From: Justin Vasel Date: Fri, 16 Aug 2024 11:53:39 -0500 Subject: [PATCH] Remove pydantic and numpy deprecated features --- snews/models/messages.py | 28 ++++++++++------------------ snews/models/timing.py | 13 +++++++------ 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/snews/models/messages.py b/snews/models/messages.py index 40c9112..cac49d4 100644 --- a/snews/models/messages.py +++ b/snews/models/messages.py @@ -8,8 +8,8 @@ # Third-party modules import numpy as np -from pydantic import (BaseModel, Field, NonNegativeFloat, ValidationError, - field_validator, model_validator) +from pydantic import (BaseModel, ConfigDict, Field, NonNegativeFloat, + ValidationError, field_validator, model_validator) # Local modules from ..__version__ import schema_version @@ -61,8 +61,7 @@ class MessageBase(BaseModel): Base class for all messages. """ - class Config: - validate_assignment = True + model_config = ConfigDict(validate_assignment=True) id: Optional[str] = Field( default=None, @@ -174,8 +173,7 @@ class DetectorMessageBase(MessageBase): Base class for all messages related to a specific detector. """ - class Config: - validate_assignment = True + model_config = ConfigDict(validate_assignment=True) detector_name: str = Field( ..., @@ -200,8 +198,7 @@ class HeartbeatMessage(DetectorMessageBase): Heartbeat detector message. """ - class Config: - validate_assignment = True + model_config = ConfigDict(validate_assignment=True) detector_status: str = Field( ..., @@ -233,8 +230,7 @@ class RetractionMessage(DetectorMessageBase): Retraction detector message. """ - class Config: - validate_assignment = True + model_config = ConfigDict(validate_assignment=True) retract_message_uid: Optional[str] = Field( default=None, @@ -275,8 +271,7 @@ class TierMessageBase(DetectorMessageBase): Tier base message """ - class Config: - validate_assignment = True + model_config = ConfigDict(validate_assignment=True) p_val: Optional[NonNegativeFloat] = Field( default=None, @@ -297,8 +292,7 @@ class TimingTierMessage(TierMessageBase): Timing tier detector message. """ - class Config: - validate_assignment = True + model_config = ConfigDict(validate_assignment=True) timing_series: List[Union[str, int]] = Field( ..., @@ -331,8 +325,7 @@ class SignificanceTierMessage(TierMessageBase): Significance tier detector message. """ - class Config: - validate_assignment = True + model_config = ConfigDict(validate_assignment=True) p_values: List[NonNegativeFloat] = Field( ..., @@ -373,8 +366,7 @@ class CoincidenceTierMessage(TierMessageBase): Coincidence tier detector message. """ - class Config: - validate_assignment = True + model_config = ConfigDict(validate_assignment=True) neutrino_time_utc: str = Field( ..., diff --git a/snews/models/timing.py b/snews/models/timing.py index 2997793..3042b98 100644 --- a/snews/models/timing.py +++ b/snews/models/timing.py @@ -9,7 +9,7 @@ # Third party imports import numpy as np -from pydantic import BaseModel, Field, field_validator +from pydantic import BaseModel, ConfigDict, Field, field_validator # Local imports from ..data.utilities import num_leap_seconds_between @@ -30,8 +30,7 @@ class PrecisionTimestamp(BaseModel, arbitrary_types_allowed=True): Supported values: "s", "ms", "us", "ns". """ - class Config: - validate_assignment = True + model_config = ConfigDict(validate_assignment=True) timestamp: Optional[Union[np.datetime64, datetime, str]] = Field( default_factory=lambda: datetime.now(UTC), @@ -61,12 +60,14 @@ def to_datetime(self): @field_validator("timestamp") def _validate_and_cast_timestamp(cls, v): + if isinstance(v, datetime) and v.tzinfo is not None: + v = v.replace(tzinfo=None) + if not isinstance(v, np.datetime64): + print(v) v = np.datetime64(v) - return v - if isinstance(v, np.datetime64): - return v + return v def __sub__(self, other) -> np.timedelta64: if isinstance(other, PrecisionTimestamp):