-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
#25790 Updating type hints to Python3 syntax in pandas/core/array #25829
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
Changes from 2 commits
a0b8c4c
46cc909
7acb89a
d9c53ab
e73fe18
545d38d
abc09fa
c37ef88
ea9c2b3
20b63e4
cf774d7
50db810
92490a8
441f4bd
33db1ee
23aba8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,8 +58,7 @@ def _get_attributes_dict(self): | |
return {k: getattr(self, k, None) for k in self._attributes} | ||
|
||
@property | ||
def _scalar_type(self): | ||
# type: () -> Union[type, Tuple[type]] | ||
def _scalar_type(self) -> Union[type, Tuple[type]]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use Type consistently |
||
"""The scalar associated with this datelike | ||
|
||
* PeriodArray : Period | ||
|
@@ -68,8 +67,10 @@ def _scalar_type(self): | |
""" | ||
raise AbstractMethodError(self) | ||
|
||
def _scalar_from_string(self, value): | ||
# type: (str) -> Union[Period, Timestamp, Timedelta, NaTType] | ||
def _scalar_from_string( | ||
self, | ||
value: str, | ||
) -> Union[Period, Timestamp, Timedelta, NaTType]: | ||
""" | ||
Construct a scalar type from a string. | ||
|
||
|
@@ -89,8 +90,10 @@ def _scalar_from_string(self, value): | |
""" | ||
raise AbstractMethodError(self) | ||
|
||
def _unbox_scalar(self, value): | ||
# type: (Union[Period, Timestamp, Timedelta, NaTType]) -> int | ||
def _unbox_scalar( | ||
self, | ||
value: Union[Period, Timestamp, Timedelta, NaTType], | ||
) -> int: | ||
""" | ||
Unbox the integer value of a scalar `value`. | ||
|
||
|
@@ -109,8 +112,10 @@ def _unbox_scalar(self, value): | |
""" | ||
raise AbstractMethodError(self) | ||
|
||
def _check_compatible_with(self, other): | ||
# type: (Union[Period, Timestamp, Timedelta, NaTType]) -> None | ||
def _check_compatible_with( | ||
self, | ||
other: Union[Period, Timestamp, Timedelta, NaTType], | ||
) -> None: | ||
""" | ||
Verify that `self` and `other` are compatible. | ||
|
||
|
@@ -350,8 +355,7 @@ def __iter__(self): | |
return (self._box_func(v) for v in self.asi8) | ||
|
||
@property | ||
def asi8(self): | ||
# type: () -> np.ndarray | ||
def asi8(self) -> np.ndarray: | ||
""" | ||
Integer representation of the values. | ||
|
||
|
@@ -402,8 +406,7 @@ def shape(self): | |
return (len(self),) | ||
|
||
@property | ||
def size(self): | ||
# type: () -> int | ||
def size(self) -> int: | ||
"""The number of elements in this array.""" | ||
return np.prod(self.shape) | ||
|
||
|
@@ -461,10 +464,9 @@ def __getitem__(self, key): | |
|
||
def __setitem__( | ||
self, | ||
key, # type: Union[int, Sequence[int], Sequence[bool], slice] | ||
value, # type: Union[NaTType, Any, Sequence[Any]] | ||
): | ||
# type: (...) -> None | ||
key: Union[int, Sequence[int], Sequence[bool], slice], | ||
value: Union[NaTType, Any, Sequence[Any]] | ||
) -> None: | ||
# I'm fudging the types a bit here. "Any" above really depends | ||
# on type(self). For PeriodArray, it's Period (or stuff coercible | ||
# to a period in from_sequence). For DatetimeArray, it's Timestamp... | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,8 +183,12 @@ def _simple_new(cls, values, freq=None, **kwargs): | |
return cls(values, freq=freq, **kwargs) | ||
|
||
@classmethod | ||
def _from_sequence(cls, scalars, dtype=None, copy=False): | ||
# type: (Sequence[Optional[Period]], PeriodDtype, bool) -> PeriodArray | ||
def _from_sequence( | ||
cls, | ||
scalars: Sequence[Optional[Period]], | ||
dtype: PeriodDtype = None, | ||
copy: bool = False, | ||
) -> 'PeriodArray': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ABCPeriodArray |
||
if dtype: | ||
freq = dtype.freq | ||
else: | ||
|
@@ -246,8 +250,7 @@ def _generate_range(cls, start, end, periods, freq, fields): | |
# ----------------------------------------------------------------- | ||
# DatetimeLike Interface | ||
|
||
def _unbox_scalar(self, value): | ||
# type: (Union[Period, NaTType]) -> int | ||
def _unbox_scalar(self, value: Union[Period, NaTType]) -> int: | ||
if value is NaT: | ||
return value.value | ||
elif isinstance(value, self._scalar_type): | ||
|
@@ -258,8 +261,7 @@ def _unbox_scalar(self, value): | |
raise ValueError("'value' should be a Period. Got '{val}' instead." | ||
.format(val=value)) | ||
|
||
def _scalar_from_string(self, value): | ||
# type: (str) -> Period | ||
def _scalar_from_string(self, value: str) -> Period: | ||
return Period(value, freq=self.freq) | ||
|
||
def _check_compatible_with(self, other): | ||
|
@@ -540,14 +542,9 @@ def _sub_period(self, other): | |
@Appender(dtl.DatetimeLikeArrayMixin._addsub_int_array.__doc__) | ||
def _addsub_int_array( | ||
self, | ||
other, # type: Union[ExtensionArray, np.ndarray[int]] | ||
op # type: Callable[Any, Any] | ||
): | ||
# type: (...) -> PeriodArray | ||
|
||
# TODO: ABCIndexClass is a valid type for other but had to be excluded | ||
# due to length of Py2 compatability comment; add back in once migrated | ||
# to Py3 syntax | ||
other: Union[ExtensionArray, np.ndarray, ABCIndexClass], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason for removing the type for ndarray? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. During my testing at the time (before you added mypy.ini and the rest of the type checking framework to the project), mypy was throwing an error that suggested that ndarrays couldn't take a subscript, so I dropped it. I probably had something in my testing environment set up incorrectly (or maybe more strictly, not sure), because it doesn't throw that error with the current mypy.ini minus the blacklist. Will add it back. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait, it wasn't mypy.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. Kind of strange this throws an error now but not before. Something to revisit later |
||
op: Callable[[Any], Any] | ||
) -> 'PeriodArray': | ||
assert op in [operator.add, operator.sub] | ||
if op is operator.sub: | ||
other = -other | ||
|
@@ -716,8 +713,11 @@ def _raise_on_incompatible(left, right): | |
# ------------------------------------------------------------------- | ||
# Constructor Helpers | ||
|
||
def period_array(data, freq=None, copy=False): | ||
# type: (Sequence[Optional[Period]], Optional[Tick], bool) -> PeriodArray | ||
def period_array( | ||
data: Sequence[Optional[Period]], | ||
freq: Optional[Tick] = None, | ||
copy: bool = False, | ||
) -> PeriodArray: | ||
""" | ||
Construct a new PeriodArray from a sequence of Period scalars. | ||
|
||
|
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.
Might as well use the ABC here and throughout module