From 91bcbcb816b0d9ddbd7a4aa88b70559bd6d4d7fb Mon Sep 17 00:00:00 2001 From: Aaron McCarty Date: Thu, 13 Aug 2026 08:22:33 -0500 Subject: [PATCH] fix: return Self from Timestamp add/subtract instead of Timestamp (#1241) add(), subtract() and add_delta() all construct self.__class__, so a subclass gets an instance of itself back at runtime. The annotations said Timestamp, so subclasses (such as Infrahub's own Timestamp) had to cast before touching anything they add on top. Self comes from typing_extensions rather than typing, since typing.Self is 3.11+ and the SDK supports 3.10. Co-authored-by: Claude Opus 5 (1M context) --- infrahub_sdk/timestamp.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/infrahub_sdk/timestamp.py b/infrahub_sdk/timestamp.py index fd69122e5..e826021a8 100644 --- a/infrahub_sdk/timestamp.py +++ b/infrahub_sdk/timestamp.py @@ -5,7 +5,7 @@ from datetime import datetime, timezone from typing import Literal, TypedDict -from typing_extensions import NotRequired +from typing_extensions import NotRequired, Self from whenever import Date, Instant, OffsetDateTime, PlainDateTime, Time, ZonedDateTime from .exceptions import TimestampFormatError @@ -147,7 +147,7 @@ def __ge__(self, other: object) -> bool: def __hash__(self) -> int: return hash(self.to_string()) - def add_delta(self, hours: int = 0, minutes: int = 0, seconds: int = 0, microseconds: int = 0) -> Timestamp: + def add_delta(self, hours: int = 0, minutes: int = 0, seconds: int = 0, microseconds: int = 0) -> Self: warnings.warn( "add_delta() is deprecated. Use add() instead.", UserWarning, @@ -168,7 +168,7 @@ def add( microseconds: float = 0, nanoseconds: int = 0, disambiguate: Literal["compatible"] = "compatible", - ) -> Timestamp: + ) -> Self: return self.__class__( self._obj.add( years=years, @@ -198,7 +198,7 @@ def subtract( microseconds: float = 0, nanoseconds: int = 0, disambiguate: Literal["compatible"] = "compatible", - ) -> Timestamp: + ) -> Self: return self.__class__( self._obj.subtract( years=years,