Skip to content

TYP: mypy fixups- something upgraded? #41593

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 1 commit into from
May 20, 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
3 changes: 1 addition & 2 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,7 @@ def size(self) -> int:
"""
The number of elements in the array.
"""
# error: Incompatible return value type (got "number", expected "int")
return np.prod(self.shape) # type: ignore[return-value]
return np.prod(self.shape)

@property
def ndim(self) -> int:
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/sparse/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,8 @@ def density(self) -> float:
"""
Ratio of non-sparse points to total (dense) data points.
"""
# error: Incompatible return value type (got "number", expected "float")
tmp = np.mean([column.array.density for _, column in self._parent.items()])
return tmp # type: ignore[return-value]
return tmp

@staticmethod
def _prep_index(data, index, columns):
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,7 @@ def size(self) -> int:
>>> df.size
4
"""
# error: Incompatible return value type (got "number", expected "int")
return np.prod(self.shape) # type: ignore[return-value]
return np.prod(self.shape)

@overload
def set_axis(
Expand Down
24 changes: 4 additions & 20 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,17 +588,9 @@ def nansum(
dtype_sum = np.float64 # type: ignore[assignment]

the_sum = values.sum(axis, dtype=dtype_sum)
# error: Incompatible types in assignment (expression has type "float", variable has
# type "Union[number, ndarray]")
# error: Argument 1 to "_maybe_null_out" has incompatible type "Union[number,
# ndarray]"; expected "ndarray"
the_sum = _maybe_null_out( # type: ignore[assignment]
the_sum, axis, mask, values.shape, min_count=min_count # type: ignore[arg-type]
)
the_sum = _maybe_null_out(the_sum, axis, mask, values.shape, min_count=min_count)

# error: Incompatible return value type (got "Union[number, ndarray]", expected
# "float")
return the_sum # type: ignore[return-value]
return the_sum


def _mask_datetimelike_result(
Expand Down Expand Up @@ -1343,12 +1335,10 @@ def nanprod(
values = values.copy()
values[mask] = 1
result = values.prod(axis)
# error: Argument 1 to "_maybe_null_out" has incompatible type "Union[number,
# ndarray]"; expected "ndarray"
# error: Incompatible return value type (got "Union[ndarray, float]", expected
# "float")
return _maybe_null_out( # type: ignore[return-value]
result, axis, mask, values.shape, min_count=min_count # type: ignore[arg-type]
result, axis, mask, values.shape, min_count=min_count
)


Expand Down Expand Up @@ -1424,13 +1414,7 @@ def _get_counts(
# expected "Union[int, float, ndarray]")
return dtype.type(count) # type: ignore[return-value]
try:
# error: Incompatible return value type (got "Union[ndarray, generic]", expected
# "Union[int, float, ndarray]")
# error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has incompatible type
# "Union[ExtensionDtype, dtype]"; expected "Union[dtype, None, type,
# _SupportsDtype, str, Tuple[Any, int], Tuple[Any, Union[int, Sequence[int]]],
# List[Any], _DtypeDict, Tuple[Any, Any]]"
return count.astype(dtype) # type: ignore[return-value,arg-type]
return count.astype(dtype)
except AttributeError:
# error: Argument "dtype" to "array" has incompatible type
# "Union[ExtensionDtype, dtype]"; expected "Union[dtype, None, type,
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ def _make_selectors(self):
self.full_shape = ngroups, stride

selector = self.sorted_labels[-1] + stride * comp_index + self.lift
# error: Argument 1 to "zeros" has incompatible type "number"; expected
# "Union[int, Sequence[int]]"
mask = np.zeros(np.prod(self.full_shape), dtype=bool) # type: ignore[arg-type]
mask = np.zeros(np.prod(self.full_shape), dtype=bool)
mask.put(selector, True)

if mask.sum() < len(self.index):
Expand Down
13 changes: 3 additions & 10 deletions pandas/core/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,22 +630,15 @@ def get_group_index_sorter(
np.ndarray[np.intp]
"""
if ngroups is None:
# error: Incompatible types in assignment (expression has type "number[Any]",
# variable has type "Optional[int]")
ngroups = 1 + group_index.max() # type: ignore[assignment]
ngroups = 1 + group_index.max()
count = len(group_index)
alpha = 0.0 # taking complexities literally; there may be
beta = 1.0 # some room for fine-tuning these parameters
# error: Unsupported operand types for * ("float" and "None")
do_groupsort = count > 0 and (
(alpha + beta * ngroups) < (count * np.log(count)) # type: ignore[operator]
)
do_groupsort = count > 0 and ((alpha + beta * ngroups) < (count * np.log(count)))
if do_groupsort:
# Argument 2 to "groupsort_indexer" has incompatible type
# "Optional[int]"; expected "int"
sorter, _ = algos.groupsort_indexer(
ensure_platform_int(group_index),
ngroups, # type: ignore[arg-type]
ngroups,
)
# sorter _should_ already be intp, but mypy is not yet able to verify
else:
Expand Down
14 changes: 2 additions & 12 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,19 +1664,9 @@ def format_percentiles(
).astype(int)
prec = max(1, prec)
out = np.empty_like(percentiles, dtype=object)
# error: No overload variant of "__getitem__" of "list" matches argument type
# "Union[bool_, ndarray]"
out[int_idx] = (
percentiles[int_idx].astype(int).astype(str) # type: ignore[call-overload]
)
out[int_idx] = percentiles[int_idx].astype(int).astype(str)

# error: Item "float" of "Union[Any, float, str]" has no attribute "round"
# error: Item "str" of "Union[Any, float, str]" has no attribute "round"
# error: Invalid index type "Union[bool_, Any]" for "Union[ndarray, List[Union[int,
# float]], List[float], List[Union[str, float]]]"; expected type "int"
out[~int_idx] = (
percentiles[~int_idx].round(prec).astype(str) # type: ignore[union-attr,index]
)
out[~int_idx] = percentiles[~int_idx].round(prec).astype(str)
return [i + "%" for i in out]


Expand Down
4 changes: 1 addition & 3 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,7 @@ def _infer_types(self, values, na_values, try_num_bool=True):
# error: Argument 2 to "isin" has incompatible type "List[Any]"; expected
# "Union[Union[ExtensionArray, ndarray], Index, Series]"
mask = algorithms.isin(values, list(na_values)) # type: ignore[arg-type]
# error: Incompatible types in assignment (expression has type
# "number[Any]", variable has type "int")
na_count = mask.sum() # type: ignore[assignment]
na_count = mask.sum()
if na_count > 0:
if is_integer_dtype(values):
values = values.astype(np.float64)
Expand Down
5 changes: 1 addition & 4 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3378,10 +3378,7 @@ def validate_multiindex(
@property
def nrows_expected(self) -> int:
""" based on our axes, compute the expected nrows """
# error: Incompatible return value type (got "number", expected "int")
return np.prod( # type: ignore[return-value]
[i.cvalues.shape[0] for i in self.index_axes]
)
return np.prod([i.cvalues.shape[0] for i in self.index_axes])

@property
def is_exists(self) -> bool:
Expand Down