Skip to content

Commit 385a2ca

Browse files
TYP: Add MyPy Error Codes (#35311)
1 parent 69cd744 commit 385a2ca

37 files changed

+170
-77
lines changed

ci/code_checks.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
230230
invgrep -R --include="*.py" -P '# type: (?!ignore)' pandas
231231
RET=$(($RET + $?)) ; echo $MSG "DONE"
232232

233+
# https://github.com/python/mypy/issues/7384
234+
# MSG='Check for missing error codes with # type: ignore' ; echo $MSG
235+
# invgrep -R --include="*.py" -P '# type: ignore(?!\[)' pandas
236+
# RET=$(($RET + $?)) ; echo $MSG "DONE"
237+
233238
MSG='Check for use of foo.__class__ instead of type(foo)' ; echo $MSG
234239
invgrep -R --include=*.{py,pyx} '\.__class__' pandas
235240
RET=$(($RET + $?)) ; echo $MSG "DONE"

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,4 @@ dependencies:
109109
- pip:
110110
- git+https://github.com/pandas-dev/pydata-sphinx-theme.git@master
111111
- git+https://github.com/numpy/numpydoc
112+
- pyflakes>=2.2.0

pandas/_config/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def register_option(
462462
for k in path:
463463
# NOTE: tokenize.Name is not a public constant
464464
# error: Module has no attribute "Name" [attr-defined]
465-
if not re.match("^" + tokenize.Name + "$", k): # type: ignore
465+
if not re.match("^" + tokenize.Name + "$", k): # type: ignore[attr-defined]
466466
raise ValueError(f"{k} is not a valid identifier")
467467
if keyword.iskeyword(k):
468468
raise ValueError(f"{k} is a python keyword")

pandas/compat/pickle_compat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class _LoadSparseSeries:
6464
# https://github.com/python/mypy/issues/1020
6565
# error: Incompatible return type for "__new__" (returns "Series", but must return
6666
# a subtype of "_LoadSparseSeries")
67-
def __new__(cls) -> "Series": # type: ignore
67+
def __new__(cls) -> "Series": # type: ignore[misc]
6868
from pandas import Series
6969

7070
warnings.warn(
@@ -82,7 +82,7 @@ class _LoadSparseFrame:
8282
# https://github.com/python/mypy/issues/1020
8383
# error: Incompatible return type for "__new__" (returns "DataFrame", but must
8484
# return a subtype of "_LoadSparseFrame")
85-
def __new__(cls) -> "DataFrame": # type: ignore
85+
def __new__(cls) -> "DataFrame": # type: ignore[misc]
8686
from pandas import DataFrame
8787

8888
warnings.warn(
@@ -181,7 +181,7 @@ def __new__(cls) -> "DataFrame": # type: ignore
181181
# functions for compat and uses a non-public class of the pickle module.
182182

183183
# error: Name 'pkl._Unpickler' is not defined
184-
class Unpickler(pkl._Unpickler): # type: ignore
184+
class Unpickler(pkl._Unpickler): # type: ignore[name-defined]
185185
def find_class(self, module, name):
186186
# override superclass
187187
key = (module, name)

pandas/core/algorithms.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> np.ndarray:
427427
if is_categorical_dtype(comps):
428428
# TODO(extension)
429429
# handle categoricals
430-
return comps.isin(values) # type: ignore
430+
# error: "ExtensionArray" has no attribute "isin" [attr-defined]
431+
return comps.isin(values) # type: ignore[attr-defined]
431432

432433
comps, dtype = _ensure_data(comps)
433434
values, _ = _ensure_data(values, dtype=dtype)

pandas/core/arrays/datetimelike.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,9 @@ def _ndarray(self) -> np.ndarray:
468468

469469
def _from_backing_data(self: _T, arr: np.ndarray) -> _T:
470470
# Note: we do not retain `freq`
471+
# error: Unexpected keyword argument "dtype" for "NDArrayBackedExtensionArray"
472+
# TODO: add my error code
473+
# https://github.com/python/mypy/issues/7384
471474
return type(self)(arr, dtype=self.dtype) # type: ignore
472475

473476
# ------------------------------------------------------------------
@@ -809,7 +812,8 @@ def _validate_scalar(
809812
value = NaT
810813

811814
elif isinstance(value, self._recognized_scalars):
812-
value = self._scalar_type(value) # type: ignore
815+
# error: Too many arguments for "object" [call-arg]
816+
value = self._scalar_type(value) # type: ignore[call-arg]
813817

814818
else:
815819
if msg is None:
@@ -1129,7 +1133,8 @@ def resolution(self) -> str:
11291133
"""
11301134
Returns day, hour, minute, second, millisecond or microsecond
11311135
"""
1132-
return self._resolution_obj.attrname # type: ignore
1136+
# error: Item "None" of "Optional[Any]" has no attribute "attrname"
1137+
return self._resolution_obj.attrname # type: ignore[union-attr]
11331138

11341139
@classmethod
11351140
def _validate_frequency(cls, index, freq, **kwargs):

pandas/core/arrays/interval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ def mid(self):
10571057

10581058
# https://github.com/python/mypy/issues/1362
10591059
# Mypy does not support decorated properties
1060-
@property # type: ignore
1060+
@property # type: ignore[misc]
10611061
@Appender(
10621062
_interval_shared_docs["is_non_overlapping_monotonic"] % _shared_docs_kwargs
10631063
)

pandas/core/arrays/period.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ def _check_compatible_with(self, other, setitem: bool = False):
278278
def dtype(self) -> PeriodDtype:
279279
return self._dtype
280280

281-
# error: Read-only property cannot override read-write property [misc]
282-
@property # type: ignore
281+
# error: Read-only property cannot override read-write property
282+
@property # type: ignore[misc]
283283
def freq(self) -> BaseOffset:
284284
"""
285285
Return the frequency object for this PeriodArray.

pandas/core/computation/expressions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ def evaluate(op, a, b, use_numexpr: bool = True):
227227
if op_str is not None:
228228
use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b)
229229
if use_numexpr:
230-
return _evaluate(op, op_str, a, b) # type: ignore
230+
# error: "None" not callable
231+
return _evaluate(op, op_str, a, b) # type: ignore[misc]
231232
return _evaluate_standard(op, op_str, a, b)
232233

233234

pandas/core/computation/parsing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ def create_valid_python_identifier(name: str) -> str:
3737
special_characters_replacements = {
3838
char: f"_{token.tok_name[tokval]}_"
3939
# The ignore here is because of a bug in mypy that is resolved in 0.740
40-
for char, tokval in tokenize.EXACT_TOKEN_TYPES.items() # type: ignore
40+
for char, tokval in (
41+
tokenize.EXACT_TOKEN_TYPES.items() # type: ignore[attr-defined]
42+
)
4143
}
4244
special_characters_replacements.update(
4345
{

0 commit comments

Comments
 (0)