typing: align date/datetime dunder annotations with typeshed#184
Merged
Conversation
Improve type annotations for date and datetime special methods to more closely match the stdlib/typeshed API.[1] - add overloads for __sub__ to accurately model supported operands - annotate unsupported date - datetime subtraction as NoReturn - widen __eq__ to accept object - use positional-only parameters where appropriate - use the timedelta alias consistently - call self.fromgregorian() instead of hardcoding the base class avoiding surprising behavior if these classes are subclassed - suppress the intentional override incompatibilities in datetime, matching the stdlib API This improves type checking while mostly preserving the existing runtime behavior. This change marks the dunder method parameters as positional-only to match the stdlib API. While this technically breaks niche keyword calls like obj.__add__(timedelta=x), it aligns the signatures with operator semantics and typeshed. [1]: https://github.com/python/typeshed/blob/main/stdlib/datetime.pyi
slashmili
approved these changes
Jul 13, 2026
Owner
|
Thanks! |
Owner
|
Published as 6.0.0 🚀 |
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.
Improve type annotations for date and datetime special methods to more closely match the stdlib/typeshed API.1
__sub__to accurately model supported operandsNoReturn__eq__to accept objecttimedeltaalias consistentlyself.fromgregorian()instead of hardcoding the base class avoiding surprising behavior if these classes are subclassedThis improves type checking while mostly preserving the existing runtime behavior.
This change marks the dunder method parameters as positional-only to match the stdlib API. While this technically breaks niche keyword calls like
obj.__add__(timedelta=x), it aligns the signatures with operator semantics and typeshed.