Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 1 commit into from
Jan 7, 2022

Conversation

BvB93
Copy link
Member

@BvB93 BvB93 commented Jan 6, 2022

Closes #20746

The time manipulation functions (e.g. busday_count) are somewhat unique in that they unsafely cast many of their inputs to datetime64 or timedelta64 arrays. This unsafe casting means that the conventionally used npt.ArrayLike alias is not comprehensive enough to capture all valid input types, most notably excluding datetime.date and datetime.datetime.

This PR thus expands the relevant argument types with support for more datetime-based objects.

@@ -3367,7 +3367,7 @@ class busdaycalendar:
def __new__(
cls,
weekmask: ArrayLike = ...,
holidays: ArrayLike = ...,
holidays: ArrayLike | dt.date | _NestedSequence[dt.date] = ...,
Copy link
Contributor

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] ?

Copy link
Member Author

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,
Copy link
Contributor

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],
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they must be broadcastable but don't have to be of the same type, as the respective inputs are converted into datetime arrays anyway (casting rules: unsafe). As of the moment we can't do any shape typing (xref #16544), but this is something that's being worked on with the likes of PEP 646.

@charris charris merged commit d90b4c4 into numpy:main Jan 7, 2022
@charris
Copy link
Member

charris commented Jan 7, 2022

Thanks Bas.

@charris
Copy link
Member

charris commented Jan 7, 2022

Hmm, this doesn't backport cleanly. Are there other backports that should be made?

@charris
Copy link
Member

charris commented Jan 7, 2022

I suspect just keeping List in the testing imports will suffice, but I'm not sure of that.

@BvB93 BvB93 deleted the datetime branch January 7, 2022 14:04
@BvB93 BvB93 changed the title TYP: Allow time manipulation functions to accept data and timedelta objects TYP: Allow time manipulation functions to accept date and timedelta objects Jan 7, 2022
@BvB93
Copy link
Member Author

BvB93 commented Jan 7, 2022

I suspect just keeping List in the testing imports will suffice, but I'm not sure of that.

Correct, just in case I just created a backport PR at #20763

Are there other backports that should be made?

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.

@BvB93 BvB93 removed the 09 - Backport-Candidate PRs tagged should be backported label Jan 7, 2022
@BvB93 BvB93 removed this from the 1.22.1 release milestone Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Overly Strict type definition for busday_offset
3 participants