Skip to content

TYP: Add MyPy Error Codes #35311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
invgrep -R --include="*.py" -P '# type: (?!ignore)' pandas
RET=$(($RET + $?)) ; echo $MSG "DONE"

# https://github.com/python/mypy/issues/7384
# MSG='Check for missing error codes with # type: ignore' ; echo $MSG
# invgrep -R --include="*.py" -P '# type: ignore(?!\[)' pandas
# RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Check for use of foo.__class__ instead of type(foo)' ; echo $MSG
invgrep -R --include=*.{py,pyx} '\.__class__' pandas
RET=$(($RET + $?)) ; echo $MSG "DONE"
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@ dependencies:
- pip:
- git+https://github.com/pandas-dev/pydata-sphinx-theme.git@master
- git+https://github.com/numpy/numpydoc
- pyflakes>=2.2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is available from conda?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give it a go on the CI, IIRC I had issues locally with conda but later found an issue in the environment. conda list showed 2.2.0 but flake8 was still using 2.1.1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conda doesn't like it.

2 changes: 1 addition & 1 deletion pandas/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def register_option(
for k in path:
# NOTE: tokenize.Name is not a public constant
# error: Module has no attribute "Name" [attr-defined]
if not re.match("^" + tokenize.Name + "$", k): # type: ignore
if not re.match("^" + tokenize.Name + "$", k): # type: ignore[attr-defined]
raise ValueError(f"{k} is not a valid identifier")
if keyword.iskeyword(k):
raise ValueError(f"{k} is a python keyword")
Expand Down
6 changes: 3 additions & 3 deletions pandas/compat/pickle_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class _LoadSparseSeries:
# https://github.com/python/mypy/issues/1020
# error: Incompatible return type for "__new__" (returns "Series", but must return
# a subtype of "_LoadSparseSeries")
def __new__(cls) -> "Series": # type: ignore
def __new__(cls) -> "Series": # type: ignore[misc]
from pandas import Series

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

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

# error: Name 'pkl._Unpickler' is not defined
class Unpickler(pkl._Unpickler): # type: ignore
class Unpickler(pkl._Unpickler): # type: ignore[name-defined]
def find_class(self, module, name):
# override superclass
key = (module, name)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> np.ndarray:
if is_categorical_dtype(comps):
# TODO(extension)
# handle categoricals
return comps.isin(values) # type: ignore
# error: "ExtensionArray" has no attribute "isin" [attr-defined]
return comps.isin(values) # type: ignore[attr-defined]

comps, dtype = _ensure_data(comps)
values, _ = _ensure_data(values, dtype=dtype)
Expand Down
9 changes: 7 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,9 @@ def _ndarray(self) -> np.ndarray:

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

# ------------------------------------------------------------------
Expand Down Expand Up @@ -809,7 +812,8 @@ def _validate_scalar(
value = NaT

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

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

@classmethod
def _validate_frequency(cls, index, freq, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ def mid(self):

# https://github.com/python/mypy/issues/1362
# Mypy does not support decorated properties
@property # type: ignore
@property # type: ignore[misc]
@Appender(
_interval_shared_docs["is_non_overlapping_monotonic"] % _shared_docs_kwargs
)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ def _check_compatible_with(self, other, setitem: bool = False):
def dtype(self) -> PeriodDtype:
return self._dtype

# error: Read-only property cannot override read-write property [misc]
@property # type: ignore
# error: Read-only property cannot override read-write property
@property # type: ignore[misc]
def freq(self) -> BaseOffset:
"""
Return the frequency object for this PeriodArray.
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ def evaluate(op, a, b, use_numexpr: bool = True):
if op_str is not None:
use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b)
if use_numexpr:
return _evaluate(op, op_str, a, b) # type: ignore
# error: "None" not callable
return _evaluate(op, op_str, a, b) # type: ignore[misc]
return _evaluate_standard(op, op_str, a, b)


Expand Down
4 changes: 3 additions & 1 deletion pandas/core/computation/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def create_valid_python_identifier(name: str) -> str:
special_characters_replacements = {
char: f"_{token.tok_name[tokval]}_"
# The ignore here is because of a bug in mypy that is resolved in 0.740
for char, tokval in tokenize.EXACT_TOKEN_TYPES.items() # type: ignore
for char, tokval in (
tokenize.EXACT_TOKEN_TYPES.items() # type: ignore[attr-defined]
)
}
special_characters_replacements.update(
{
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _resolve_name(self):
return self.name

# read-only property overwriting read/write property
@property # type: ignore
@property # type: ignore[misc]
def value(self):
return self._value

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def is_terminal() -> bool:
"""
try:
# error: Name 'get_ipython' is not defined
ip = get_ipython() # type: ignore
ip = get_ipython() # type: ignore[name-defined]
except NameError: # assume standard Python interpreter in a terminal
return True
else:
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,13 @@ def ensure_int_or_float(arr: ArrayLike, copy: bool = False) -> np.array:
"""
# TODO: GH27506 potential bug with ExtensionArrays
try:
return arr.astype("int64", copy=copy, casting="safe") # type: ignore
# error: Unexpected keyword argument "casting" for "astype"
return arr.astype("int64", copy=copy, casting="safe") # type: ignore[call-arg]
except TypeError:
pass
try:
return arr.astype("uint64", copy=copy, casting="safe") # type: ignore
# error: Unexpected keyword argument "casting" for "astype"
return arr.astype("uint64", copy=copy, casting="safe") # type: ignore[call-arg]
except TypeError:
if is_extension_array_dtype(arr.dtype):
return arr.to_numpy(dtype="float64", na_value=np.nan)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,8 @@ class DatetimeTZDtype(PandasExtensionDtype):

def __init__(self, unit: Union[str_type, "DatetimeTZDtype"] = "ns", tz=None):
if isinstance(unit, DatetimeTZDtype):
unit, tz = unit.unit, unit.tz # type: ignore
# error: "str" has no attribute "tz"
unit, tz = unit.unit, unit.tz # type: ignore[attr-defined]

if unit != "ns":
if isinstance(unit, str) and tz is None:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def create_pandas_abc_type(name, attr, comp):

# https://github.com/python/mypy/issues/1006
# error: 'classmethod' used with a non-method
@classmethod # type: ignore
@classmethod # type: ignore[misc]
def _check(cls, inst) -> bool:
return getattr(inst, attr, "_typ") in comp

Expand Down
21 changes: 16 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2164,10 +2164,14 @@ def to_stata(
from pandas.io.stata import StataWriter as statawriter
elif version == 117:
# mypy: Name 'statawriter' already defined (possibly by an import)
from pandas.io.stata import StataWriter117 as statawriter # type: ignore
from pandas.io.stata import ( # type: ignore[no-redef]
StataWriter117 as statawriter,
)
else: # versions 118 and 119
# mypy: Name 'statawriter' already defined (possibly by an import)
from pandas.io.stata import StataWriterUTF8 as statawriter # type: ignore
from pandas.io.stata import ( # type: ignore[no-redef]
StataWriterUTF8 as statawriter,
)

kwargs: Dict[str, Any] = {}
if version is None or version >= 117:
Expand All @@ -2178,7 +2182,7 @@ def to_stata(
kwargs["version"] = version

# mypy: Too many arguments for "StataWriter"
writer = statawriter( # type: ignore
writer = statawriter( # type: ignore[call-arg]
path,
self,
convert_dates=convert_dates,
Expand Down Expand Up @@ -3578,7 +3582,13 @@ def extract_unique_dtypes_from_dtypes_set(
extracted_dtypes = [
unique_dtype
for unique_dtype in unique_dtypes
if issubclass(unique_dtype.type, tuple(dtypes_set)) # type: ignore
# error: Argument 1 to "tuple" has incompatible type
# "FrozenSet[Union[ExtensionDtype, str, Any, Type[str],
# Type[float], Type[int], Type[complex], Type[bool]]]";
# expected "Iterable[Union[type, Tuple[Any, ...]]]"
if issubclass(
unique_dtype.type, tuple(dtypes_set) # type: ignore[arg-type]
)
]
return extracted_dtypes

Expand Down Expand Up @@ -5250,7 +5260,8 @@ def f(vals):
# TODO: Just move the sort_values doc here.
@Substitution(**_shared_doc_kwargs)
@Appender(NDFrame.sort_values.__doc__)
def sort_values( # type: ignore[override] # NOQA # issue 27237
# error: Signature of "sort_values" incompatible with supertype "NDFrame"
def sort_values( # type: ignore[override]
self,
by,
axis=0,
Expand Down
18 changes: 13 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,9 @@ 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).__finalize__( # type: ignore
self, method="swapaxes"
)
return self._constructor(
new_values, *new_axes # type: ignore[arg-type]
).__finalize__(self, method="swapaxes")

def droplevel(self: FrameOrSeries, level, axis=0) -> FrameOrSeries:
"""
Expand Down Expand Up @@ -4011,7 +4011,11 @@ def add_prefix(self: FrameOrSeries, prefix: str) -> FrameOrSeries:
f = functools.partial("{prefix}{}".format, prefix=prefix)

mapper = {self._info_axis_name: f}
return self.rename(**mapper) # type: ignore
# error: Incompatible return value type (got "Optional[FrameOrSeries]",
# expected "FrameOrSeries")
# error: Argument 1 to "rename" of "NDFrame" has incompatible type
# "**Dict[str, partial[str]]"; expected "Union[str, int, None]"
return self.rename(**mapper) # type: ignore[return-value, arg-type]

def add_suffix(self: FrameOrSeries, suffix: str) -> FrameOrSeries:
"""
Expand Down Expand Up @@ -4070,7 +4074,11 @@ def add_suffix(self: FrameOrSeries, suffix: str) -> FrameOrSeries:
f = functools.partial("{}{suffix}".format, suffix=suffix)

mapper = {self._info_axis_name: f}
return self.rename(**mapper) # type: ignore
# error: Incompatible return value type (got "Optional[FrameOrSeries]",
# expected "FrameOrSeries")
# error: Argument 1 to "rename" of "NDFrame" has incompatible type
# "**Dict[str, partial[str]]"; expected "Union[str, int, None]"
return self.rename(**mapper) # type: ignore[return-value, arg-type]

def sort_values(
self,
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ def _format_data(self, name=None) -> str_t:
if self.inferred_type == "string":
is_justify = False
elif self.inferred_type == "categorical":
if is_object_dtype(self.categories): # type: ignore
# error: "Index" has no attribute "categories"
if is_object_dtype(self.categories): # type: ignore[attr-defined]
is_justify = False

return format_object_summary(
Expand Down Expand Up @@ -940,7 +941,8 @@ def _format_with_header(self, header, na_rep="NaN") -> List[str_t]:
if mask.any():
result = np.array(result)
result[mask] = na_rep
result = result.tolist() # type: ignore
# error: "List[str]" has no attribute "tolist"
result = result.tolist() # type: ignore[attr-defined]
else:
result = trim_front(format_array(values, None, justify="left"))
return header + result
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def _join_i8_wrapper(joinf, with_indexers: bool = True):
Create the join wrapper methods.
"""

@staticmethod # type: ignore
# error: 'staticmethod' used with a non-method
@staticmethod # type: ignore[misc]
def wrapper(left, right):
if isinstance(left, (np.ndarray, ABCIndex, ABCSeries, DatetimeLikeArrayMixin)):
left = left.view("i8")
Expand Down Expand Up @@ -95,7 +96,10 @@ class DatetimeIndexOpsMixin(ExtensionIndex):
_bool_ops: List[str] = []
_field_ops: List[str] = []

hasnans = cache_readonly(DatetimeLikeArrayMixin._hasnans.fget) # type: ignore
# error: "Callable[[Any], Any]" has no attribute "fget"
hasnans = cache_readonly(
DatetimeLikeArrayMixin._hasnans.fget # type: ignore[attr-defined]
)
_hasnans = hasnans # for index / array -agnostic code

@property
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2744,7 +2744,8 @@ def _block_shape(values: ArrayLike, ndim: int = 1) -> ArrayLike:
# TODO(EA2D): https://github.com/pandas-dev/pandas/issues/23023
# block.shape is incorrect for "2D" ExtensionArrays
# We can't, and don't need to, reshape.
values = values.reshape(tuple((1,) + shape)) # type: ignore
# error: "ExtensionArray" has no attribute "reshape"
values = values.reshape(tuple((1,) + shape)) # type: ignore[attr-defined]
return values


Expand Down
3 changes: 2 additions & 1 deletion pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ def fill_bool(x, left=None):
filler = fill_int if is_self_int_dtype and is_other_int_dtype else fill_bool

res_values = na_logical_op(lvalues, rvalues, op)
res_values = filler(res_values) # type: ignore
# error: Cannot call function of unknown type
res_values = filler(res_values) # type: ignore[operator]

return res_values

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ def __init__(self, obj, *args, **kwargs):
setattr(self, attr, kwargs.get(attr, getattr(parent, attr)))

# error: Too many arguments for "__init__" of "object"
super().__init__(None) # type: ignore
super().__init__(None) # type: ignore[call-arg]
self._groupby = groupby
self._groupby.mutated = True
self._groupby.grouper.mutated = True
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,8 @@ def _values(self):
"""
return self._mgr.internal_values()

@Appender(base.IndexOpsMixin.array.__doc__) # type: ignore
# error: Decorated property not supported
@Appender(base.IndexOpsMixin.array.__doc__) # type: ignore[misc]
@property
def array(self) -> ExtensionArray:
return self._mgr._block.array_values()
Expand Down Expand Up @@ -4921,7 +4922,10 @@ def to_timestamp(self, freq=None, how="start", copy=True) -> "Series":

if not isinstance(self.index, PeriodIndex):
raise TypeError(f"unsupported Type {type(self.index).__name__}")
new_index = self.index.to_timestamp(freq=freq, how=how) # type: ignore
# error: "PeriodIndex" has no attribute "to_timestamp"
new_index = self.index.to_timestamp( # type: ignore[attr-defined]
freq=freq, how=how
)
return self._constructor(new_values, index=new_index).__finalize__(
self, method="to_timestamp"
)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def _convert_listlike_datetimes(
if tz == "utc":
# error: Item "DatetimeIndex" of "Union[DatetimeArray, DatetimeIndex]" has
# no attribute "tz_convert"
arg = arg.tz_convert(None).tz_localize(tz) # type: ignore
arg = arg.tz_convert(None).tz_localize(tz) # type: ignore[union-attr]
return arg

elif is_datetime64_ns_dtype(arg_dtype):
Expand Down
Loading