Skip to content

Commit

Permalink
TYP: add mypy messages as comments to ignores (#40268)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjayhawkins authored Mar 6, 2021
1 parent 97b832b commit 9d0517a
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 7 deletions.
1 change: 1 addition & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ def frame_or_series(request):
return request.param


# error: List item 0 has incompatible type "Type[Index]"; expected "Type[IndexOpsMixin]"
@pytest.fixture(
params=[pd.Index, pd.Series], ids=["index", "series"] # type: ignore[list-item]
)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class BooleanDtype(BaseMaskedDtype):

name = "boolean"

# mypy: https://github.com/python/mypy/issues/4125
# https://github.com/python/mypy/issues/4125
# error: Signature of "type" incompatible with supertype "BaseMaskedDtype"
@property
def type(self) -> Type: # type: ignore[override]
return np.bool_
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/arrays/floating.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ def _get_common_dtype(self, dtypes: List[DtypeObj]) -> Optional[DtypeObj]:
if not all(isinstance(t, FloatingDtype) for t in dtypes):
return None
np_dtype = np.find_common_type(
[t.numpy_dtype for t in dtypes], [] # type: ignore[union-attr]
# error: Item "ExtensionDtype" of "Union[Any, ExtensionDtype]" has no
# attribute "numpy_dtype"
[t.numpy_dtype for t in dtypes], # type: ignore[union-attr]
[],
)
if np.issubdtype(np_dtype, np.floating):
return FLOAT_STR_TO_DTYPE[str(np_dtype)]
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,12 @@ def convert_to_list_like(
inputs are returned unmodified whereas others are converted to list.
"""
if isinstance(values, (list, np.ndarray, ABCIndex, ABCSeries, ABCExtensionArray)):
# np.ndarray resolving as Any gives a false positive
# error: Incompatible return value type (got "Union[Any, List[Any], Index,
# Series, ExtensionArray]", expected "Union[List[Any], ExtensionArray]")
# error: Incompatible return value type (got "Union[Any, List[Any], Index,
# Series, ExtensionArray]", expected "Union[List[Any], Index]")
# error: Incompatible return value type (got "Union[Any, List[Any], Index,
# Series, ExtensionArray]", expected "Union[List[Any], Series]")
return values # type: ignore[return-value]
elif isinstance(values, abc.Iterable) and not isinstance(values, str):
return list(values)
Expand Down
17 changes: 15 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4079,7 +4079,13 @@ def extract_unique_dtypes_from_dtypes_set(
for unique_dtype in unique_dtypes
if (
issubclass(
unique_dtype.type, tuple(dtypes_set) # type: ignore[arg-type]
# error: Argument 1 to "tuple" has incompatible type
# "FrozenSet[Union[ExtensionDtype, Union[str, Any], Type[str],
# Type[float], Type[int], Type[complex], Type[bool],
# Type[object]]]"; expected "Iterable[Union[type, Tuple[Any,
# ...]]]"
unique_dtype.type,
tuple(dtypes_set), # type: ignore[arg-type]
)
or (
np.number in dtypes_set
Expand Down Expand Up @@ -6382,7 +6388,14 @@ def _dispatch_frame_op(self, right, func, axis: Optional[int] = None):

# TODO operate_blockwise expects a manager of the same type
bm = self._mgr.operate_blockwise(
right._mgr, array_op # type: ignore[arg-type]
# error: Argument 1 to "operate_blockwise" of "ArrayManager" has
# incompatible type "Union[ArrayManager, BlockManager]"; expected
# "ArrayManager"
# error: Argument 1 to "operate_blockwise" of "BlockManager" has
# incompatible type "Union[ArrayManager, BlockManager]"; expected
# "BlockManager"
right._mgr, # type: ignore[arg-type]
array_op,
)
return type(self)(bm)

Expand Down
17 changes: 16 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,12 @@ def swapaxes(self: FrameOrSeries, axis1, axis2, copy=True) -> FrameOrSeries:
# ignore needed because of NDFrame constructor is different than
# DataFrame/Series constructors.
return self._constructor(
new_values, *new_axes # type: ignore[arg-type]
# error: Argument 2 to "NDFrame" has incompatible type "*Generator[Index,
# None, None]"; expected "bool" [arg-type]
# error: Argument 2 to "NDFrame" has incompatible type "*Generator[Index,
# None, None]"; expected "Optional[Mapping[Optional[Hashable], Any]]"
new_values,
*new_axes, # type: ignore[arg-type]
).__finalize__(self, method="swapaxes")

@final
Expand Down Expand Up @@ -11046,37 +11051,47 @@ def _inplace_method(self, other, op):
return self

def __iadd__(self, other):
# error: Unsupported left operand type for + ("Type[NDFrame]")
return self._inplace_method(other, type(self).__add__) # type: ignore[operator]

def __isub__(self, other):
# error: Unsupported left operand type for - ("Type[NDFrame]")
return self._inplace_method(other, type(self).__sub__) # type: ignore[operator]

def __imul__(self, other):
# error: Unsupported left operand type for * ("Type[NDFrame]")
return self._inplace_method(other, type(self).__mul__) # type: ignore[operator]

def __itruediv__(self, other):
# error: Unsupported left operand type for / ("Type[NDFrame]")
return self._inplace_method(
other, type(self).__truediv__ # type: ignore[operator]
)

def __ifloordiv__(self, other):
# error: Unsupported left operand type for // ("Type[NDFrame]")
return self._inplace_method(
other, type(self).__floordiv__ # type: ignore[operator]
)

def __imod__(self, other):
# error: Unsupported left operand type for % ("Type[NDFrame]")
return self._inplace_method(other, type(self).__mod__) # type: ignore[operator]

def __ipow__(self, other):
# error: Unsupported left operand type for ** ("Type[NDFrame]")
return self._inplace_method(other, type(self).__pow__) # type: ignore[operator]

def __iand__(self, other):
# error: Unsupported left operand type for & ("Type[NDFrame]")
return self._inplace_method(other, type(self).__and__) # type: ignore[operator]

def __ior__(self, other):
# error: Unsupported left operand type for | ("Type[NDFrame]")
return self._inplace_method(other, type(self).__or__) # type: ignore[operator]

def __ixor__(self, other):
# error: Unsupported left operand type for ^ ("Type[NDFrame]")
return self._inplace_method(other, type(self).__xor__) # type: ignore[operator]

# ----------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def items(self) -> Index:
return self._axes[-1]

@property
# error: Signature of "axes" incompatible with supertype "DataManager"
def axes(self) -> List[Index]: # type: ignore[override]
# mypy doesn't work to override attribute with property
# see https://github.com/python/mypy/issues/4125
Expand Down Expand Up @@ -454,11 +455,19 @@ def apply_with_block(self: T, f, align_keys=None, swap_axis=True, **kwargs) -> T
if obj.ndim == 2:
kwargs[k] = obj[[i]]

# error: Item "ExtensionArray" of "Union[Any, ExtensionArray]" has no
# attribute "tz"
if hasattr(arr, "tz") and arr.tz is None: # type: ignore[union-attr]
# DatetimeArray needs to be converted to ndarray for DatetimeBlock

# error: Item "ExtensionArray" of "Union[Any, ExtensionArray]" has no
# attribute "_data"
arr = arr._data # type: ignore[union-attr]
elif arr.dtype.kind == "m" and not isinstance(arr, np.ndarray):
# TimedeltaArray needs to be converted to ndarray for TimedeltaBlock

# error: Item "ExtensionArray" of "Union[Any, ExtensionArray]" has no
# attribute "_data"
arr = arr._data # type: ignore[union-attr]

if self.ndim == 2:
Expand Down
1 change: 1 addition & 0 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,7 @@ def na_accum_func(values: ArrayLike, accum_func, *, skipna: bool) -> ArrayLike:
# TODO: have this case go through a DTA method?
# For DatetimeTZDtype, view result as M8[ns]
npdtype = orig_dtype if isinstance(orig_dtype, np.dtype) else "M8[ns]"
# error: "Type[ExtensionArray]" has no attribute "_simple_new"
result = type(values)._simple_new( # type: ignore[attr-defined]
result.view(npdtype), dtype=orig_dtype
)
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,9 @@ def _set_labels(self, key, value):
def _set_values(self, key, value):
if isinstance(key, Series):
key = key._values
# error: Incompatible types in assignment (expression has type "Union[Any,
# BlockManager]", variable has type "Union[SingleArrayManager,
# SingleBlockManager]")
self._mgr = self._mgr.setitem( # type: ignore[assignment]
indexer=key, value=value
)
Expand Down
16 changes: 16 additions & 0 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,9 @@ def get_handle(
)
else:
handle = gzip.GzipFile(
# error: Argument "fileobj" to "GzipFile" has incompatible type
# "Union[str, Union[IO[Any], RawIOBase, BufferedIOBase, TextIOBase,
# TextIOWrapper, mmap]]"; expected "Optional[IO[bytes]]"
fileobj=handle, # type: ignore[arg-type]
mode=ioargs.mode,
**compression_args,
Expand All @@ -633,6 +636,10 @@ def get_handle(
# BZ Compression
elif compression == "bz2":
handle = bz2.BZ2File(
# Argument 1 to "BZ2File" has incompatible type "Union[str,
# Union[IO[Any], RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper,
# mmap]]"; expected "Union[Union[str, bytes, _PathLike[str],
# _PathLike[bytes]], IO[bytes]]"
handle, # type: ignore[arg-type]
mode=ioargs.mode,
**compression_args,
Expand Down Expand Up @@ -690,6 +697,9 @@ def get_handle(
is_wrapped = False
if is_text and (compression or _is_binary_mode(handle, ioargs.mode)):
handle = TextIOWrapper(
# error: Argument 1 to "TextIOWrapper" has incompatible type
# "Union[IO[bytes], IO[Any], RawIOBase, BufferedIOBase, TextIOBase, mmap]";
# expected "IO[bytes]"
handle, # type: ignore[arg-type]
encoding=ioargs.encoding,
errors=errors,
Expand Down Expand Up @@ -752,6 +762,10 @@ def __init__(
kwargs_zip: Dict[str, Any] = {"compression": zipfile.ZIP_DEFLATED}
kwargs_zip.update(kwargs)

# error: Argument 1 to "__init__" of "ZipFile" has incompatible type
# "Union[_PathLike[str], Union[str, Union[IO[Any], RawIOBase, BufferedIOBase,
# TextIOBase, TextIOWrapper, mmap]]]"; expected "Union[Union[str,
# _PathLike[str]], IO[bytes]]"
super().__init__(file, mode, **kwargs_zip) # type: ignore[arg-type]

def write(self, data):
Expand Down Expand Up @@ -849,6 +863,8 @@ def _maybe_memory_map(
handles.append(handle)

try:
# error: Argument 1 to "_MMapWrapper" has incompatible type "Union[IO[Any],
# RawIOBase, BufferedIOBase, TextIOBase, mmap]"; expected "IO[Any]"
wrapped = cast(mmap.mmap, _MMapWrapper(handle)) # type: ignore[arg-type]
handle.close()
handles.remove(handle)
Expand Down
12 changes: 11 additions & 1 deletion pandas/io/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,19 @@ def to_pickle(
# "zip" would also be here if pandas.io.common._BytesZipFile
# wouldn't buffer write calls
handles.handle.write(
# error: Argument 1 to "write" of "TextIOBase" has incompatible type
# "bytes"; expected "str"
pickle.dumps(obj, protocol=protocol) # type: ignore[arg-type]
)
else:
# letting pickle write directly to the buffer is more memory-efficient
pickle.dump(
obj, handles.handle, protocol=protocol # type: ignore[arg-type]
# error: Argument 2 to "dump" has incompatible type "Union[IO[Any],
# RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap]"; expected
# "IO[bytes]"
obj,
handles.handle, # type: ignore[arg-type]
protocol=protocol,
)


Expand Down Expand Up @@ -204,6 +211,9 @@ def read_pickle(
with warnings.catch_warnings(record=True):
# We want to silence any warnings about, e.g. moved modules.
warnings.simplefilter("ignore", Warning)
# error: Argument 1 to "load" has incompatible type "Union[IO[Any],
# RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap]";
# expected "IO[bytes]"
return pickle.load(handles.handle) # type: ignore[arg-type]
except excs_to_catch:
# e.g.
Expand Down
4 changes: 4 additions & 0 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4022,10 +4022,14 @@ def get_blk_items(mgr):
new_labels = Index(axis_labels).difference(Index(data_columns))
mgr = frame.reindex(new_labels, axis=axis)._mgr

# error: Item "ArrayManager" of "Union[ArrayManager, BlockManager]" has no
# attribute "blocks"
blocks = list(mgr.blocks) # type: ignore[union-attr]
blk_items = get_blk_items(mgr)
for c in data_columns:
mgr = frame.reindex([c], axis=axis)._mgr
# error: Item "ArrayManager" of "Union[ArrayManager, BlockManager]" has
# no attribute "blocks"
blocks.extend(mgr.blocks) # type: ignore[union-attr]
blk_items.extend(get_blk_items(mgr))

Expand Down
3 changes: 3 additions & 0 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,9 @@ def __init__(
compression=compression,
) as handles:
# Copy to BytesIO, and ensure no encoding

# Argument 1 to "BytesIO" has incompatible type "Union[Any, bytes, None,
# str]"; expected "bytes"
self.path_or_buf = BytesIO(handles.handle.read()) # type: ignore[arg-type]

self._read_header()
Expand Down

0 comments on commit 9d0517a

Please sign in to comment.