-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
TYP: Allow time manipulation functions to accept date
and timedelta
objects
#20750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -3367,7 +3367,7 @@ class busdaycalendar: | |||
def __new__( | |||
cls, | |||
weekmask: ArrayLike = ..., | |||
holidays: ArrayLike = ..., | |||
holidays: ArrayLike | dt.date | _NestedSequence[dt.date] = ..., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not create a type alias for ArrayLike | dt.date | _NestedSequence[dt.date]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering they're only applicable for very limited (and niche) set of functions, I'm not sure it's worthwhile to introduce aliases here. Line length is also not an issue here.
@@ -812,129 +813,129 @@ def datetime_data( | |||
|
|||
@overload | |||
def busday_count( # type: ignore[misc] | |||
begindates: _ScalarLike_co, | |||
enddates: _ScalarLike_co, | |||
begindates: _ScalarLike_co | dt.date, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto for a type alias for: _ScalarLike_co | dt.date
busdaycal: None | busdaycalendar = ..., | ||
out: None = ..., | ||
) -> datetime64: ... | ||
@overload | ||
def busday_offset( # type: ignore[misc] | ||
dates: ArrayLike, | ||
offsets: ArrayLike, | ||
dates: ArrayLike | dt.date | _NestedSequence[dt.date], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should dates
and offsets
be of the same type here? That might be overkill but something like:
NTD = TypeVar("NTD", bound=ArrayLike | dt.date | _NestedSequence[dt.date])
....
dates: NTD
offsets: NTD
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Bas. |
Hmm, this doesn't backport cleanly. Are there other backports that should be made? |
I suspect just keeping |
data
and timedelta
objectsdate
and timedelta
objects
Correct, just in case I just created a backport PR at #20763
There is a small fix for #20756 that I'll make in a bit, but that's about it for now as far as typing-based backport candidates are concerned. |
Closes #20746
The time manipulation functions (e.g.
busday_count
) are somewhat unique in that they unsafely cast many of their inputs todatetime64
ortimedelta64
arrays. This unsafe casting means that the conventionally usednpt.ArrayLike
alias is not comprehensive enough to capture all valid input types, most notably excludingdatetime.date
anddatetime.datetime
.This PR thus expands the relevant argument types with support for more
datetime
-based objects.