Skip to content

Commit 9d0517a

Browse files
TYP: add mypy messages as comments to ignores (#40268)
1 parent 97b832b commit 9d0517a

13 files changed

+91
-7
lines changed

pandas/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ def frame_or_series(request):
326326
return request.param
327327

328328

329+
# error: List item 0 has incompatible type "Type[Index]"; expected "Type[IndexOpsMixin]"
329330
@pytest.fixture(
330331
params=[pd.Index, pd.Series], ids=["index", "series"] # type: ignore[list-item]
331332
)

pandas/core/arrays/boolean.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class BooleanDtype(BaseMaskedDtype):
7676

7777
name = "boolean"
7878

79-
# mypy: https://github.com/python/mypy/issues/4125
79+
# https://github.com/python/mypy/issues/4125
80+
# error: Signature of "type" incompatible with supertype "BaseMaskedDtype"
8081
@property
8182
def type(self) -> Type: # type: ignore[override]
8283
return np.bool_

pandas/core/arrays/floating.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ def _get_common_dtype(self, dtypes: List[DtypeObj]) -> Optional[DtypeObj]:
7878
if not all(isinstance(t, FloatingDtype) for t in dtypes):
7979
return None
8080
np_dtype = np.find_common_type(
81-
[t.numpy_dtype for t in dtypes], [] # type: ignore[union-attr]
81+
# error: Item "ExtensionDtype" of "Union[Any, ExtensionDtype]" has no
82+
# attribute "numpy_dtype"
83+
[t.numpy_dtype for t in dtypes], # type: ignore[union-attr]
84+
[],
8285
)
8386
if np.issubdtype(np_dtype, np.floating):
8487
return FLOAT_STR_TO_DTYPE[str(np_dtype)]

pandas/core/common.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,12 @@ def convert_to_list_like(
493493
inputs are returned unmodified whereas others are converted to list.
494494
"""
495495
if isinstance(values, (list, np.ndarray, ABCIndex, ABCSeries, ABCExtensionArray)):
496-
# np.ndarray resolving as Any gives a false positive
496+
# error: Incompatible return value type (got "Union[Any, List[Any], Index,
497+
# Series, ExtensionArray]", expected "Union[List[Any], ExtensionArray]")
498+
# error: Incompatible return value type (got "Union[Any, List[Any], Index,
499+
# Series, ExtensionArray]", expected "Union[List[Any], Index]")
500+
# error: Incompatible return value type (got "Union[Any, List[Any], Index,
501+
# Series, ExtensionArray]", expected "Union[List[Any], Series]")
497502
return values # type: ignore[return-value]
498503
elif isinstance(values, abc.Iterable) and not isinstance(values, str):
499504
return list(values)

pandas/core/frame.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -4079,7 +4079,13 @@ def extract_unique_dtypes_from_dtypes_set(
40794079
for unique_dtype in unique_dtypes
40804080
if (
40814081
issubclass(
4082-
unique_dtype.type, tuple(dtypes_set) # type: ignore[arg-type]
4082+
# error: Argument 1 to "tuple" has incompatible type
4083+
# "FrozenSet[Union[ExtensionDtype, Union[str, Any], Type[str],
4084+
# Type[float], Type[int], Type[complex], Type[bool],
4085+
# Type[object]]]"; expected "Iterable[Union[type, Tuple[Any,
4086+
# ...]]]"
4087+
unique_dtype.type,
4088+
tuple(dtypes_set), # type: ignore[arg-type]
40834089
)
40844090
or (
40854091
np.number in dtypes_set
@@ -6382,7 +6388,14 @@ def _dispatch_frame_op(self, right, func, axis: Optional[int] = None):
63826388

63836389
# TODO operate_blockwise expects a manager of the same type
63846390
bm = self._mgr.operate_blockwise(
6385-
right._mgr, array_op # type: ignore[arg-type]
6391+
# error: Argument 1 to "operate_blockwise" of "ArrayManager" has
6392+
# incompatible type "Union[ArrayManager, BlockManager]"; expected
6393+
# "ArrayManager"
6394+
# error: Argument 1 to "operate_blockwise" of "BlockManager" has
6395+
# incompatible type "Union[ArrayManager, BlockManager]"; expected
6396+
# "BlockManager"
6397+
right._mgr, # type: ignore[arg-type]
6398+
array_op,
63866399
)
63876400
return type(self)(bm)
63886401

pandas/core/generic.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,12 @@ def swapaxes(self: FrameOrSeries, axis1, axis2, copy=True) -> FrameOrSeries:
752752
# ignore needed because of NDFrame constructor is different than
753753
# DataFrame/Series constructors.
754754
return self._constructor(
755-
new_values, *new_axes # type: ignore[arg-type]
755+
# error: Argument 2 to "NDFrame" has incompatible type "*Generator[Index,
756+
# None, None]"; expected "bool" [arg-type]
757+
# error: Argument 2 to "NDFrame" has incompatible type "*Generator[Index,
758+
# None, None]"; expected "Optional[Mapping[Optional[Hashable], Any]]"
759+
new_values,
760+
*new_axes, # type: ignore[arg-type]
756761
).__finalize__(self, method="swapaxes")
757762

758763
@final
@@ -11046,37 +11051,47 @@ def _inplace_method(self, other, op):
1104611051
return self
1104711052

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

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

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

1105711065
def __itruediv__(self, other):
11066+
# error: Unsupported left operand type for / ("Type[NDFrame]")
1105811067
return self._inplace_method(
1105911068
other, type(self).__truediv__ # type: ignore[operator]
1106011069
)
1106111070

1106211071
def __ifloordiv__(self, other):
11072+
# error: Unsupported left operand type for // ("Type[NDFrame]")
1106311073
return self._inplace_method(
1106411074
other, type(self).__floordiv__ # type: ignore[operator]
1106511075
)
1106611076

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

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

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

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

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

1108211097
# ----------------------------------------------------------------------

pandas/core/internals/array_manager.py

+9
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def items(self) -> Index:
148148
return self._axes[-1]
149149

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

458+
# error: Item "ExtensionArray" of "Union[Any, ExtensionArray]" has no
459+
# attribute "tz"
457460
if hasattr(arr, "tz") and arr.tz is None: # type: ignore[union-attr]
458461
# DatetimeArray needs to be converted to ndarray for DatetimeBlock
462+
463+
# error: Item "ExtensionArray" of "Union[Any, ExtensionArray]" has no
464+
# attribute "_data"
459465
arr = arr._data # type: ignore[union-attr]
460466
elif arr.dtype.kind == "m" and not isinstance(arr, np.ndarray):
461467
# TimedeltaArray needs to be converted to ndarray for TimedeltaBlock
468+
469+
# error: Item "ExtensionArray" of "Union[Any, ExtensionArray]" has no
470+
# attribute "_data"
462471
arr = arr._data # type: ignore[union-attr]
463472

464473
if self.ndim == 2:

pandas/core/nanops.py

+1
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,7 @@ def na_accum_func(values: ArrayLike, accum_func, *, skipna: bool) -> ArrayLike:
17481748
# TODO: have this case go through a DTA method?
17491749
# For DatetimeTZDtype, view result as M8[ns]
17501750
npdtype = orig_dtype if isinstance(orig_dtype, np.dtype) else "M8[ns]"
1751+
# error: "Type[ExtensionArray]" has no attribute "_simple_new"
17511752
result = type(values)._simple_new( # type: ignore[attr-defined]
17521753
result.view(npdtype), dtype=orig_dtype
17531754
)

pandas/core/series.py

+3
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,9 @@ def _set_labels(self, key, value):
10941094
def _set_values(self, key, value):
10951095
if isinstance(key, Series):
10961096
key = key._values
1097+
# error: Incompatible types in assignment (expression has type "Union[Any,
1098+
# BlockManager]", variable has type "Union[SingleArrayManager,
1099+
# SingleBlockManager]")
10971100
self._mgr = self._mgr.setitem( # type: ignore[assignment]
10981101
indexer=key, value=value
10991102
)

pandas/io/common.py

+16
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,9 @@ def get_handle(
625625
)
626626
else:
627627
handle = gzip.GzipFile(
628+
# error: Argument "fileobj" to "GzipFile" has incompatible type
629+
# "Union[str, Union[IO[Any], RawIOBase, BufferedIOBase, TextIOBase,
630+
# TextIOWrapper, mmap]]"; expected "Optional[IO[bytes]]"
628631
fileobj=handle, # type: ignore[arg-type]
629632
mode=ioargs.mode,
630633
**compression_args,
@@ -633,6 +636,10 @@ def get_handle(
633636
# BZ Compression
634637
elif compression == "bz2":
635638
handle = bz2.BZ2File(
639+
# Argument 1 to "BZ2File" has incompatible type "Union[str,
640+
# Union[IO[Any], RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper,
641+
# mmap]]"; expected "Union[Union[str, bytes, _PathLike[str],
642+
# _PathLike[bytes]], IO[bytes]]"
636643
handle, # type: ignore[arg-type]
637644
mode=ioargs.mode,
638645
**compression_args,
@@ -690,6 +697,9 @@ def get_handle(
690697
is_wrapped = False
691698
if is_text and (compression or _is_binary_mode(handle, ioargs.mode)):
692699
handle = TextIOWrapper(
700+
# error: Argument 1 to "TextIOWrapper" has incompatible type
701+
# "Union[IO[bytes], IO[Any], RawIOBase, BufferedIOBase, TextIOBase, mmap]";
702+
# expected "IO[bytes]"
693703
handle, # type: ignore[arg-type]
694704
encoding=ioargs.encoding,
695705
errors=errors,
@@ -752,6 +762,10 @@ def __init__(
752762
kwargs_zip: Dict[str, Any] = {"compression": zipfile.ZIP_DEFLATED}
753763
kwargs_zip.update(kwargs)
754764

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

757771
def write(self, data):
@@ -849,6 +863,8 @@ def _maybe_memory_map(
849863
handles.append(handle)
850864

851865
try:
866+
# error: Argument 1 to "_MMapWrapper" has incompatible type "Union[IO[Any],
867+
# RawIOBase, BufferedIOBase, TextIOBase, mmap]"; expected "IO[Any]"
852868
wrapped = cast(mmap.mmap, _MMapWrapper(handle)) # type: ignore[arg-type]
853869
handle.close()
854870
handles.remove(handle)

pandas/io/pickle.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,19 @@ def to_pickle(
104104
# "zip" would also be here if pandas.io.common._BytesZipFile
105105
# wouldn't buffer write calls
106106
handles.handle.write(
107+
# error: Argument 1 to "write" of "TextIOBase" has incompatible type
108+
# "bytes"; expected "str"
107109
pickle.dumps(obj, protocol=protocol) # type: ignore[arg-type]
108110
)
109111
else:
110112
# letting pickle write directly to the buffer is more memory-efficient
111113
pickle.dump(
112-
obj, handles.handle, protocol=protocol # type: ignore[arg-type]
114+
# error: Argument 2 to "dump" has incompatible type "Union[IO[Any],
115+
# RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap]"; expected
116+
# "IO[bytes]"
117+
obj,
118+
handles.handle, # type: ignore[arg-type]
119+
protocol=protocol,
113120
)
114121

115122

@@ -204,6 +211,9 @@ def read_pickle(
204211
with warnings.catch_warnings(record=True):
205212
# We want to silence any warnings about, e.g. moved modules.
206213
warnings.simplefilter("ignore", Warning)
214+
# error: Argument 1 to "load" has incompatible type "Union[IO[Any],
215+
# RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap]";
216+
# expected "IO[bytes]"
207217
return pickle.load(handles.handle) # type: ignore[arg-type]
208218
except excs_to_catch:
209219
# e.g.

pandas/io/pytables.py

+4
Original file line numberDiff line numberDiff line change
@@ -4022,10 +4022,14 @@ def get_blk_items(mgr):
40224022
new_labels = Index(axis_labels).difference(Index(data_columns))
40234023
mgr = frame.reindex(new_labels, axis=axis)._mgr
40244024

4025+
# error: Item "ArrayManager" of "Union[ArrayManager, BlockManager]" has no
4026+
# attribute "blocks"
40254027
blocks = list(mgr.blocks) # type: ignore[union-attr]
40264028
blk_items = get_blk_items(mgr)
40274029
for c in data_columns:
40284030
mgr = frame.reindex([c], axis=axis)._mgr
4031+
# error: Item "ArrayManager" of "Union[ArrayManager, BlockManager]" has
4032+
# no attribute "blocks"
40294033
blocks.extend(mgr.blocks) # type: ignore[union-attr]
40304034
blk_items.extend(get_blk_items(mgr))
40314035

pandas/io/stata.py

+3
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,9 @@ def __init__(
10911091
compression=compression,
10921092
) as handles:
10931093
# Copy to BytesIO, and ensure no encoding
1094+
1095+
# Argument 1 to "BytesIO" has incompatible type "Union[Any, bytes, None,
1096+
# str]"; expected "bytes"
10941097
self.path_or_buf = BytesIO(handles.handle.read()) # type: ignore[arg-type]
10951098

10961099
self._read_header()

0 commit comments

Comments
 (0)