Skip to content

TYP: libperiod #40990

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
May 5, 2021
Merged

TYP: libperiod #40990

merged 6 commits into from
May 5, 2021

Conversation

jbrockmendel
Copy link
Member

  • closes #xxxx
  • tests added / passed
  • Ensure all linting tests pass, see here for how to run them
  • whatsnew entry

@jbrockmendel jbrockmendel added the Typing type annotations, mypy/pyright type checking label Apr 17, 2021
Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @jbrockmendel

new_fill = self._scalar_type(fill_value)
# error: Incompatible types in assignment (expression has type
# "Union[Period, Any, Timedelta]", variable has type "Period")
new_fill = self._scalar_type(fill_value) # type: ignore[assignment]
Copy link
Member

Choose a reason for hiding this comment

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

new_fill is inferred as Period from the first assignment to new_fill above. https://mypy.readthedocs.io/en/stable/type_inference_and_annotations.html#type-inference

remove ignore by adding add new_fill: DatetimeLikeScalar before initial assignment

# error: Argument 1 of "_unbox_scalar" is incompatible with supertype
# "DatetimeLikeArrayMixin"; supertype defines the argument type as
# "Union[Union[Period, Any, Timedelta], NaTType]"
def _unbox_scalar( # type: ignore[override]
Copy link
Member

Choose a reason for hiding this comment

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

I think DatetimeLikeArrayMixin needs to be Generic[DatetimeLikeScalarT].

Copy link
Member Author

Choose a reason for hiding this comment

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

Trying this i get error: Free type variable expected in Generic[...] [misc]

I'll troubleshoot this a bit

Copy link
Member

Choose a reason for hiding this comment

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

ok to leave as a 'fix later' ignore so we can start benefitting straight away from the added types

Copy link
Member Author

Choose a reason for hiding this comment

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

sounds good

if value is NaT:
return np.int64(value.value)
# error: Item "Period" of "Union[Period, NaTType]" has no attribute "value"
return np.int64(value.value) # type: ignore[union-attr]
Copy link
Member

Choose a reason for hiding this comment

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

This is a false positive. if not planning to 'fix later', can either cast or add a reference to a mypy issue.

maybe python/mypy#7642, although AFAICT the discussion there is more around a singleton class (i.e. final, not subclassed) rather than a singleton instance.

however, from python/mypy#7642 (comment)

You're trying to introduce a singleton value other than None, and you want the type checker to understand code that excludes the singleton value.

I think this is a reasonable request.

so probably an appropriate issue to discuss this.

@@ -910,7 +918,7 @@ def raise_on_incompatible(left, right):


def period_array(
data: Sequence[Period | None] | AnyArrayLike,
data: Sequence[Period | str | None] | AnyArrayLike,
Copy link
Member

Choose a reason for hiding this comment

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

and from the docstring pd.NaT (np.nan also seems to be acceptable but not pd.NA) and also datetime.datetime

>>> period_array(
...     [pd.Period("2017", freq="A"), pd.Period("2018", freq="A"), datetime.datetime.now()]
... )
<PeriodArray>
['2017', '2018', '2021']
Length: 3, dtype: period[A-DEC]
>>> 

@@ -2539,7 +2539,7 @@ cdef int64_t _ordinal_from_fields(int year, int month, quarter, int day,
minute, second, 0, 0, base)


def validate_end_alias(how):
def validate_end_alias(how: str) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

returns Literal["S", "E"] or raises?

) -> np.ndarray: ... # np.ndarray[np.int64]

def get_period_field_arr(
field: str,
Copy link
Member

Choose a reason for hiding this comment

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

do we need a alias to a Literal for this to match accessor type is the cython code.


def from_ordinals(
values: np.ndarray, # const int64_t[:]
freq,
Copy link
Member

Choose a reason for hiding this comment

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

str, tuple, datetime.timedelta, DateOffset or None

same as type of freq to to_offset in pandas/_libs/tslibs/offsets.pyx, maybe need an alias since used in several places


def extract_ordinals(
values: np.ndarray, # np.ndarray[object]
freq,
Copy link
Member

Choose a reason for hiding this comment

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

same as above and also int?


def strftime(self, fmt: str) -> str: ...

def to_timestamp(self, freq=..., how=..., tz=...) -> Timestamp: ...
Copy link
Member

Choose a reason for hiding this comment

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

missed types on this one.

@property
def start_time(self) -> Timestamp: ...

def __sub__(self, other): ...
Copy link
Member

Choose a reason for hiding this comment

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

return type

Copy link
Member Author

Choose a reason for hiding this comment

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

annotating as Period for now. for the Technically Correct longer term, do we annotate that it can return NotImplemented?

Copy link
Member

Choose a reason for hiding this comment

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

sure. I don't think NotImplemented is a valid type.

(when mypy is checking the function NotImplemented needs to be compatible with any return type to avoid false positives, but that's not relevant for stubs)

@simonjayhawkins simonjayhawkins added this to the 1.3 milestone Apr 27, 2021
@simonjayhawkins
Copy link
Member

Thanks @jbrockmendel for the updates. some new mypy failures.

@jbrockmendel
Copy link
Member Author

mypy complaints fixed; new CI failure is 3.10, looks unrelated

@jreback jreback merged commit c2c2ce5 into pandas-dev:master May 5, 2021
@jreback
Copy link
Contributor

jreback commented May 5, 2021

thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the typ-period branch May 5, 2021 14:47
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
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.

3 participants