Skip to content

Commit

Permalink
CLN,TYP Remove string return annotations (pandas-dev#39174)
Browse files Browse the repository at this point in the history
* remove string return annotations

* fixup conflict

* fixup merge conflict
  • Loading branch information
MarcoGorelli authored Jan 16, 2021
1 parent a3b5c49 commit f51547c
Show file tree
Hide file tree
Showing 59 changed files with 314 additions and 248 deletions.
5 changes: 3 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@ repos:
language: pygrep
types_or: [python, cython]
- id: unwanted-typing
name: Check for use of comment-based annotation syntax and missing error codes
name: Check for outdated annotation syntax and missing error codes
entry: |
(?x)
\#\ type:\ (?!ignore)|
\#\ type:\s?ignore(?!\[)
\#\ type:\s?ignore(?!\[)|
\)\ ->\ \"
language: pygrep
types: [python]
- id: np-bool
Expand Down
5 changes: 3 additions & 2 deletions pandas/compat/pickle_compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Support pre-0.12 series pickle compatibility.
"""
from __future__ import annotations

import contextlib
import copy
Expand Down Expand Up @@ -64,7 +65,7 @@ class _LoadSparseSeries:
# https://github.com/python/mypy/issues/1020
# error: Incompatible return type for "__new__" (returns "Series", but must return
# a subtype of "_LoadSparseSeries")
def __new__(cls) -> "Series": # type: ignore[misc]
def __new__(cls) -> Series: # type: ignore[misc]
from pandas import Series

warnings.warn(
Expand All @@ -82,7 +83,7 @@ class _LoadSparseFrame:
# https://github.com/python/mypy/issues/1020
# error: Incompatible return type for "__new__" (returns "DataFrame", but must
# return a subtype of "_LoadSparseFrame")
def __new__(cls) -> "DataFrame": # type: ignore[misc]
def __new__(cls) -> DataFrame: # type: ignore[misc]
from pandas import DataFrame

warnings.warn(
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import numbers
from typing import TYPE_CHECKING, List, Optional, Tuple, Type, Union
import warnings
Expand Down Expand Up @@ -93,7 +95,7 @@ def _is_numeric(self) -> bool:

def __from_arrow__(
self, array: Union["pyarrow.Array", "pyarrow.ChunkedArray"]
) -> "BooleanArray":
) -> BooleanArray:
"""
Construct BooleanArray from pyarrow Array/ChunkedArray.
"""
Expand Down Expand Up @@ -276,7 +278,7 @@ def dtype(self) -> BooleanDtype:
@classmethod
def _from_sequence(
cls, scalars, *, dtype: Optional[Dtype] = None, copy: bool = False
) -> "BooleanArray":
) -> BooleanArray:
if dtype:
assert dtype == "boolean"
values, mask = coerce_to_array(scalars, copy=copy)
Expand All @@ -291,7 +293,7 @@ def _from_sequence_of_strings(
copy: bool = False,
true_values: Optional[List[str]] = None,
false_values: Optional[List[str]] = None,
) -> "BooleanArray":
) -> BooleanArray:
true_values_union = cls._TRUE_VALUES.union(true_values or [])
false_values_union = cls._FALSE_VALUES.union(false_values or [])

Expand Down
8 changes: 5 additions & 3 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from csv import QUOTE_NONNUMERIC
from functools import partial
import operator
Expand Down Expand Up @@ -740,7 +742,7 @@ def _set_categories(self, categories, fastpath=False):

self._dtype = new_dtype

def _set_dtype(self, dtype: CategoricalDtype) -> "Categorical":
def _set_dtype(self, dtype: CategoricalDtype) -> Categorical:
"""
Internal method for directly updating the CategoricalDtype
Expand Down Expand Up @@ -1740,7 +1742,7 @@ def fillna(self, value=None, method=None, limit=None):
def _ndarray(self) -> np.ndarray:
return self._codes

def _from_backing_data(self, arr: np.ndarray) -> "Categorical":
def _from_backing_data(self, arr: np.ndarray) -> Categorical:
return self._constructor(arr, dtype=self.dtype, fastpath=True)

def _box_func(self, i: int):
Expand Down Expand Up @@ -2160,7 +2162,7 @@ def _concat_same_type(

# ------------------------------------------------------------------

def _encode_with_my_categories(self, other: "Categorical") -> "Categorical":
def _encode_with_my_categories(self, other: "Categorical") -> Categorical:
"""
Re-encode another categorical using this Categorical's categories.
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from datetime import datetime, time, timedelta, tzinfo
from typing import Optional, Union, cast
import warnings
Expand Down Expand Up @@ -291,7 +293,7 @@ def __init__(self, values, dtype=DT64NS_DTYPE, freq=None, copy=False):
@classmethod
def _simple_new(
cls, values, freq: Optional[BaseOffset] = None, dtype=DT64NS_DTYPE
) -> "DatetimeArray":
) -> DatetimeArray:
assert isinstance(values, np.ndarray)
if values.dtype != DT64NS_DTYPE:
assert values.dtype == "i8"
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/arrays/floating.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import List, Optional, Tuple, Type
import warnings

Expand Down Expand Up @@ -242,14 +244,14 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
@classmethod
def _from_sequence(
cls, scalars, *, dtype=None, copy: bool = False
) -> "FloatingArray":
) -> FloatingArray:
values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy)
return FloatingArray(values, mask)

@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype=None, copy: bool = False
) -> "FloatingArray":
) -> FloatingArray:
scalars = to_numeric(strings, errors="raise")
return cls._from_sequence(scalars, dtype=dtype, copy=copy)

Expand Down
6 changes: 4 additions & 2 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Dict, List, Optional, Tuple, Type
import warnings

Expand Down Expand Up @@ -303,14 +305,14 @@ def __abs__(self):
@classmethod
def _from_sequence(
cls, scalars, *, dtype: Optional[Dtype] = None, copy: bool = False
) -> "IntegerArray":
) -> IntegerArray:
values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy)
return IntegerArray(values, mask)

@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype: Optional[Dtype] = None, copy: bool = False
) -> "IntegerArray":
) -> IntegerArray:
scalars = to_numeric(strings, errors="raise")
return cls._from_sequence(scalars, dtype=dtype, copy=copy)

Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import operator
from operator import le, lt
import textwrap
Expand Down Expand Up @@ -861,7 +863,7 @@ def copy(self: IntervalArrayT) -> IntervalArrayT:
def isna(self) -> np.ndarray:
return isna(self._left)

def shift(self, periods: int = 1, fill_value: object = None) -> "IntervalArray":
def shift(self, periods: int = 1, fill_value: object = None) -> IntervalArray:
if not len(self) or periods == 0:
return self.copy()

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def factorize(self, na_sentinel: int = -1) -> Tuple[np.ndarray, ExtensionArray]:
uniques = type(self)(uniques, np.zeros(len(uniques), dtype=bool))
return codes, uniques

def value_counts(self, dropna: bool = True) -> "Series":
def value_counts(self, dropna: bool = True) -> Series:
"""
Returns a Series containing counts of each unique value.
Expand Down
10 changes: 6 additions & 4 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import numbers
from typing import Optional, Tuple, Type, Union

Expand Down Expand Up @@ -75,7 +77,7 @@ def _is_boolean(self) -> bool:
return self.kind == "b"

@classmethod
def construct_from_string(cls, string: str) -> "PandasDtype":
def construct_from_string(cls, string: str) -> PandasDtype:
try:
dtype = np.dtype(string)
except TypeError as err:
Expand Down Expand Up @@ -174,7 +176,7 @@ def __init__(self, values: Union[np.ndarray, "PandasArray"], copy: bool = False)
@classmethod
def _from_sequence(
cls, scalars, *, dtype: Optional[Dtype] = None, copy: bool = False
) -> "PandasArray":
) -> PandasArray:
if isinstance(dtype, PandasDtype):
dtype = dtype._dtype

Expand All @@ -184,10 +186,10 @@ def _from_sequence(
return cls(result)

@classmethod
def _from_factorized(cls, values, original) -> "PandasArray":
def _from_factorized(cls, values, original) -> PandasArray:
return cls(values)

def _from_backing_data(self, arr: np.ndarray) -> "PandasArray":
def _from_backing_data(self, arr: np.ndarray) -> PandasArray:
return type(self)(arr)

# ------------------------------------------------------------------------
Expand Down
14 changes: 8 additions & 6 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from datetime import timedelta
import operator
from typing import Any, Callable, List, Optional, Sequence, Type, Union
Expand Down Expand Up @@ -191,7 +193,7 @@ def _simple_new(
values: np.ndarray,
freq: Optional[BaseOffset] = None,
dtype: Optional[Dtype] = None,
) -> "PeriodArray":
) -> PeriodArray:
# alias for PeriodArray.__init__
assertion_msg = "Should be numpy array of type i8"
assert isinstance(values, np.ndarray) and values.dtype == "i8", assertion_msg
Expand All @@ -204,7 +206,7 @@ def _from_sequence(
*,
dtype: Optional[Dtype] = None,
copy: bool = False,
) -> "PeriodArray":
) -> PeriodArray:
if dtype and isinstance(dtype, PeriodDtype):
freq = dtype.freq
else:
Expand All @@ -225,11 +227,11 @@ def _from_sequence(
@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype: Optional[Dtype] = None, copy=False
) -> "PeriodArray":
) -> PeriodArray:
return cls._from_sequence(strings, dtype=dtype, copy=copy)

@classmethod
def _from_datetime64(cls, data, freq, tz=None) -> "PeriodArray":
def _from_datetime64(cls, data, freq, tz=None) -> PeriodArray:
"""
Construct a PeriodArray from a datetime64 array
Expand Down Expand Up @@ -504,7 +506,7 @@ def _box_func(self, x) -> Union[Period, NaTType]:
return Period._from_ordinal(ordinal=x, freq=self.freq)

@doc(**_shared_doc_kwargs, other="PeriodIndex", other_name="PeriodIndex")
def asfreq(self, freq=None, how: str = "E") -> "PeriodArray":
def asfreq(self, freq=None, how: str = "E") -> PeriodArray:
"""
Convert the {klass} to the specified frequency `freq`.
Expand Down Expand Up @@ -675,7 +677,7 @@ def _sub_period_array(self, other):

def _addsub_int_array(
self, other: np.ndarray, op: Callable[[Any, Any], Any]
) -> "PeriodArray":
) -> PeriodArray:
"""
Add or subtract array of integers; equivalent to applying
`_time_shift` pointwise.
Expand Down
14 changes: 8 additions & 6 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""
SparseArray data structure
"""
from __future__ import annotations

from collections import abc
import numbers
import operator
Expand Down Expand Up @@ -812,7 +814,7 @@ def _get_val_at(self, loc):
val = maybe_box_datetimelike(val, self.sp_values.dtype)
return val

def take(self, indices, *, allow_fill=False, fill_value=None) -> "SparseArray":
def take(self, indices, *, allow_fill=False, fill_value=None) -> SparseArray:
if is_scalar(indices):
raise ValueError(f"'indices' must be an array, not a scalar '{indices}'.")
indices = np.asarray(indices, dtype=np.int32)
Expand Down Expand Up @@ -1403,7 +1405,7 @@ def _arith_method(self, other, op):
other = SparseArray(other, fill_value=self.fill_value, dtype=dtype)
return _sparse_array_op(self, other, op, op_name)

def _cmp_method(self, other, op) -> "SparseArray":
def _cmp_method(self, other, op) -> SparseArray:
if not is_scalar(other) and not isinstance(other, type(self)):
# convert list-like to ndarray
other = np.asarray(other)
Expand Down Expand Up @@ -1431,19 +1433,19 @@ def _cmp_method(self, other, op) -> "SparseArray":

_logical_method = _cmp_method

def _unary_method(self, op) -> "SparseArray":
def _unary_method(self, op) -> SparseArray:
fill_value = op(np.array(self.fill_value)).item()
values = op(self.sp_values)
dtype = SparseDtype(values.dtype, fill_value)
return type(self)._simple_new(values, self.sp_index, dtype)

def __pos__(self) -> "SparseArray":
def __pos__(self) -> SparseArray:
return self._unary_method(operator.pos)

def __neg__(self) -> "SparseArray":
def __neg__(self) -> SparseArray:
return self._unary_method(operator.neg)

def __invert__(self) -> "SparseArray":
def __invert__(self) -> SparseArray:
return self._unary_method(operator.invert)

# ----------
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/sparse/dtype.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sparse Dtype"""
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Type
Expand Down Expand Up @@ -185,7 +186,7 @@ def construct_array_type(cls) -> Type["SparseArray"]:
return SparseArray

@classmethod
def construct_from_string(cls, string: str) -> "SparseDtype":
def construct_from_string(cls, string: str) -> SparseDtype:
"""
Construct a SparseDtype from a string form.
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/arrays/string_.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Optional, Type, Union

import numpy as np
Expand Down Expand Up @@ -84,7 +86,7 @@ def __repr__(self) -> str:

def __from_arrow__(
self, array: Union["pyarrow.Array", "pyarrow.ChunkedArray"]
) -> "StringArray":
) -> StringArray:
"""
Construct StringArray from pyarrow Array/ChunkedArray.
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/string_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def __repr__(self) -> str:

def __from_arrow__(
self, array: Union["pa.Array", "pa.ChunkedArray"]
) -> "ArrowStringArray":
) -> ArrowStringArray:
"""
Construct StringArray from pyarrow Array/ChunkedArray.
"""
Expand Down Expand Up @@ -507,7 +507,7 @@ def __setitem__(self, key: Union[int, np.ndarray], value: Any) -> None:

def take(
self, indices: Sequence[int], allow_fill: bool = False, fill_value: Any = None
) -> "ExtensionArray":
) -> ExtensionArray:
"""
Take elements from an array.
Expand Down
Loading

0 comments on commit f51547c

Please sign in to comment.