Skip to content

Commit 0afde72

Browse files
TYP: tidy comments for # type: ignore (#39794)
1 parent 9f792cd commit 0afde72

35 files changed

+159
-261
lines changed

pandas/_testing/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def makeCustomIndex(
559559
"p": makePeriodIndex,
560560
}.get(idx_type)
561561
if idx_func:
562-
# pandas\_testing.py:2120: error: Cannot call function of unknown type
562+
# error: Cannot call function of unknown type
563563
idx = idx_func(nentries) # type: ignore[operator]
564564
# but we need to fill in the name
565565
if names:

pandas/_testing/_io.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ def dec(f):
8282
is_decorating = not kwargs and len(args) == 1 and callable(args[0])
8383
if is_decorating:
8484
f = args[0]
85-
# pandas\_testing.py:2331: error: Incompatible types in assignment
86-
# (expression has type "List[<nothing>]", variable has type
87-
# "Tuple[Any, ...]")
85+
# error: Incompatible types in assignment (expression has type
86+
# "List[<nothing>]", variable has type "Tuple[Any, ...]")
8887
args = [] # type: ignore[assignment]
8988
return dec(f)
9089
else:
@@ -205,8 +204,7 @@ def wrapper(*args, **kwargs):
205204
except Exception as err:
206205
errno = getattr(err, "errno", None)
207206
if not errno and hasattr(errno, "reason"):
208-
# pandas\_testing.py:2521: error: "Exception" has no attribute
209-
# "reason"
207+
# error: "Exception" has no attribute "reason"
210208
errno = getattr(err.reason, "errno", None) # type: ignore[attr-defined]
211209

212210
if errno in skip_errnos:

pandas/core/arrays/datetimelike.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,7 @@ def _validate_comparison_value(self, other):
445445
raise InvalidComparison(other)
446446

447447
if isinstance(other, self._recognized_scalars) or other is NaT:
448-
# pandas\core\arrays\datetimelike.py:432: error: Too many arguments
449-
# for "object" [call-arg]
448+
# error: Too many arguments for "object"
450449
other = self._scalar_type(other) # type: ignore[call-arg]
451450
try:
452451
self._check_compatible_with(other)
@@ -497,17 +496,15 @@ def _validate_shift_value(self, fill_value):
497496
if is_valid_na_for_dtype(fill_value, self.dtype):
498497
fill_value = NaT
499498
elif isinstance(fill_value, self._recognized_scalars):
500-
# pandas\core\arrays\datetimelike.py:746: error: Too many arguments
501-
# for "object" [call-arg]
499+
# error: Too many arguments for "object"
502500
fill_value = self._scalar_type(fill_value) # type: ignore[call-arg]
503501
else:
504502
# only warn if we're not going to raise
505503
if self._scalar_type is Period and lib.is_integer(fill_value):
506504
# kludge for #31971 since Period(integer) tries to cast to str
507505
new_fill = Period._from_ordinal(fill_value, freq=self.freq)
508506
else:
509-
# pandas\core\arrays\datetimelike.py:753: error: Too many
510-
# arguments for "object" [call-arg]
507+
# error: Too many arguments for "object"
511508
new_fill = self._scalar_type(fill_value) # type: ignore[call-arg]
512509

513510
# stacklevel here is chosen to be correct when called from
@@ -563,7 +560,7 @@ def _validate_scalar(
563560
value = NaT
564561

565562
elif isinstance(value, self._recognized_scalars):
566-
# error: Too many arguments for "object" [call-arg]
563+
# error: Too many arguments for "object"
567564
value = self._scalar_type(value) # type: ignore[call-arg]
568565

569566
else:
@@ -1679,7 +1676,7 @@ def factorize(self, na_sentinel=-1, sort: bool = False):
16791676
# TODO: overload __getitem__, a slice indexer returns same type as self
16801677
# error: Incompatible types in assignment (expression has type
16811678
# "Union[DatetimeLikeArrayMixin, Union[Any, Any]]", variable
1682-
# has type "TimelikeOps") [assignment]
1679+
# has type "TimelikeOps")
16831680
uniques = uniques[::-1] # type: ignore[assignment]
16841681
return codes, uniques
16851682
# FIXME: shouldn't get here; we are ignoring sort

pandas/core/arrays/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ def isin(self, values) -> np.ndarray:
15081508
# GH#38353 instead of casting to object, operating on a
15091509
# complex128 ndarray is much more performant.
15101510

1511-
# error: "ArrayLike" has no attribute "view" [attr-defined]
1511+
# error: "ArrayLike" has no attribute "view"
15121512
left = self._combined.view("complex128") # type:ignore[attr-defined]
15131513
right = values._combined.view("complex128")
15141514
return np.in1d(left, right)

pandas/core/arrays/string_.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,8 @@ def __init__(self, values, copy=False):
190190
values = extract_array(values)
191191

192192
super().__init__(values, copy=copy)
193-
# pandas\core\arrays\string_.py:188: error: Incompatible types in
194-
# assignment (expression has type "StringDtype", variable has type
195-
# "PandasDtype") [assignment]
193+
# error: Incompatible types in assignment (expression has type "StringDtype",
194+
# variable has type "PandasDtype")
196195
self._dtype = StringDtype() # type: ignore[assignment]
197196
if not isinstance(values, type(self)):
198197
self._validate()

pandas/core/base.py

+22-41
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ def __sizeof__(self):
9595
either a value or Series of values
9696
"""
9797
if hasattr(self, "memory_usage"):
98-
# pandas\core\base.py:84: error: "PandasObject" has no attribute
99-
# "memory_usage" [attr-defined]
98+
# error: "PandasObject" has no attribute "memory_usage"
10099
mem = self.memory_usage(deep=True) # type: ignore[attr-defined]
101100
return int(mem if is_scalar(mem) else mem.sum())
102101

@@ -206,17 +205,14 @@ def _selection_list(self):
206205

207206
@cache_readonly
208207
def _selected_obj(self):
209-
# pandas\core\base.py:195: error: "SelectionMixin" has no attribute
210-
# "obj" [attr-defined]
208+
# error: "SelectionMixin" has no attribute "obj"
211209
if self._selection is None or isinstance(
212210
self.obj, ABCSeries # type: ignore[attr-defined]
213211
):
214-
# pandas\core\base.py:194: error: "SelectionMixin" has no attribute
215-
# "obj" [attr-defined]
212+
# error: "SelectionMixin" has no attribute "obj"
216213
return self.obj # type: ignore[attr-defined]
217214
else:
218-
# pandas\core\base.py:204: error: "SelectionMixin" has no attribute
219-
# "obj" [attr-defined]
215+
# error: "SelectionMixin" has no attribute "obj"
220216
return self.obj[self._selection] # type: ignore[attr-defined]
221217

222218
@cache_readonly
@@ -225,57 +221,48 @@ def ndim(self) -> int:
225221

226222
@cache_readonly
227223
def _obj_with_exclusions(self):
228-
# pandas\core\base.py:209: error: "SelectionMixin" has no attribute
229-
# "obj" [attr-defined]
224+
# error: "SelectionMixin" has no attribute "obj"
230225
if self._selection is not None and isinstance(
231226
self.obj, ABCDataFrame # type: ignore[attr-defined]
232227
):
233-
# pandas\core\base.py:217: error: "SelectionMixin" has no attribute
234-
# "obj" [attr-defined]
228+
# error: "SelectionMixin" has no attribute "obj"
235229
return self.obj.reindex( # type: ignore[attr-defined]
236230
columns=self._selection_list
237231
)
238232

239-
# pandas\core\base.py:207: error: "SelectionMixin" has no attribute
240-
# "exclusions" [attr-defined]
233+
# error: "SelectionMixin" has no attribute "exclusions"
241234
if len(self.exclusions) > 0: # type: ignore[attr-defined]
242-
# pandas\core\base.py:208: error: "SelectionMixin" has no attribute
243-
# "obj" [attr-defined]
244-
245-
# pandas\core\base.py:208: error: "SelectionMixin" has no attribute
246-
# "exclusions" [attr-defined]
235+
# error: "SelectionMixin" has no attribute "obj"
236+
# error: "SelectionMixin" has no attribute "exclusions"
247237
return self.obj.drop(self.exclusions, axis=1) # type: ignore[attr-defined]
248238
else:
249-
# pandas\core\base.py:210: error: "SelectionMixin" has no attribute
250-
# "obj" [attr-defined]
239+
# error: "SelectionMixin" has no attribute "obj"
251240
return self.obj # type: ignore[attr-defined]
252241

253242
def __getitem__(self, key):
254243
if self._selection is not None:
255244
raise IndexError(f"Column(s) {self._selection} already selected")
256245

257246
if isinstance(key, (list, tuple, ABCSeries, ABCIndex, np.ndarray)):
258-
# pandas\core\base.py:217: error: "SelectionMixin" has no attribute
259-
# "obj" [attr-defined]
247+
# error: "SelectionMixin" has no attribute "obj"
260248
if len(
261249
self.obj.columns.intersection(key) # type: ignore[attr-defined]
262250
) != len(key):
263-
# pandas\core\base.py:218: error: "SelectionMixin" has no
264-
# attribute "obj" [attr-defined]
251+
# error: "SelectionMixin" has no attribute "obj"
265252
bad_keys = list(
266253
set(key).difference(self.obj.columns) # type: ignore[attr-defined]
267254
)
268255
raise KeyError(f"Columns not found: {str(bad_keys)[1:-1]}")
269256
return self._gotitem(list(key), ndim=2)
270257

271258
elif not getattr(self, "as_index", False):
272-
# error: "SelectionMixin" has no attribute "obj" [attr-defined]
259+
# error: "SelectionMixin" has no attribute "obj"
273260
if key not in self.obj.columns: # type: ignore[attr-defined]
274261
raise KeyError(f"Column not found: {key}")
275262
return self._gotitem(key, ndim=2)
276263

277264
else:
278-
# error: "SelectionMixin" has no attribute "obj" [attr-defined]
265+
# error: "SelectionMixin" has no attribute "obj"
279266
if key not in self.obj: # type: ignore[attr-defined]
280267
raise KeyError(f"Column not found: {key}")
281268
return self._gotitem(key, ndim=1)
@@ -601,8 +588,7 @@ def to_numpy(
601588
dtype='datetime64[ns]')
602589
"""
603590
if is_extension_array_dtype(self.dtype):
604-
# pandas\core\base.py:837: error: Too many arguments for "to_numpy"
605-
# of "ExtensionArray" [call-arg]
591+
# error: Too many arguments for "to_numpy" of "ExtensionArray"
606592
return self.array.to_numpy( # type: ignore[call-arg]
607593
dtype, copy=copy, na_value=na_value, **kwargs
608594
)
@@ -914,13 +900,11 @@ def _map_values(self, mapper, na_action=None):
914900
# use the built in categorical series mapper which saves
915901
# time by mapping the categories instead of all values
916902

917-
# pandas\core\base.py:893: error: Incompatible types in
918-
# assignment (expression has type "Categorical", variable has
919-
# type "IndexOpsMixin") [assignment]
903+
# error: Incompatible types in assignment (expression has type
904+
# "Categorical", variable has type "IndexOpsMixin")
920905
self = cast("Categorical", self) # type: ignore[assignment]
921-
# pandas\core\base.py:894: error: Item "ExtensionArray" of
922-
# "Union[ExtensionArray, Any]" has no attribute "map"
923-
# [union-attr]
906+
# error: Item "ExtensionArray" of "Union[ExtensionArray, Any]" has no
907+
# attribute "map"
924908
return self._values.map(mapper) # type: ignore[union-attr]
925909

926910
values = self._values
@@ -938,8 +922,7 @@ def _map_values(self, mapper, na_action=None):
938922
raise NotImplementedError
939923
map_f = lambda values, f: values.map(f)
940924
else:
941-
# pandas\core\base.py:1142: error: "IndexOpsMixin" has no attribute
942-
# "astype" [attr-defined]
925+
# error: "IndexOpsMixin" has no attribute "astype"
943926
values = self.astype(object)._values # type: ignore[attr-defined]
944927
if na_action == "ignore":
945928
map_f = lambda values, f: lib.map_infer_mask(
@@ -1177,8 +1160,7 @@ def memory_usage(self, deep=False):
11771160
are not components of the array if deep=False or if used on PyPy
11781161
"""
11791162
if hasattr(self.array, "memory_usage"):
1180-
# pandas\core\base.py:1379: error: "ExtensionArray" has no
1181-
# attribute "memory_usage" [attr-defined]
1163+
# error: "ExtensionArray" has no attribute "memory_usage"
11821164
return self.array.memory_usage(deep=deep) # type: ignore[attr-defined]
11831165

11841166
v = self.array.nbytes
@@ -1313,8 +1295,7 @@ def searchsorted(self, value, side="left", sorter=None) -> np.ndarray:
13131295

13141296
def drop_duplicates(self, keep="first"):
13151297
duplicated = self.duplicated(keep=keep)
1316-
# pandas\core\base.py:1507: error: Value of type "IndexOpsMixin" is not
1317-
# indexable [index]
1298+
# error: Value of type "IndexOpsMixin" is not indexable
13181299
return self[~duplicated] # type: ignore[index]
13191300

13201301
def duplicated(self, keep="first"):

pandas/core/common.py

-4
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,6 @@ def maybe_iterable_to_list(obj: Union[Iterable[T], T]) -> Union[Collection[T], T
268268
"""
269269
if isinstance(obj, abc.Iterable) and not isinstance(obj, abc.Sized):
270270
return list(obj)
271-
# error: Incompatible return value type (got
272-
# "Union[pandas.core.common.<subclass of "Iterable" and "Sized">,
273-
# pandas.core.common.<subclass of "Iterable" and "Sized">1, T]", expected
274-
# "Union[Collection[T], T]") [return-value]
275271
obj = cast(Collection, obj)
276272
return obj
277273

pandas/core/computation/expr.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,7 @@ def visit_Call(self, node, side=None, **kwargs):
659659
raise
660660

661661
if res is None:
662-
# pandas\core\computation\expr.py:663: error: "expr" has no
663-
# attribute "id" [attr-defined]
662+
# error: "expr" has no attribute "id"
664663
raise ValueError(
665664
f"Invalid function call {node.func.id}" # type: ignore[attr-defined]
666665
)
@@ -684,8 +683,7 @@ def visit_Call(self, node, side=None, **kwargs):
684683

685684
for key in node.keywords:
686685
if not isinstance(key, ast.keyword):
687-
# pandas\core\computation\expr.py:684: error: "expr" has no
688-
# attribute "id" [attr-defined]
686+
# error: "expr" has no attribute "id"
689687
raise ValueError(
690688
"keyword error in function call " # type: ignore[attr-defined]
691689
f"'{node.func.id}'"

pandas/core/computation/ops.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def __init__(self, name: str, is_local: Optional[bool] = None):
7171
class Term:
7272
def __new__(cls, name, env, side=None, encoding=None):
7373
klass = Constant if not isinstance(name, str) else cls
74-
# pandas\core\computation\ops.py:72: error: Argument 2 for "super" not
75-
# an instance of argument 1 [misc]
74+
# error: Argument 2 for "super" not an instance of argument 1
7675
supr_new = super(Term, klass).__new__ # type: ignore[misc]
7776
return supr_new(klass)
7877

@@ -593,7 +592,7 @@ def __init__(self, func, args):
593592
self.func = func
594593

595594
def __call__(self, env):
596-
# pandas\core\computation\ops.py:592: error: "Op" not callable [operator]
595+
# error: "Op" not callable
597596
operands = [op(env) for op in self.operands] # type: ignore[operator]
598597
with np.errstate(all="ignore"):
599598
return self.func.func(*operands)

pandas/core/computation/scope.py

+12-22
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,14 @@ def __init__(
131131
# scope when we align terms (alignment accesses the underlying
132132
# numpy array of pandas objects)
133133

134-
# pandas\core\computation\scope.py:132: error: Incompatible types
135-
# in assignment (expression has type "ChainMap[str, Any]", variable
136-
# has type "DeepChainMap[str, Any]") [assignment]
134+
# error: Incompatible types in assignment (expression has type
135+
# "ChainMap[str, Any]", variable has type "DeepChainMap[str, Any]")
137136
self.scope = self.scope.new_child( # type: ignore[assignment]
138137
(global_dict or frame.f_globals).copy()
139138
)
140139
if not isinstance(local_dict, Scope):
141-
# pandas\core\computation\scope.py:134: error: Incompatible
142-
# types in assignment (expression has type "ChainMap[str,
143-
# Any]", variable has type "DeepChainMap[str, Any]")
144-
# [assignment]
140+
# error: Incompatible types in assignment (expression has type
141+
# "ChainMap[str, Any]", variable has type "DeepChainMap[str, Any]")
145142
self.scope = self.scope.new_child( # type: ignore[assignment]
146143
(local_dict or frame.f_locals).copy()
147144
)
@@ -150,8 +147,7 @@ def __init__(
150147

151148
# assumes that resolvers are going from outermost scope to inner
152149
if isinstance(local_dict, Scope):
153-
# pandas\core\computation\scope.py:140: error: Cannot determine
154-
# type of 'resolvers' [has-type]
150+
# error: Cannot determine type of 'resolvers'
155151
resolvers += tuple(local_dict.resolvers.maps) # type: ignore[has-type]
156152
self.resolvers = DeepChainMap(*resolvers)
157153
self.temps = {}
@@ -239,8 +235,7 @@ def swapkey(self, old_key: str, new_key: str, new_value=None):
239235

240236
for mapping in maps:
241237
if old_key in mapping:
242-
# pandas\core\computation\scope.py:228: error: Unsupported
243-
# target for indexed assignment ("Mapping[Any, Any]") [index]
238+
# error: Unsupported target for indexed assignment ("Mapping[Any, Any]")
244239
mapping[new_key] = new_value # type: ignore[index]
245240
return
246241

@@ -260,10 +255,8 @@ def _get_vars(self, stack, scopes: List[str]):
260255
for scope, (frame, _, _, _, _, _) in variables:
261256
try:
262257
d = getattr(frame, "f_" + scope)
263-
# pandas\core\computation\scope.py:247: error: Incompatible
264-
# types in assignment (expression has type "ChainMap[str,
265-
# Any]", variable has type "DeepChainMap[str, Any]")
266-
# [assignment]
258+
# error: Incompatible types in assignment (expression has type
259+
# "ChainMap[str, Any]", variable has type "DeepChainMap[str, Any]")
267260
self.scope = self.scope.new_child(d) # type: ignore[assignment]
268261
finally:
269262
# won't remove it, but DECREF it
@@ -331,13 +324,10 @@ def full_scope(self):
331324
vars : DeepChainMap
332325
All variables in this scope.
333326
"""
334-
# pandas\core\computation\scope.py:314: error: Unsupported operand
335-
# types for + ("List[Dict[Any, Any]]" and "List[Mapping[Any, Any]]")
336-
# [operator]
337-
338-
# pandas\core\computation\scope.py:314: error: Unsupported operand
339-
# types for + ("List[Dict[Any, Any]]" and "List[Mapping[str, Any]]")
340-
# [operator]
327+
# error: Unsupported operand types for + ("List[Dict[Any, Any]]" and
328+
# "List[Mapping[Any, Any]]")
329+
# error: Unsupported operand types for + ("List[Dict[Any, Any]]" and
330+
# "List[Mapping[str, Any]]")
341331
maps = (
342332
[self.temps]
343333
+ self.resolvers.maps # type: ignore[operator]

0 commit comments

Comments
 (0)