Skip to content

TYP: some type annotations in core\tools\datetimes.py #34630

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 6 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 92 additions & 30 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
from datetime import datetime
from functools import partial
from itertools import islice
from typing import TYPE_CHECKING, Optional, TypeVar, Union
from typing import (
TYPE_CHECKING,
Callable,
List,
Optional,
Tuple,
TypeVar,
Union,
overload,
)
import warnings

import numpy as np
Expand All @@ -15,7 +24,7 @@
_guess_datetime_format,
)
from pandas._libs.tslibs.strptime import array_strptime
from pandas._typing import ArrayLike
from pandas._typing import ArrayLike, Label

from pandas.core.dtypes.common import (
ensure_object,
Expand Down Expand Up @@ -49,12 +58,10 @@
# ---------------------------------------------------------------------
# types used in annotations

ArrayConvertible = Union[list, tuple, ArrayLike, "Series"]
ArrayConvertible = Union[List, Tuple, ArrayLike, "Series"]
Scalar = Union[int, float, str]
DatetimeScalar = TypeVar("DatetimeScalar", Scalar, datetime)
DatetimeScalarOrArrayConvertible = Union[
DatetimeScalar, list, tuple, ArrayLike, "Series"
]
DatetimeScalarOrArrayConvertible = Union[DatetimeScalar, ArrayConvertible]


# ---------------------------------------------------------------------
Expand Down Expand Up @@ -123,7 +130,12 @@ def should_cache(
return do_caching


def _maybe_cache(arg, format, cache, convert_listlike):
def _maybe_cache(
arg: ArrayConvertible,
format: Optional[str],
cache: bool,
convert_listlike: Callable,
) -> "Series":
"""
Create a cache of unique dates from an array of dates

Expand Down Expand Up @@ -159,7 +171,7 @@ def _maybe_cache(arg, format, cache, convert_listlike):


def _box_as_indexlike(
dt_array: ArrayLike, utc: Optional[bool] = None, name: Optional[str] = None
dt_array: ArrayLike, utc: Optional[bool] = None, name: Label = None
) -> Index:
"""
Properly boxes the ndarray of datetimes to DatetimeIndex
Expand Down Expand Up @@ -244,15 +256,15 @@ def _return_parsed_timezone_results(result, timezones, tz, name):

def _convert_listlike_datetimes(
arg,
format,
name=None,
tz=None,
unit=None,
errors=None,
infer_datetime_format=None,
dayfirst=None,
yearfirst=None,
exact=None,
format: Optional[str],
name: Label = None,
tz: Optional[str] = None,
Copy link
Member

Choose a reason for hiding this comment

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

tz could be a tzinfo? might merit a type in pd._typing corresponding to "tzinfo or str that we can convert to one"

unit: Optional[str] = None,
errors: Optional[str] = None,
infer_datetime_format: Optional[bool] = None,
dayfirst: Optional[bool] = None,
yearfirst: Optional[bool] = None,
exact: Optional[bool] = None,
):
"""
Helper function for to_datetime. Performs the conversions of 1D listlike
Expand Down Expand Up @@ -539,19 +551,70 @@ def _adjust_to_origin(arg, origin, unit):
return arg


@overload
def to_datetime(
arg,
errors="raise",
dayfirst=False,
yearfirst=False,
utc=None,
format=None,
exact=True,
unit=None,
infer_datetime_format=False,
arg: DatetimeScalar,
errors: str = ...,
dayfirst: bool = ...,
yearfirst: bool = ...,
utc: Optional[bool] = ...,
format: Optional[str] = ...,
exact: bool = ...,
unit: Optional[str] = ...,
infer_datetime_format: bool = ...,
origin=...,
cache: bool = ...,
) -> Timestamp:
Copy link
Member

Choose a reason for hiding this comment

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

or NaT?

Copy link
Member Author

Choose a reason for hiding this comment

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

The return type here is dependent of the value of the errors parameter. so to refine the overload further requires Literal.

updated to a Union return type with NaT but needs a cast (or ignore/assert) in pandas/io/excel/_odfreader.py

also changed from Timestamp to DatetimeScalar since with error="ignore" returns arg value

Copy link
Member

Choose a reason for hiding this comment

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

so to refine the overload further requires Literal

FWIW I think (separate to this PR) that we will need to start vendoring parts of typing_extensions and this would be a good candidate. I also think Protocol is necessary for us to unpin where we are at now particularly with our mixin classes

Copy link
Member Author

Choose a reason for hiding this comment

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

+1 for vendoring typing_extensions

...


@overload
def to_datetime(
arg: "Series",
errors: str = ...,
dayfirst: bool = ...,
yearfirst: bool = ...,
utc: Optional[bool] = ...,
format: Optional[str] = ...,
exact: bool = ...,
unit: Optional[str] = ...,
infer_datetime_format: bool = ...,
origin=...,
cache: bool = ...,
) -> "Series":
...


@overload
def to_datetime(
arg: Union[List, Tuple],
errors: str = ...,
dayfirst: bool = ...,
yearfirst: bool = ...,
utc: Optional[bool] = ...,
format: Optional[str] = ...,
exact: bool = ...,
unit: Optional[str] = ...,
infer_datetime_format: bool = ...,
origin=...,
cache: bool = ...,
) -> DatetimeIndex:
...


def to_datetime(
arg: DatetimeScalarOrArrayConvertible,
errors: str = "raise",
dayfirst: bool = False,
yearfirst: bool = False,
utc: Optional[bool] = None,
format: Optional[str] = None,
exact: bool = True,
unit: Optional[str] = None,
infer_datetime_format: bool = False,
origin="unix",
cache=True,
):
cache: bool = True,
) -> Union[DatetimeIndex, "Series", Timestamp]:
"""
Convert argument to datetime.

Expand Down Expand Up @@ -746,8 +809,7 @@ def to_datetime(
if not cache_array.empty:
result = _convert_and_box_cache(arg, cache_array, name=arg.name)
else:
convert_listlike = partial(convert_listlike, name=arg.name)
result = convert_listlike(arg, format)
Copy link
Member

Choose a reason for hiding this comment

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

+1 for avoiding the partial here

result = convert_listlike(arg, format, name=arg.name)
elif is_list_like(arg):
try:
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ def __add__(self, other):
pd.array([1, 3, 2], dtype="int64"),
pd.array([1, 10, 0], dtype="Sparse[int]"),
pd.to_datetime(["2000", "2010", "2001"]),
pd.to_datetime(["2000", "2010", "2001"]).tz_localize("CET"),
pd.to_datetime(["2000", "2010", "2001"]).to_period(freq="D"),
pd.to_datetime(["2000", "2010", "2001"]).tz_localize("CET"), # type: ignore
pd.to_datetime(["2000", "2010", "2001"]).to_period(freq="D"), # type: ignore
Copy link
Member Author

Choose a reason for hiding this comment

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

$ mypy pandas
pandas\tests\series\test_ufunc.py:258: error: "DatetimeIndex" has no attribute "tz_localize"
pandas\tests\series\test_ufunc.py:259: error: "DatetimeIndex" has no attribute "to_period"
Found 2 errors in 1 file (checked 1018 source files)

Copy link
Member

Choose a reason for hiding this comment

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

IIRC this happens bc we use inherit_names on DatetimeIndex. I'd be OK with pinning the inherited methods in a way mypy gets even if its a little more verbose if you think its worthwhile

],
)
def test_reduce(values):
Expand Down