Skip to content

typing: align date/datetime dunder annotations with typeshed#184

Merged
slashmili merged 1 commit into
slashmili:mainfrom
5j9:sub-type-hint
Jul 13, 2026
Merged

typing: align date/datetime dunder annotations with typeshed#184
slashmili merged 1 commit into
slashmili:mainfrom
5j9:sub-type-hint

Conversation

@5j9

@5j9 5j9 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

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.

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
slashmili merged commit 3d0884d into slashmili:main Jul 13, 2026
13 checks passed
@slashmili

Copy link
Copy Markdown
Owner

Thanks!

@slashmili

Copy link
Copy Markdown
Owner

Published as 6.0.0 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants