Skip to content

TYP: fix ignores #40412

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 3 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ cdef class TextReader:
object skiprows
object dtype
object usecols
list dtype_cast_order
list dtype_cast_order # list[np.dtype]
set unnamed_cols
set noconvert

Expand Down
4 changes: 2 additions & 2 deletions pandas/_testing/asserters.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,8 @@ def assert_series_equal(
left_values = left._values
right_values = right._values
# Only check exact if dtype is numeric
if is_extension_array_dtype(left_values) and is_extension_array_dtype(
right_values
if isinstance(left_values, ExtensionArray) and isinstance(
right_values, ExtensionArray
):
assert_extension_array_equal(
left_values,
Expand Down
35 changes: 11 additions & 24 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,41 +235,26 @@ def _reconstruct_data(
# Catch DatetimeArray/TimedeltaArray
return values

if is_extension_array_dtype(dtype):
# error: Item "dtype[Any]" of "Union[dtype[Any], ExtensionDtype]" has no
# attribute "construct_array_type"
cls = dtype.construct_array_type() # type: ignore[union-attr]
if not isinstance(dtype, np.dtype):
# i.e. ExtensionDtype
cls = dtype.construct_array_type()
if isinstance(values, cls) and values.dtype == dtype:
return values

values = cls._from_sequence(values)
elif is_bool_dtype(dtype):
# error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has
# incompatible type "Union[dtype, ExtensionDtype]"; expected
# "Union[dtype, None, type, _SupportsDtype, str, Tuple[Any, int],
# Tuple[Any, Union[int, Sequence[int]]], List[Any], _DtypeDict,
# Tuple[Any, Any]]"
values = values.astype(dtype, copy=False) # type: ignore[arg-type]
values = values.astype(dtype, copy=False)

# we only support object dtypes bool Index
if isinstance(original, ABCIndex):
values = values.astype(object, copy=False)
elif dtype is not None:
if is_datetime64_dtype(dtype):
# error: Incompatible types in assignment (expression has type
# "str", variable has type "Union[dtype, ExtensionDtype]")
dtype = "datetime64[ns]" # type: ignore[assignment]
dtype = np.dtype("datetime64[ns]")
elif is_timedelta64_dtype(dtype):
# error: Incompatible types in assignment (expression has type
# "str", variable has type "Union[dtype, ExtensionDtype]")
dtype = "timedelta64[ns]" # type: ignore[assignment]
dtype = np.dtype("timedelta64[ns]")

# error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has
# incompatible type "Union[dtype, ExtensionDtype]"; expected
# "Union[dtype, None, type, _SupportsDtype, str, Tuple[Any, int],
# Tuple[Any, Union[int, Sequence[int]]], List[Any], _DtypeDict,
# Tuple[Any, Any]]"
values = values.astype(dtype, copy=False) # type: ignore[arg-type]
values = values.astype(dtype, copy=False)

return values

Expand Down Expand Up @@ -772,7 +757,8 @@ def factorize(
uniques = Index(uniques)
return codes, uniques

if is_extension_array_dtype(values.dtype):
if not isinstance(values.dtype, np.dtype):
# i.e. ExtensionDtype
codes, uniques = values.factorize(na_sentinel=na_sentinel)
dtype = original.dtype
else:
Expand Down Expand Up @@ -1662,7 +1648,8 @@ def diff(arr, n: int, axis: int = 0, stacklevel=3):
arr = arr.to_numpy()
dtype = arr.dtype

if is_extension_array_dtype(dtype):
if not isinstance(dtype, np.dtype):
# i.e ExtensionDtype
if hasattr(arr, f"__{op.__name__}__"):
if axis != 0:
raise ValueError(f"cannot diff {type(arr).__name__} on axis={axis}")
Expand Down
24 changes: 7 additions & 17 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@
needs_i8_conversion,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import CategoricalDtype
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
ExtensionDtype,
)
from pandas.core.dtypes.generic import (
ABCIndex,
ABCSeries,
Expand Down Expand Up @@ -504,7 +507,7 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
result = self._set_dtype(dtype)

# TODO: consolidate with ndarray case?
elif is_extension_array_dtype(dtype):
elif isinstance(dtype, ExtensionDtype):
result = pd_array(self, dtype=dtype, copy=copy)

elif is_integer_dtype(dtype) and self.isna().any():
Expand All @@ -515,28 +518,15 @@ def astype(self, dtype: Dtype, copy: bool = True) -> ArrayLike:
# variable has type "Categorical")
result = np.array( # type: ignore[assignment]
self,
# error: Argument "dtype" to "array" has incompatible type
# "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float],
# Type[int], Type[complex], Type[bool], Type[object]]"; expected
# "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any,
# int], Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict,
# Tuple[Any, Any]]]"
dtype=dtype, # type: ignore[arg-type]
dtype=dtype,
copy=copy,
)

else:
# GH8628 (PERF): astype category codes instead of astyping array
try:
new_cats = np.asarray(self.categories)
# error: Argument "dtype" to "astype" of "_ArrayOrScalarCommon" has
# incompatible type "Union[ExtensionDtype, dtype[Any]]"; expected
# "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any,
# int], Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict,
# Tuple[Any, Any]]]"
new_cats = new_cats.astype(
dtype=dtype, copy=copy # type: ignore[arg-type]
)
new_cats = new_cats.astype(dtype=dtype, copy=copy)
except (
TypeError, # downstream error msg for CategoricalIndex is misleading
ValueError,
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,12 +878,11 @@ def _isnan(self) -> np.ndarray:
return self.asi8 == iNaT

@property # NB: override with cache_readonly in immutable subclasses
def _hasnans(self) -> np.ndarray:
def _hasnans(self) -> bool:
"""
return if I have any nans; enables various perf speedups
"""
# error: Incompatible return value type (got "bool", expected "ndarray")
return bool(self._isnan.any()) # type: ignore[return-value]
return bool(self._isnan.any())

def _maybe_mask_results(
self, result: np.ndarray, fill_value=iNaT, convert=None
Expand Down
114 changes: 29 additions & 85 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@
str_t = str


# error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object"
_o_dtype = np.dtype(object) # type: ignore[type-var]
_o_dtype = np.dtype("object")


_Identity = NewType("_Identity", object)
Expand Down Expand Up @@ -417,11 +416,7 @@ def __new__(
# maybe coerce to a sub-class
arr = data
else:
# error: Argument "dtype" to "asarray_tuplesafe" has incompatible type
# "Type[object]"; expected "Union[str, dtype[Any], None]"
arr = com.asarray_tuplesafe(
data, dtype=object # type: ignore[arg-type]
)
arr = com.asarray_tuplesafe(data, dtype=np.dtype("object"))
Copy link
Member

Choose a reason for hiding this comment

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

do not change but I think dtype should be the string version to satisfy the typing in asarray_tuplesafe since not sure if there are perf issues that will accumulate creating a dtype object unnecessarily. but there does look like there may be an issue in asarray_tuplesafe since "object" in [np.object_, object] is False so the type annotations for asarray_tuplesafe maybe incorrect.

side note:

I think that we want to import the numpy type definitions for dtype in pandas._typing

in asarray_tuplesafe we use NpDtype from pandas._typing, which is not as permissive as the numpy alias, or what is actually accepted by numpy for dtype arguments (with the exception of object which is a false positive but is a special case since it creates issues with static typing)

Copy link
Member Author

Choose a reason for hiding this comment

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

since not sure if there are perf issues that will accumulate creating a dtype object unnecessarily

my thought is that these will have to be converted to np.dtype objects somewhere in the call stack, so should be roughly perf neutral. no actual measurements on that though.


if dtype is None:
arr = _maybe_cast_data_without_dtype(arr)
Expand Down Expand Up @@ -456,9 +451,7 @@ def __new__(
)
# other iterable of some kind

# error: Argument "dtype" to "asarray_tuplesafe" has incompatible type
# "Type[object]"; expected "Union[str, dtype[Any], None]"
subarr = com.asarray_tuplesafe(data, dtype=object) # type: ignore[arg-type]
subarr = com.asarray_tuplesafe(data, dtype=np.dtype("object"))
return Index(subarr, dtype=dtype, copy=copy, name=name, **kwargs)

@classmethod
Expand Down Expand Up @@ -2902,16 +2895,10 @@ def union(self, other, sort=None):
# <T> | <T> -> T
# <T> | <U> -> object
if not (is_integer_dtype(self.dtype) and is_integer_dtype(other.dtype)):
# error: Incompatible types in assignment (expression has type
# "str", variable has type "Union[dtype[Any], ExtensionDtype]")
dtype = "float64" # type: ignore[assignment]
dtype = np.dtype("float64")
Copy link
Member

Choose a reason for hiding this comment

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

dtype is passed to astype which accepts a string? i'm not convinced this is the best approach to removing the ignores. maybe could add a type variable declaration to dtype = find_common_type(... to avoid the Incompatible types in assignment... error.

else:
# one is int64 other is uint64

# error: Incompatible types in assignment (expression has type
# "Type[object]", variable has type "Union[dtype[Any],
# ExtensionDtype]")
dtype = object # type: ignore[assignment]
dtype = np.dtype("object")

left = self.astype(dtype, copy=False)
right = other.astype(dtype, copy=False)
Expand Down Expand Up @@ -3906,6 +3893,9 @@ def join(
self_is_mi = isinstance(self, ABCMultiIndex)
other_is_mi = isinstance(other, ABCMultiIndex)

lindexer: Optional[np.ndarray]
rindexer: Optional[np.ndarray]

# try to figure out the join level
# GH3662
if level is None and (self_is_mi or other_is_mi):
Expand Down Expand Up @@ -4003,15 +3993,11 @@ def join(

if return_indexers:
if join_index is self:
# error: Incompatible types in assignment (expression has type "None",
# variable has type "ndarray")
lindexer = None # type: ignore[assignment]
lindexer = None
else:
lindexer = self.get_indexer(join_index)
if join_index is other:
# error: Incompatible types in assignment (expression has type "None",
# variable has type "ndarray")
rindexer = None # type: ignore[assignment]
rindexer = None
else:
rindexer = other.get_indexer(join_index)
return join_index, lindexer, rindexer
Expand Down Expand Up @@ -4114,15 +4100,11 @@ def _join_non_unique(self, other, how="left", return_indexers=False):
left_idx = ensure_platform_int(left_idx)
right_idx = ensure_platform_int(right_idx)

join_index = np.asarray(lvalues.take(left_idx))
join_array = np.asarray(lvalues.take(left_idx))
mask = left_idx == -1
np.putmask(join_index, mask, rvalues.take(right_idx))
np.putmask(join_array, mask, rvalues.take(right_idx))

# error: Incompatible types in assignment (expression has type "Index", variable
# has type "ndarray")
join_index = self._wrap_joined_index(
join_index, other # type: ignore[assignment]
)
join_index = self._wrap_joined_index(join_array, other)

if return_indexers:
return join_index, left_idx, right_idx
Expand Down Expand Up @@ -4286,6 +4268,9 @@ def _join_monotonic(self, other, how="left", return_indexers=False):
sv = self._get_engine_target()
ov = other._get_engine_target()

ridx: Optional[np.ndarray]
lidx: Optional[np.ndarray]

if self.is_unique and other.is_unique:
# We can perform much better than the general case
if how == "left":
Expand All @@ -4295,61 +4280,24 @@ def _join_monotonic(self, other, how="left", return_indexers=False):
elif how == "right":
join_index = other
lidx = self._left_indexer_unique(ov, sv)
# error: Incompatible types in assignment (expression has type "None",
# variable has type "ndarray")
ridx = None # type: ignore[assignment]
ridx = None
elif how == "inner":
# error: Incompatible types in assignment (expression has type
# "ndarray", variable has type "Index")
join_index, lidx, ridx = self._inner_indexer( # type:ignore[assignment]
sv, ov
)
# error: Argument 1 to "_wrap_joined_index" of "Index" has incompatible
# type "Index"; expected "ndarray"
join_index = self._wrap_joined_index(
join_index, other # type: ignore[arg-type]
)
join_array, lidx, ridx = self._inner_indexer(sv, ov)
join_index = self._wrap_joined_index(join_array, other)
elif how == "outer":
# error: Incompatible types in assignment (expression has type
# "ndarray", variable has type "Index")
join_index, lidx, ridx = self._outer_indexer( # type:ignore[assignment]
sv, ov
)
# error: Argument 1 to "_wrap_joined_index" of "Index" has incompatible
# type "Index"; expected "ndarray"
join_index = self._wrap_joined_index(
join_index, other # type: ignore[arg-type]
)
join_array, lidx, ridx = self._outer_indexer(sv, ov)
join_index = self._wrap_joined_index(join_array, other)
else:
if how == "left":
# error: Incompatible types in assignment (expression has type
# "ndarray", variable has type "Index")
join_index, lidx, ridx = self._left_indexer( # type: ignore[assignment]
sv, ov
)
join_array, lidx, ridx = self._left_indexer(sv, ov)
elif how == "right":
# error: Incompatible types in assignment (expression has type
# "ndarray", variable has type "Index")
join_index, ridx, lidx = self._left_indexer( # type: ignore[assignment]
ov, sv
)
join_array, ridx, lidx = self._left_indexer(ov, sv)
elif how == "inner":
# error: Incompatible types in assignment (expression has type
# "ndarray", variable has type "Index")
join_index, lidx, ridx = self._inner_indexer( # type:ignore[assignment]
sv, ov
)
join_array, lidx, ridx = self._inner_indexer(sv, ov)
elif how == "outer":
# error: Incompatible types in assignment (expression has type
# "ndarray", variable has type "Index")
join_index, lidx, ridx = self._outer_indexer( # type:ignore[assignment]
sv, ov
)
# error: Argument 1 to "_wrap_joined_index" of "Index" has incompatible type
# "Index"; expected "ndarray"
join_index = self._wrap_joined_index(
join_index, other # type: ignore[arg-type]
)
join_array, lidx, ridx = self._outer_indexer(sv, ov)

join_index = self._wrap_joined_index(join_array, other)

if return_indexers:
lidx = None if lidx is None else ensure_platform_int(lidx)
Expand Down Expand Up @@ -6481,12 +6429,8 @@ def _maybe_cast_data_without_dtype(subarr):
pass

elif inferred.startswith("timedelta"):
# error: Incompatible types in assignment (expression has type
# "TimedeltaArray", variable has type "ndarray")
data = TimedeltaArray._from_sequence( # type: ignore[assignment]
subarr, copy=False
)
return data
tda = TimedeltaArray._from_sequence(subarr, copy=False)
return tda
elif inferred == "period":
try:
data = PeriodArray._from_sequence(subarr)
Expand Down
15 changes: 4 additions & 11 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray

# comparison is faster than is_object_dtype

# error: Value of type variable "_DTypeScalar" of "dtype" cannot be "object"
_dtype_obj = np.dtype(object) # type: ignore[type-var]
_dtype_obj = np.dtype("object")


class Block(PandasObject):
Expand Down Expand Up @@ -1598,14 +1596,9 @@ def to_native_types(self, na_rep="nan", quoting=None, **kwargs):
values = self.values
mask = isna(values)

# error: Incompatible types in assignment (expression has type "ndarray",
# variable has type "ExtensionArray")
values = np.asarray(values.astype(object)) # type: ignore[assignment]
values[mask] = na_rep

# TODO(EA2D): reshape not needed with 2D EAs
# we are expected to return a 2-d ndarray
return self.make_block(values)
new_values = np.asarray(values.astype(object))
new_values[mask] = na_rep
return self.make_block(new_values)

def take_nd(
self, indexer, axis: int = 0, new_mgr_locs=None, fill_value=lib.no_default
Expand Down
Loading