Skip to content

CLN: add typing to dtype arg in selection of files in core/arrays (GH38808) #38826

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 4 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import numpy as np

from pandas._libs import lib
from pandas._typing import ArrayLike, Shape
from pandas._typing import ArrayLike, DtypeArg, Shape
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
Expand Down Expand Up @@ -189,7 +189,7 @@ class ExtensionArray:
# ------------------------------------------------------------------------

@classmethod
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
def _from_sequence(cls, scalars, *, dtype: Optional[DtypeArg] = None, copy=False):
"""
Construct a new ExtensionArray from a sequence of scalars.

Expand All @@ -211,7 +211,9 @@ def _from_sequence(cls, scalars, *, dtype=None, copy=False):
raise AbstractMethodError(cls)

@classmethod
def _from_sequence_of_strings(cls, strings, *, dtype=None, copy=False):
def _from_sequence_of_strings(
cls, strings, *, dtype: Optional[DtypeArg] = None, copy=False
):
"""
Construct a new ExtensionArray from a sequence of strings.

Expand Down Expand Up @@ -391,7 +393,10 @@ def __ne__(self, other: Any) -> ArrayLike:
return ~(self == other)

def to_numpy(
self, dtype=None, copy: bool = False, na_value=lib.no_default
self,
dtype: Optional[DtypeArg] = None,
copy: bool = False,
na_value=lib.no_default,
) -> np.ndarray:
"""
Convert to a NumPy ndarray.
Expand Down Expand Up @@ -1065,7 +1070,7 @@ def copy(self: ExtensionArrayT) -> ExtensionArrayT:
"""
raise AbstractMethodError(self)

def view(self, dtype=None) -> ArrayLike:
def view(self, dtype: Optional[DtypeArg] = None) -> ArrayLike:
"""
Return a view on the array.

Expand Down
8 changes: 4 additions & 4 deletions pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import numbers
from typing import TYPE_CHECKING, List, Tuple, Type, Union
from typing import TYPE_CHECKING, List, Optional, Tuple, Type, Union
import warnings

import numpy as np

from pandas._libs import lib, missing as libmissing
from pandas._typing import ArrayLike
from pandas._typing import ArrayLike, DtypeArg
from pandas.compat.numpy import function as nv

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -273,7 +273,7 @@ def dtype(self) -> BooleanDtype:

@classmethod
def _from_sequence(
cls, scalars, *, dtype=None, copy: bool = False
cls, scalars, *, dtype: Optional[DtypeArg] = None, copy: bool = False
Copy link
Contributor

Choose a reason for hiding this comment

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

most of these are Optional[Dtype} NOT DtypeArg (eg. only a single dtype and not a dict of them). The only things that take DtypeArg are things like astype and the I/O methods

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed it. I did it right last time, but I think I was a bit too quick with submitting this PR. Thanks for the review.

) -> "BooleanArray":
if dtype:
assert dtype == "boolean"
Expand All @@ -282,7 +282,7 @@ def _from_sequence(

@classmethod
def _from_sequence_of_strings(
cls, strings: List[str], *, dtype=None, copy: bool = False
cls, strings: List[str], *, dtype: Optional[DtypeArg] = None, copy: bool = False
) -> "BooleanArray":
def map_string(s):
if isna(s):
Expand Down
13 changes: 8 additions & 5 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Dict,
Hashable,
List,
Optional,
Sequence,
Type,
TypeVar,
Expand All @@ -21,7 +22,7 @@

from pandas._libs import NaT, algos as libalgos, hashtable as htable
from pandas._libs.lib import no_default
from pandas._typing import ArrayLike, Dtype, Ordered, Scalar
from pandas._typing import ArrayLike, Dtype, DtypeArg, Ordered, Scalar
from pandas.compat.numpy import function as nv
from pandas.util._decorators import cache_readonly, deprecate_kwarg
from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs
Expand Down Expand Up @@ -318,7 +319,7 @@ def __init__(
values,
categories=None,
ordered=None,
dtype=None,
dtype: Optional[DtypeArg] = None,
fastpath=False,
copy: bool = True,
):
Expand Down Expand Up @@ -423,7 +424,7 @@ def _constructor(self) -> Type["Categorical"]:
return Categorical

@classmethod
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
def _from_sequence(cls, scalars, *, dtype: Optional[DtypeArg] = None, copy=False):
return Categorical(scalars, dtype=dtype, copy=copy)

def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
Expand Down Expand Up @@ -558,7 +559,9 @@ def _from_inferred_categories(
return cls(codes, dtype=dtype, fastpath=True)

@classmethod
def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
def from_codes(
cls, codes, categories=None, ordered=None, dtype: Optional[DtypeArg] = None
):
"""
Make a Categorical type from codes and categories or dtype.

Expand Down Expand Up @@ -1294,7 +1297,7 @@ def _validate_fill_value(self, fill_value):

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

def __array__(self, dtype=None) -> np.ndarray:
def __array__(self, dtype: Optional[DtypeArg] = None) -> np.ndarray:
"""
The numpy array interface.

Expand Down
10 changes: 5 additions & 5 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
integer_op_not_supported,
round_nsint64,
)
from pandas._typing import DatetimeLikeScalar, DtypeObj
from pandas._typing import DatetimeLikeScalar, DtypeArg, DtypeObj
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError, NullFrequencyError, PerformanceWarning
from pandas.util._decorators import Appender, Substitution, cache_readonly
Expand Down Expand Up @@ -107,15 +107,15 @@ class DatetimeLikeArrayMixin(OpsMixin, NDArrayBackedExtensionArray):
_recognized_scalars: Tuple[Type, ...]
_data: np.ndarray

def __init__(self, data, dtype=None, freq=None, copy=False):
def __init__(self, data, dtype: Optional[DtypeArg] = None, freq=None, copy=False):
raise AbstractMethodError(self)

@classmethod
def _simple_new(
cls: Type[DatetimeLikeArrayT],
values: np.ndarray,
freq: Optional[BaseOffset] = None,
dtype=None,
dtype: Optional[DtypeArg] = None,
) -> DatetimeLikeArrayT:
raise AbstractMethodError(cls)

Expand Down Expand Up @@ -265,7 +265,7 @@ def _formatter(self, boxed=False):
# ----------------------------------------------------------------
# Array-Like / EA-Interface Methods

def __array__(self, dtype=None) -> np.ndarray:
def __array__(self, dtype: Optional[DtypeArg] = None) -> np.ndarray:
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
if is_object_dtype(dtype):
return np.array(list(self), dtype=object)
Expand Down Expand Up @@ -383,7 +383,7 @@ def astype(self, dtype, copy=True):
else:
return np.asarray(self, dtype=dtype)

def view(self, dtype=None):
def view(self, dtype: Optional[DtypeArg] = None):
if dtype is None or dtype is self.dtype:
return type(self)(self._ndarray, dtype=self.dtype)
return self._ndarray.view(dtype=dtype)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np

from pandas._libs import iNaT, lib, missing as libmissing
from pandas._typing import ArrayLike, DtypeObj
from pandas._typing import ArrayLike, DtypeArg, DtypeObj
from pandas.compat.numpy import function as nv
from pandas.util._decorators import cache_readonly

Expand Down Expand Up @@ -304,14 +304,14 @@ def __abs__(self):

@classmethod
def _from_sequence(
cls, scalars, *, dtype=None, copy: bool = False
cls, scalars, *, dtype: Optional[DtypeArg] = None, copy: bool = False
) -> "IntegerArray":
values, mask = coerce_to_array(scalars, dtype=dtype, copy=copy)
return IntegerArray(values, mask)

@classmethod
def _from_sequence_of_strings(
cls, strings, *, dtype=None, copy: bool = False
cls, strings, *, dtype: Optional[DtypeArg] = None, copy: bool = False
) -> "IntegerArray":
scalars = to_numeric(strings, errors="raise")
return cls._from_sequence(scalars, dtype=dtype, copy=copy)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
get_period_field_arr,
period_asfreq_arr,
)
from pandas._typing import AnyArrayLike
from pandas._typing import AnyArrayLike, DtypeArg
from pandas.util._decorators import cache_readonly, doc

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -198,10 +198,10 @@ def _from_sequence(
cls: Type["PeriodArray"],
scalars: Union[Sequence[Optional[Period]], AnyArrayLike],
*,
dtype: Optional[PeriodDtype] = None,
dtype: Optional[DtypeArg] = None,
copy: bool = False,
) -> "PeriodArray":
if dtype:
if dtype and isinstance(dtype, PeriodDtype):
freq = dtype.freq
else:
freq = None
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pandas._libs.interval import Interval
from pandas._libs.tslibs import NaT, Period, Timestamp, dtypes, timezones, to_offset
from pandas._libs.tslibs.offsets import BaseOffset
from pandas._typing import DtypeObj, Ordered
from pandas._typing import DtypeArg, DtypeObj, Ordered

from pandas.core.dtypes.base import ExtensionDtype, register_extension_dtype
from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCIndex
Expand Down Expand Up @@ -185,7 +185,7 @@ def _from_values_or_dtype(
values=None,
categories=None,
ordered: Optional[bool] = None,
dtype: Optional["CategoricalDtype"] = None,
dtype: Optional[DtypeArg] = None,
) -> "CategoricalDtype":
"""
Construct dtype from the input parameters used in :class:`Categorical`.
Expand Down Expand Up @@ -247,7 +247,7 @@ def _from_values_or_dtype(
CategoricalDtype(categories=['x', 'y'], ordered=False)
"""

if dtype is not None:
if dtype is not None and isinstance(dtype, CategoricalDtype):
# The dtype argument takes precedence over values.dtype (if any)
if isinstance(dtype, str):
if dtype == "category":
Expand Down