fix: return Self from Timestamp add/subtract instead of Timestamp - #1241
Merged
Conversation
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) <noreply@anthropic.com>
Deploying infrahub-sdk-python with
|
| Latest commit: |
ae7b4d3
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://80895abf.infrahub-sdk-python.pages.dev |
| Branch Preview URL: | https://ajtm-timestamp-self-return-t.infrahub-sdk-python.pages.dev |
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## stable #1241 +/- ##
==========================================
- Coverage 83.37% 82.54% -0.84%
==========================================
Files 141 138 -3
Lines 13577 12186 -1391
Branches 2195 1833 -362
==========================================
- Hits 11320 10059 -1261
+ Misses 1642 1568 -74
+ Partials 615 559 -56
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 9 files with indirect coverage changes 🚀 New features to boost your workflow:
|
ogenstad
approved these changes
Aug 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Timestamp.add(),Timestamp.subtract()andTimestamp.add_delta()all build their result withself.__class__(...), so at runtime a subclass gets an instance of itself back. The annotations claimedTimestamp.Any subclass — Infrahub's own
Timestampamong them — therefore loses its type as soon as it does arithmetic, and callers have to cast or# type: ignorebefore touching anything the subclass adds.Change
Annotate the three methods as returning
Self, which is what they already do.Selfis imported fromtyping_extensions, nottyping:typing.Selfis 3.11+ and this package supports 3.10 (requires-python = ">=3.10"). The module already importsNotRequiredfromtyping_extensions.Annotations only — no runtime behaviour changes.
Verification
uv run invoke format lint-code— clean (ruff + mypy pass; the pre-existingtyunresolved-import diagnostics in unrelated test modules are an incomplete local venv, not this change)Skipping a changelog fragment — annotation-only change.
🤖 Generated with Claude Code
Summary by cubic
Annotates Timestamp.add(), Timestamp.subtract(), and Timestamp.add_delta() to return Self instead of Timestamp, preserving subclass types. Runtime behavior is unchanged; these methods already return self.class.
Selffromtyping_extensionsto support Python 3.10.Written for commit ae7b4d3. Summary will update on new commits.