Skip to content

Commit

Permalink
TYP: Upgrade mypy to 0.981 (pandas-dev#48871)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
2 people authored and noatamir committed Nov 9, 2022
1 parent 1f8b6fa commit fc53d39
Show file tree
Hide file tree
Showing 19 changed files with 41 additions and 71 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ If installed, we now require:
+-----------------+-----------------+----------+---------+
| Package | Minimum Version | Required | Changed |
+=================+=================+==========+=========+
| | | X | X |
| mypy (dev) | 0.981 | | X |
+-----------------+-----------------+----------+---------+

For `optional libraries <https://pandas.pydata.org/docs/getting_started/install.html>`_ the general recommendation is to use the latest version.
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ dependencies:
- flake8=5.0.4
- flake8-bugbear=22.7.1 # used by flake8, find likely bugs
- isort>=5.2.1 # check that imports are in the right order
- mypy=0.971
- mypy=0.981
- pre-commit>=2.15.0
- pycodestyle # used by flake8
- pyupgrade
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,12 @@ def agg_dict_like(self) -> DataFrame | Series:
keys_to_use = ktu

axis = 0 if isinstance(obj, ABCSeries) else 1
# error: Key expression in dictionary comprehension has incompatible type
# "Hashable"; expected type "NDFrame" [misc]
result = concat(
{k: results[k] for k in keys_to_use}, axis=axis, keys=keys_to_use
{k: results[k] for k in keys_to_use}, # type: ignore[misc]
axis=axis,
keys=keys_to_use,
)
elif any(is_ndframe):
# There is a mix of NDFrames and scalars
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1400,9 +1400,7 @@ def set_closed(self: IntervalArrayT, closed: IntervalClosedType) -> IntervalArra
either monotonic increasing or monotonic decreasing.
"""

# https://github.com/python/mypy/issues/1362
# Mypy does not support decorated properties
@property # type: ignore[misc]
@property
@Appender(
_interval_shared_docs["is_non_overlapping_monotonic"] % _shared_docs_kwargs
)
Expand Down
10 changes: 1 addition & 9 deletions pandas/core/computation/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,5 @@ def full_scope(self) -> DeepChainMap:
vars : DeepChainMap
All variables in this scope.
"""
# error: Unsupported operand types for + ("List[Dict[Any, Any]]" and
# "List[Mapping[Any, Any]]")
# error: Unsupported operand types for + ("List[Dict[Any, Any]]" and
# "List[Mapping[str, Any]]")
maps = (
[self.temps]
+ self.resolvers.maps # type: ignore[operator]
+ self.scope.maps # type: ignore[operator]
)
maps = [self.temps] + self.resolvers.maps + self.scope.maps
return DeepChainMap(*maps)
6 changes: 2 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4527,6 +4527,7 @@ def drop(

if inplace:
self._update_inplace(obj)
return None
else:
return obj

Expand Down Expand Up @@ -6960,10 +6961,7 @@ def fillna(

result = self.T.fillna(value=value, limit=limit).T

# error: Incompatible types in assignment (expression has type
# "NDFrameT", variable has type "Union[ArrayManager,
# SingleArrayManager, BlockManager, SingleBlockManager]")
new_data = result # type: ignore[assignment]
new_data = result
else:

new_data = self._mgr.fillna(
Expand Down
30 changes: 8 additions & 22 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,17 +687,8 @@ def value_counts(

# multi-index components
codes = self.grouper.reconstructed_codes
# error: Incompatible types in assignment (expression has type
# "List[ndarray[Any, dtype[_SCT]]]",
# variable has type "List[ndarray[Any, dtype[signedinteger[Any]]]]")
codes = [ # type: ignore[assignment]
rep(level_codes) for level_codes in codes
] + [llab(lab, inc)]
# error: List item 0 has incompatible type "Union[ndarray[Any, Any], Index]";
# expected "Index"
levels = [ping.group_index for ping in self.grouper.groupings] + [
lev # type: ignore[list-item]
]
codes = [rep(level_codes) for level_codes in codes] + [llab(lab, inc)]
levels = [ping.group_index for ping in self.grouper.groupings] + [lev]

if dropna:
mask = codes[-1] != -1
Expand Down Expand Up @@ -905,8 +896,7 @@ def tshift(self, periods: int = 1, freq=None) -> Series:
result = self._op_via_apply("tshift", periods=periods, freq=freq)
return result

# Decorated property not supported - https://github.com/python/mypy/issues/1362
@property # type: ignore[misc]
@property
@doc(Series.plot.__doc__)
def plot(self):
result = GroupByPlot(self)
Expand Down Expand Up @@ -965,15 +955,13 @@ def cov(
)
return result

# Decorated property not supported - https://github.com/python/mypy/issues/1362
@property # type: ignore[misc]
@property
@doc(Series.is_monotonic_increasing.__doc__)
def is_monotonic_increasing(self) -> Series:
result = self._op_via_apply("is_monotonic_increasing")
return result

# Decorated property not supported - https://github.com/python/mypy/issues/1362
@property # type: ignore[misc]
@property
@doc(Series.is_monotonic_decreasing.__doc__)
def is_monotonic_decreasing(self) -> Series:
result = self._op_via_apply("is_monotonic_decreasing")
Expand Down Expand Up @@ -1012,8 +1000,7 @@ def hist(
)
return result

# Decorated property not supported - https://github.com/python/mypy/issues/1362
@property # type: ignore[misc]
@property
@doc(Series.dtype.__doc__)
def dtype(self) -> Series:
result = self._op_via_apply("dtype")
Expand Down Expand Up @@ -2336,7 +2323,7 @@ def tshift(self, periods: int = 1, freq=None, axis: Axis = 0) -> DataFrame:
result = self._op_via_apply("tshift", periods=periods, freq=freq, axis=axis)
return result

@property # type: ignore[misc]
@property
@doc(DataFrame.plot.__doc__)
def plot(self) -> GroupByPlot:
result = GroupByPlot(self)
Expand Down Expand Up @@ -2407,8 +2394,7 @@ def hist(
)
return result

# Decorated property not supported - https://github.com/python/mypy/issues/1362
@property # type: ignore[misc]
@property
@doc(DataFrame.dtypes.__doc__)
def dtypes(self) -> Series:
result = self._op_via_apply("dtypes")
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5171,9 +5171,7 @@ def values(self) -> ArrayLike:
"""
return self._data

# error: Decorated property not supported
# https://github.com/python/mypy/issues/1362
@cache_readonly # type: ignore[misc]
@cache_readonly
@doc(IndexOpsMixin.array)
def array(self) -> ExtensionArray:
array = self._data
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/indexes/frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def difference(self, other) -> FrozenList:
return type(self)(temp)

# TODO: Consider deprecating these in favor of `union` (xref gh-15506)
__add__ = __iadd__ = union
# error: Incompatible types in assignment (expression has type
# "Callable[[FrozenList, Any], FrozenList]", base class "list" defined the
# type as overloaded function)
__add__ = __iadd__ = union # type: ignore[assignment]

def __getitem__(self, n):
if isinstance(n, slice):
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
# ----------------------------------------------------------------
# Indexing Methods

# error: Decorated property not supported
@cache_readonly # type: ignore[misc]
@cache_readonly
@doc(Index._should_fallback_to_positional)
def _should_fallback_to_positional(self) -> bool:
return False
Expand Down
12 changes: 3 additions & 9 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,17 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeIndex:
arr = self._data.to_timestamp(freq, how)
return DatetimeIndex._simple_new(arr, name=self.name)

# https://github.com/python/mypy/issues/1362
# error: Decorated property not supported
@property # type: ignore[misc]
@property
@doc(PeriodArray.hour.fget)
def hour(self) -> Int64Index:
return Int64Index(self._data.hour, name=self.name)

# https://github.com/python/mypy/issues/1362
# error: Decorated property not supported
@property # type: ignore[misc]
@property
@doc(PeriodArray.minute.fget)
def minute(self) -> Int64Index:
return Int64Index(self._data.minute, name=self.name)

# https://github.com/python/mypy/issues/1362
# error: Decorated property not supported
@property # type: ignore[misc]
@property
@doc(PeriodArray.second.fget)
def second(self) -> Int64Index:
return Int64Index(self._data.second, name=self.name)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,8 @@ def _convert_to_multiindex(index: Index) -> MultiIndex:
else:
restore_codes = algos.take_nd(codes, indexer, fill_value=-1)

join_levels = join_levels + [restore_levels]
# error: Cannot determine type of "__add__"
join_levels = join_levels + [restore_levels] # type: ignore[has-type]
join_codes = join_codes + [restore_codes]
join_names = join_names + [dropped_level_name]

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ def get_new_columns(self, value_columns: Index | None):
new_levels: FrozenList | list[Index]

if isinstance(value_columns, MultiIndex):
new_levels = value_columns.levels + (self.removed_level_full,)
# error: Cannot determine type of "__add__" [has-type]
new_levels = value_columns.levels + ( # type: ignore[has-type]
self.removed_level_full,
)
new_names = value_columns.names + (self.removed_name,)

new_codes = [lab.take(propagator) for lab in value_columns.codes]
Expand Down
4 changes: 1 addition & 3 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,7 @@ def infer_compression(
if compression in _supported_compressions:
return compression

# https://github.com/python/mypy/issues/5492
# Unsupported operand types for + ("List[Optional[str]]" and "List[str]")
valid = ["infer", None] + sorted(_supported_compressions) # type: ignore[operator]
valid = ["infer", None] + sorted(_supported_compressions)
msg = (
f"Unrecognized compression type: {compression}\n"
f"Valid compression types are {valid}"
Expand Down
4 changes: 1 addition & 3 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3481,9 +3481,7 @@ def queryables(self) -> dict[str, Any]:
(v.cname, v) for v in self.values_axes if v.name in set(self.data_columns)
]

# error: Unsupported operand types for + ("List[Tuple[str, IndexCol]]" and
# "List[Tuple[str, None]]")
return dict(d1 + d2 + d3) # type: ignore[operator]
return dict(d1 + d2 + d3)

def index_cols(self):
"""return a list of my index cols"""
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,7 @@ def test_setitem_extension_types(self, obj, dtype):
# property would require instantiation
if not isinstance(dtype.name, property)
]
# mypy doesn't allow adding lists of different types
# https://github.com/python/mypy/issues/5492
+ ["datetime64[ns, UTC]", "period[D]"], # type: ignore[list-item]
+ ["datetime64[ns, UTC]", "period[D]"],
)
def test_setitem_with_ea_name(self, ea_name):
# GH 38386
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ def test_resample_consistency():
dates2: List[DatetimeNaTType] = (
dates1[:2] + [pd.NaT] + dates1[2:4] + [pd.NaT] + dates1[4:]
)
dates3 = [pd.NaT] + dates1 + [pd.NaT] # type: ignore[operator]
dates3 = [pd.NaT] + dates1 + [pd.NaT]


@pytest.mark.parametrize("dates", [dates1, dates2, dates3])
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,11 +1086,11 @@ def test_convert_object_to_datetime_with_cache(
("input", "expected"),
(
(
Series([NaT] * 20 + [None] * 20, dtype="object"), # type: ignore[list-item] # noqa: E501
Series([NaT] * 20 + [None] * 20, dtype="object"),
Series([NaT] * 40, dtype="datetime64[ns]"),
),
(
Series([NaT] * 60 + [None] * 60, dtype="object"), # type: ignore[list-item] # noqa: E501
Series([NaT] * 60 + [None] * 60, dtype="object"),
Series([NaT] * 120, dtype="datetime64[ns]"),
),
(Series([None] * 20), Series([NaT] * 20, dtype="datetime64[ns]")),
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ cpplint
flake8==5.0.4
flake8-bugbear==22.7.1
isort>=5.2.1
mypy==0.971
mypy==0.981
pre-commit>=2.15.0
pycodestyle
pyupgrade
Expand Down

0 comments on commit fc53d39

Please sign in to comment.