Skip to content

Commit 8b6f091

Browse files
TYP: Check untyped defs (except vendored) (#37556)
1 parent 82cd86c commit 8b6f091

27 files changed

+616
-232
lines changed

pandas/_testing.py

+54-14
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,24 @@ def set_testing_mode():
117117
# set the testing mode filters
118118
testing_mode = os.environ.get("PANDAS_TESTING_MODE", "None")
119119
if "deprecate" in testing_mode:
120-
warnings.simplefilter("always", _testing_mode_warnings)
120+
# pandas\_testing.py:119: error: Argument 2 to "simplefilter" has
121+
# incompatible type "Tuple[Type[DeprecationWarning],
122+
# Type[ResourceWarning]]"; expected "Type[Warning]"
123+
warnings.simplefilter(
124+
"always", _testing_mode_warnings # type: ignore[arg-type]
125+
)
121126

122127

123128
def reset_testing_mode():
124129
# reset the testing mode filters
125130
testing_mode = os.environ.get("PANDAS_TESTING_MODE", "None")
126131
if "deprecate" in testing_mode:
127-
warnings.simplefilter("ignore", _testing_mode_warnings)
132+
# pandas\_testing.py:126: error: Argument 2 to "simplefilter" has
133+
# incompatible type "Tuple[Type[DeprecationWarning],
134+
# Type[ResourceWarning]]"; expected "Type[Warning]"
135+
warnings.simplefilter(
136+
"ignore", _testing_mode_warnings # type: ignore[arg-type]
137+
)
128138

129139

130140
set_testing_mode()
@@ -241,16 +251,22 @@ def decompress_file(path, compression):
241251
if compression is None:
242252
f = open(path, "rb")
243253
elif compression == "gzip":
244-
f = gzip.open(path, "rb")
254+
# pandas\_testing.py:243: error: Incompatible types in assignment
255+
# (expression has type "IO[Any]", variable has type "BinaryIO")
256+
f = gzip.open(path, "rb") # type: ignore[assignment]
245257
elif compression == "bz2":
246-
f = bz2.BZ2File(path, "rb")
258+
# pandas\_testing.py:245: error: Incompatible types in assignment
259+
# (expression has type "BZ2File", variable has type "BinaryIO")
260+
f = bz2.BZ2File(path, "rb") # type: ignore[assignment]
247261
elif compression == "xz":
248262
f = get_lzma_file(lzma)(path, "rb")
249263
elif compression == "zip":
250264
zip_file = zipfile.ZipFile(path)
251265
zip_names = zip_file.namelist()
252266
if len(zip_names) == 1:
253-
f = zip_file.open(zip_names.pop())
267+
# pandas\_testing.py:252: error: Incompatible types in assignment
268+
# (expression has type "IO[bytes]", variable has type "BinaryIO")
269+
f = zip_file.open(zip_names.pop()) # type: ignore[assignment]
254270
else:
255271
raise ValueError(f"ZIP file {path} error. Only one file per ZIP.")
256272
else:
@@ -286,9 +302,15 @@ def write_to_compressed(compression, path, data, dest="test"):
286302
if compression == "zip":
287303
compress_method = zipfile.ZipFile
288304
elif compression == "gzip":
289-
compress_method = gzip.GzipFile
305+
# pandas\_testing.py:288: error: Incompatible types in assignment
306+
# (expression has type "Type[GzipFile]", variable has type
307+
# "Type[ZipFile]")
308+
compress_method = gzip.GzipFile # type: ignore[assignment]
290309
elif compression == "bz2":
291-
compress_method = bz2.BZ2File
310+
# pandas\_testing.py:290: error: Incompatible types in assignment
311+
# (expression has type "Type[BZ2File]", variable has type
312+
# "Type[ZipFile]")
313+
compress_method = bz2.BZ2File # type: ignore[assignment]
292314
elif compression == "xz":
293315
compress_method = get_lzma_file(lzma)
294316
else:
@@ -300,7 +322,10 @@ def write_to_compressed(compression, path, data, dest="test"):
300322
method = "writestr"
301323
else:
302324
mode = "wb"
303-
args = (data,)
325+
# pandas\_testing.py:302: error: Incompatible types in assignment
326+
# (expression has type "Tuple[Any]", variable has type "Tuple[Any,
327+
# Any]")
328+
args = (data,) # type: ignore[assignment]
304329
method = "write"
305330

306331
with compress_method(path, mode=mode) as f:
@@ -1996,7 +2021,8 @@ def all_timeseries_index_generator(k=10):
19962021
"""
19972022
make_index_funcs = [makeDateIndex, makePeriodIndex, makeTimedeltaIndex]
19982023
for make_index_func in make_index_funcs:
1999-
yield make_index_func(k=k)
2024+
# pandas\_testing.py:1986: error: Cannot call function of unknown type
2025+
yield make_index_func(k=k) # type: ignore[operator]
20002026

20012027

20022028
# make series
@@ -2130,7 +2156,8 @@ def makeCustomIndex(
21302156
p=makePeriodIndex,
21312157
).get(idx_type)
21322158
if idx_func:
2133-
idx = idx_func(nentries)
2159+
# pandas\_testing.py:2120: error: Cannot call function of unknown type
2160+
idx = idx_func(nentries) # type: ignore[operator]
21342161
# but we need to fill in the name
21352162
if names:
21362163
idx.name = names[0]
@@ -2158,7 +2185,8 @@ def keyfunc(x):
21582185

21592186
# build a list of lists to create the index from
21602187
div_factor = nentries // ndupe_l[i] + 1
2161-
cnt = Counter()
2188+
# pandas\_testing.py:2148: error: Need type annotation for 'cnt'
2189+
cnt = Counter() # type: ignore[var-annotated]
21622190
for j in range(div_factor):
21632191
label = f"{prefix}_l{i}_g{j}"
21642192
cnt[label] = ndupe_l[i]
@@ -2316,7 +2344,14 @@ def _gen_unique_rand(rng, _extra_size):
23162344

23172345
def makeMissingDataframe(density=0.9, random_state=None):
23182346
df = makeDataFrame()
2319-
i, j = _create_missing_idx(*df.shape, density=density, random_state=random_state)
2347+
# pandas\_testing.py:2306: error: "_create_missing_idx" gets multiple
2348+
# values for keyword argument "density" [misc]
2349+
2350+
# pandas\_testing.py:2306: error: "_create_missing_idx" gets multiple
2351+
# values for keyword argument "random_state" [misc]
2352+
i, j = _create_missing_idx( # type: ignore[misc]
2353+
*df.shape, density=density, random_state=random_state
2354+
)
23202355
df.values[i, j] = np.nan
23212356
return df
23222357

@@ -2341,7 +2376,10 @@ def dec(f):
23412376
is_decorating = not kwargs and len(args) == 1 and callable(args[0])
23422377
if is_decorating:
23432378
f = args[0]
2344-
args = []
2379+
# pandas\_testing.py:2331: error: Incompatible types in assignment
2380+
# (expression has type "List[<nothing>]", variable has type
2381+
# "Tuple[Any, ...]")
2382+
args = [] # type: ignore[assignment]
23452383
return dec(f)
23462384
else:
23472385
return dec
@@ -2534,7 +2572,9 @@ def wrapper(*args, **kwargs):
25342572
except Exception as err:
25352573
errno = getattr(err, "errno", None)
25362574
if not errno and hasattr(errno, "reason"):
2537-
errno = getattr(err.reason, "errno", None)
2575+
# pandas\_testing.py:2521: error: "Exception" has no attribute
2576+
# "reason"
2577+
errno = getattr(err.reason, "errno", None) # type: ignore[attr-defined]
25382578

25392579
if errno in skip_errnos:
25402580
skip(f"Skipping test due to known errno and error {err}")

pandas/core/apply.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ def get_result(self):
141141
""" compute the results """
142142
# dispatch to agg
143143
if is_list_like(self.f) or is_dict_like(self.f):
144-
return self.obj.aggregate(self.f, axis=self.axis, *self.args, **self.kwds)
144+
# pandas\core\apply.py:144: error: "aggregate" of "DataFrame" gets
145+
# multiple values for keyword argument "axis"
146+
return self.obj.aggregate( # type: ignore[misc]
147+
self.f, axis=self.axis, *self.args, **self.kwds
148+
)
145149

146150
# all empty
147151
if len(self.columns) == 0 and len(self.index) == 0:

pandas/core/arrays/datetimelike.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,9 @@ def _validate_comparison_value(self, other):
431431
raise InvalidComparison(other)
432432

433433
if isinstance(other, self._recognized_scalars) or other is NaT:
434-
other = self._scalar_type(other)
434+
# pandas\core\arrays\datetimelike.py:432: error: Too many arguments
435+
# for "object" [call-arg]
436+
other = self._scalar_type(other) # type: ignore[call-arg]
435437
try:
436438
self._check_compatible_with(other)
437439
except TypeError as err:
@@ -491,14 +493,18 @@ def _validate_shift_value(self, fill_value):
491493
if is_valid_nat_for_dtype(fill_value, self.dtype):
492494
fill_value = NaT
493495
elif isinstance(fill_value, self._recognized_scalars):
494-
fill_value = self._scalar_type(fill_value)
496+
# pandas\core\arrays\datetimelike.py:746: error: Too many arguments
497+
# for "object" [call-arg]
498+
fill_value = self._scalar_type(fill_value) # type: ignore[call-arg]
495499
else:
496500
# only warn if we're not going to raise
497501
if self._scalar_type is Period and lib.is_integer(fill_value):
498502
# kludge for #31971 since Period(integer) tries to cast to str
499503
new_fill = Period._from_ordinal(fill_value, freq=self.freq)
500504
else:
501-
new_fill = self._scalar_type(fill_value)
505+
# pandas\core\arrays\datetimelike.py:753: error: Too many
506+
# arguments for "object" [call-arg]
507+
new_fill = self._scalar_type(fill_value) # type: ignore[call-arg]
502508

503509
# stacklevel here is chosen to be correct when called from
504510
# DataFrame.shift or Series.shift

pandas/core/arrays/string_.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ def __init__(self, values, copy=False):
186186
values = extract_array(values)
187187

188188
super().__init__(values, copy=copy)
189-
self._dtype = StringDtype()
189+
# pandas\core\arrays\string_.py:188: error: Incompatible types in
190+
# assignment (expression has type "StringDtype", variable has type
191+
# "PandasDtype") [assignment]
192+
self._dtype = StringDtype() # type: ignore[assignment]
190193
if not isinstance(values, type(self)):
191194
self._validate()
192195

pandas/core/base.py

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

101103
# no memory_usage attribute, so fall back to object's 'sizeof'
@@ -204,32 +206,65 @@ def _selection_list(self):
204206

205207
@cache_readonly
206208
def _selected_obj(self):
207-
if self._selection is None or isinstance(self.obj, ABCSeries):
208-
return self.obj
209+
# pandas\core\base.py:195: error: "SelectionMixin" has no attribute
210+
# "obj" [attr-defined]
211+
if self._selection is None or isinstance(
212+
self.obj, ABCSeries # type: ignore[attr-defined]
213+
):
214+
# pandas\core\base.py:194: error: "SelectionMixin" has no attribute
215+
# "obj" [attr-defined]
216+
return self.obj # type: ignore[attr-defined]
209217
else:
210-
return self.obj[self._selection]
218+
# pandas\core\base.py:204: error: "SelectionMixin" has no attribute
219+
# "obj" [attr-defined]
220+
return self.obj[self._selection] # type: ignore[attr-defined]
211221

212222
@cache_readonly
213223
def ndim(self) -> int:
214224
return self._selected_obj.ndim
215225

216226
@cache_readonly
217227
def _obj_with_exclusions(self):
218-
if self._selection is not None and isinstance(self.obj, ABCDataFrame):
219-
return self.obj.reindex(columns=self._selection_list)
228+
# pandas\core\base.py:209: error: "SelectionMixin" has no attribute
229+
# "obj" [attr-defined]
230+
if self._selection is not None and isinstance(
231+
self.obj, ABCDataFrame # type: ignore[attr-defined]
232+
):
233+
# pandas\core\base.py:217: error: "SelectionMixin" has no attribute
234+
# "obj" [attr-defined]
235+
return self.obj.reindex( # type: ignore[attr-defined]
236+
columns=self._selection_list
237+
)
238+
239+
# pandas\core\base.py:207: error: "SelectionMixin" has no attribute
240+
# "exclusions" [attr-defined]
241+
if len(self.exclusions) > 0: # type: ignore[attr-defined]
242+
# pandas\core\base.py:208: error: "SelectionMixin" has no attribute
243+
# "obj" [attr-defined]
220244

221-
if len(self.exclusions) > 0:
222-
return self.obj.drop(self.exclusions, axis=1)
245+
# pandas\core\base.py:208: error: "SelectionMixin" has no attribute
246+
# "exclusions" [attr-defined]
247+
return self.obj.drop(self.exclusions, axis=1) # type: ignore[attr-defined]
223248
else:
224-
return self.obj
249+
# pandas\core\base.py:210: error: "SelectionMixin" has no attribute
250+
# "obj" [attr-defined]
251+
return self.obj # type: ignore[attr-defined]
225252

226253
def __getitem__(self, key):
227254
if self._selection is not None:
228255
raise IndexError(f"Column(s) {self._selection} already selected")
229256

230257
if isinstance(key, (list, tuple, ABCSeries, ABCIndexClass, np.ndarray)):
231-
if len(self.obj.columns.intersection(key)) != len(key):
232-
bad_keys = list(set(key).difference(self.obj.columns))
258+
# pandas\core\base.py:217: error: "SelectionMixin" has no attribute
259+
# "obj" [attr-defined]
260+
if len(
261+
self.obj.columns.intersection(key) # type: ignore[attr-defined]
262+
) != len(key):
263+
# pandas\core\base.py:218: error: "SelectionMixin" has no
264+
# attribute "obj" [attr-defined]
265+
bad_keys = list(
266+
set(key).difference(self.obj.columns) # type: ignore[attr-defined]
267+
)
233268
raise KeyError(f"Columns not found: {str(bad_keys)[1:-1]}")
234269
return self._gotitem(list(key), ndim=2)
235270

@@ -559,7 +594,11 @@ def to_numpy(self, dtype=None, copy=False, na_value=lib.no_default, **kwargs):
559594
dtype='datetime64[ns]')
560595
"""
561596
if is_extension_array_dtype(self.dtype):
562-
return self.array.to_numpy(dtype, copy=copy, na_value=na_value, **kwargs)
597+
# pandas\core\base.py:837: error: Too many arguments for "to_numpy"
598+
# of "ExtensionArray" [call-arg]
599+
return self.array.to_numpy( # type: ignore[call-arg]
600+
dtype, copy=copy, na_value=na_value, **kwargs
601+
)
563602
elif kwargs:
564603
bad_keys = list(kwargs.keys())[0]
565604
raise TypeError(
@@ -851,8 +890,15 @@ def _map_values(self, mapper, na_action=None):
851890
if is_categorical_dtype(self.dtype):
852891
# use the built in categorical series mapper which saves
853892
# time by mapping the categories instead of all values
854-
self = cast("Categorical", self)
855-
return self._values.map(mapper)
893+
894+
# pandas\core\base.py:893: error: Incompatible types in
895+
# assignment (expression has type "Categorical", variable has
896+
# type "IndexOpsMixin") [assignment]
897+
self = cast("Categorical", self) # type: ignore[assignment]
898+
# pandas\core\base.py:894: error: Item "ExtensionArray" of
899+
# "Union[ExtensionArray, Any]" has no attribute "map"
900+
# [union-attr]
901+
return self._values.map(mapper) # type: ignore[union-attr]
856902

857903
values = self._values
858904

@@ -869,7 +915,9 @@ def _map_values(self, mapper, na_action=None):
869915
raise NotImplementedError
870916
map_f = lambda values, f: values.map(f)
871917
else:
872-
values = self.astype(object)._values
918+
# pandas\core\base.py:1142: error: "IndexOpsMixin" has no attribute
919+
# "astype" [attr-defined]
920+
values = self.astype(object)._values # type: ignore[attr-defined]
873921
if na_action == "ignore":
874922

875923
def map_f(values, f):
@@ -1111,7 +1159,9 @@ def memory_usage(self, deep=False):
11111159
are not components of the array if deep=False or if used on PyPy
11121160
"""
11131161
if hasattr(self.array, "memory_usage"):
1114-
return self.array.memory_usage(deep=deep)
1162+
# pandas\core\base.py:1379: error: "ExtensionArray" has no
1163+
# attribute "memory_usage" [attr-defined]
1164+
return self.array.memory_usage(deep=deep) # type: ignore[attr-defined]
11151165

11161166
v = self.array.nbytes
11171167
if deep and is_object_dtype(self) and not PYPY:
@@ -1245,7 +1295,9 @@ def searchsorted(self, value, side="left", sorter=None) -> np.ndarray:
12451295

12461296
def drop_duplicates(self, keep="first"):
12471297
duplicated = self.duplicated(keep=keep)
1248-
result = self[np.logical_not(duplicated)]
1298+
# pandas\core\base.py:1507: error: Value of type "IndexOpsMixin" is not
1299+
# indexable [index]
1300+
result = self[np.logical_not(duplicated)] # type: ignore[index]
12491301
return result
12501302

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

pandas/core/computation/expr.py

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

661661
if res is None:
662-
raise ValueError(f"Invalid function call {node.func.id}")
662+
# pandas\core\computation\expr.py:663: error: "expr" has no
663+
# attribute "id" [attr-defined]
664+
raise ValueError(
665+
f"Invalid function call {node.func.id}" # type: ignore[attr-defined]
666+
)
663667
if hasattr(res, "value"):
664668
res = res.value
665669

@@ -680,7 +684,12 @@ def visit_Call(self, node, side=None, **kwargs):
680684

681685
for key in node.keywords:
682686
if not isinstance(key, ast.keyword):
683-
raise ValueError(f"keyword error in function call '{node.func.id}'")
687+
# pandas\core\computation\expr.py:684: error: "expr" has no
688+
# attribute "id" [attr-defined]
689+
raise ValueError(
690+
"keyword error in function call " # type: ignore[attr-defined]
691+
f"'{node.func.id}'"
692+
)
684693

685694
if key.arg:
686695
kwargs[key.arg] = self.visit(key.value).value

0 commit comments

Comments
 (0)