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

Conversation

simonjayhawkins
Copy link
Member

No description provided.

@simonjayhawkins simonjayhawkins added the Typing type annotations, mypy/pyright type checking label Jun 7, 2020
Comment on lines 258 to 259
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


@overload
def to_datetime(
arg: Union[list, tuple],
Copy link
Member

Choose a reason for hiding this comment

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

For consistency in code base can you use List, Tuple here?

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch. cut and paste from the alias. will update the alias too.

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

Lgtm - I find these overloads really insightful

@WillAyd WillAyd added this to the 1.1 milestone Jun 7, 2020
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"

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

@@ -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

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm. merge when ready.

@simonjayhawkins simonjayhawkins merged commit bbb89ca into pandas-dev:master Jun 10, 2020
@simonjayhawkins simonjayhawkins deleted the to_datetime-typing branch June 10, 2020 09:25
willpeppo pushed a commit to willpeppo/pandas that referenced this pull request Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants