Skip to content

Commit 050b3b8

Browse files
phoflmroeschke
andauthored
TYP: Upgrade mypy to 0.981 (pandas-dev#48871)
Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 5e503b4 commit 050b3b8

File tree

19 files changed

+41
-71
lines changed

19 files changed

+41
-71
lines changed

doc/source/whatsnew/v1.6.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ If installed, we now require:
9696
+-----------------+-----------------+----------+---------+
9797
| Package | Minimum Version | Required | Changed |
9898
+=================+=================+==========+=========+
99-
| | | X | X |
99+
| mypy (dev) | 0.981 | | X |
100100
+-----------------+-----------------+----------+---------+
101101

102102
For `optional libraries <https://pandas.pydata.org/docs/getting_started/install.html>`_ the general recommendation is to use the latest version.

environment.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ dependencies:
8989
- flake8=5.0.4
9090
- flake8-bugbear=22.7.1 # used by flake8, find likely bugs
9191
- isort>=5.2.1 # check that imports are in the right order
92-
- mypy=0.971
92+
- mypy=0.981
9393
- pre-commit>=2.15.0
9494
- pycodestyle # used by flake8
9595
- pyupgrade

pandas/core/apply.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,12 @@ def agg_dict_like(self) -> DataFrame | Series:
507507
keys_to_use = ktu
508508

509509
axis = 0 if isinstance(obj, ABCSeries) else 1
510+
# error: Key expression in dictionary comprehension has incompatible type
511+
# "Hashable"; expected type "NDFrame" [misc]
510512
result = concat(
511-
{k: results[k] for k in keys_to_use}, axis=axis, keys=keys_to_use
513+
{k: results[k] for k in keys_to_use}, # type: ignore[misc]
514+
axis=axis,
515+
keys=keys_to_use,
512516
)
513517
elif any(is_ndframe):
514518
# There is a mix of NDFrames and scalars

pandas/core/arrays/interval.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1400,9 +1400,7 @@ def set_closed(self: IntervalArrayT, closed: IntervalClosedType) -> IntervalArra
14001400
either monotonic increasing or monotonic decreasing.
14011401
"""
14021402

1403-
# https://github.com/python/mypy/issues/1362
1404-
# Mypy does not support decorated properties
1405-
@property # type: ignore[misc]
1403+
@property
14061404
@Appender(
14071405
_interval_shared_docs["is_non_overlapping_monotonic"] % _shared_docs_kwargs
14081406
)

pandas/core/computation/scope.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,5 @@ def full_scope(self) -> DeepChainMap:
318318
vars : DeepChainMap
319319
All variables in this scope.
320320
"""
321-
# error: Unsupported operand types for + ("List[Dict[Any, Any]]" and
322-
# "List[Mapping[Any, Any]]")
323-
# error: Unsupported operand types for + ("List[Dict[Any, Any]]" and
324-
# "List[Mapping[str, Any]]")
325-
maps = (
326-
[self.temps]
327-
+ self.resolvers.maps # type: ignore[operator]
328-
+ self.scope.maps # type: ignore[operator]
329-
)
321+
maps = [self.temps] + self.resolvers.maps + self.scope.maps
330322
return DeepChainMap(*maps)

pandas/core/generic.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -4527,6 +4527,7 @@ def drop(
45274527

45284528
if inplace:
45294529
self._update_inplace(obj)
4530+
return None
45304531
else:
45314532
return obj
45324533

@@ -6960,10 +6961,7 @@ def fillna(
69606961

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

6963-
# error: Incompatible types in assignment (expression has type
6964-
# "NDFrameT", variable has type "Union[ArrayManager,
6965-
# SingleArrayManager, BlockManager, SingleBlockManager]")
6966-
new_data = result # type: ignore[assignment]
6964+
new_data = result
69676965
else:
69686966

69696967
new_data = self._mgr.fillna(

pandas/core/groupby/generic.py

+8-22
Original file line numberDiff line numberDiff line change
@@ -687,17 +687,8 @@ def value_counts(
687687

688688
# multi-index components
689689
codes = self.grouper.reconstructed_codes
690-
# error: Incompatible types in assignment (expression has type
691-
# "List[ndarray[Any, dtype[_SCT]]]",
692-
# variable has type "List[ndarray[Any, dtype[signedinteger[Any]]]]")
693-
codes = [ # type: ignore[assignment]
694-
rep(level_codes) for level_codes in codes
695-
] + [llab(lab, inc)]
696-
# error: List item 0 has incompatible type "Union[ndarray[Any, Any], Index]";
697-
# expected "Index"
698-
levels = [ping.group_index for ping in self.grouper.groupings] + [
699-
lev # type: ignore[list-item]
700-
]
690+
codes = [rep(level_codes) for level_codes in codes] + [llab(lab, inc)]
691+
levels = [ping.group_index for ping in self.grouper.groupings] + [lev]
701692

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

908-
# Decorated property not supported - https://github.com/python/mypy/issues/1362
909-
@property # type: ignore[misc]
899+
@property
910900
@doc(Series.plot.__doc__)
911901
def plot(self):
912902
result = GroupByPlot(self)
@@ -965,15 +955,13 @@ def cov(
965955
)
966956
return result
967957

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

975-
# Decorated property not supported - https://github.com/python/mypy/issues/1362
976-
@property # type: ignore[misc]
964+
@property
977965
@doc(Series.is_monotonic_decreasing.__doc__)
978966
def is_monotonic_decreasing(self) -> Series:
979967
result = self._op_via_apply("is_monotonic_decreasing")
@@ -1012,8 +1000,7 @@ def hist(
10121000
)
10131001
return result
10141002

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

2339-
@property # type: ignore[misc]
2326+
@property
23402327
@doc(DataFrame.plot.__doc__)
23412328
def plot(self) -> GroupByPlot:
23422329
result = GroupByPlot(self)
@@ -2407,8 +2394,7 @@ def hist(
24072394
)
24082395
return result
24092396

2410-
# Decorated property not supported - https://github.com/python/mypy/issues/1362
2411-
@property # type: ignore[misc]
2397+
@property
24122398
@doc(DataFrame.dtypes.__doc__)
24132399
def dtypes(self) -> Series:
24142400
result = self._op_via_apply("dtypes")

pandas/core/indexes/base.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -5171,9 +5171,7 @@ def values(self) -> ArrayLike:
51715171
"""
51725172
return self._data
51735173

5174-
# error: Decorated property not supported
5175-
# https://github.com/python/mypy/issues/1362
5176-
@cache_readonly # type: ignore[misc]
5174+
@cache_readonly
51775175
@doc(IndexOpsMixin.array)
51785176
def array(self) -> ExtensionArray:
51795177
array = self._data

pandas/core/indexes/frozen.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ def difference(self, other) -> FrozenList:
6565
return type(self)(temp)
6666

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

7073
def __getitem__(self, n):
7174
if isinstance(n, slice):

pandas/core/indexes/numeric.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
213213
# ----------------------------------------------------------------
214214
# Indexing Methods
215215

216-
# error: Decorated property not supported
217-
@cache_readonly # type: ignore[misc]
216+
@cache_readonly
218217
@doc(Index._should_fallback_to_positional)
219218
def _should_fallback_to_positional(self) -> bool:
220219
return False

pandas/core/indexes/period.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,17 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeIndex:
191191
arr = self._data.to_timestamp(freq, how)
192192
return DatetimeIndex._simple_new(arr, name=self.name)
193193

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

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

208-
# https://github.com/python/mypy/issues/1362
209-
# error: Decorated property not supported
210-
@property # type: ignore[misc]
204+
@property
211205
@doc(PeriodArray.second.fget)
212206
def second(self) -> Int64Index:
213207
return Int64Index(self._data.second, name=self.name)

pandas/core/reshape/merge.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,8 @@ def _convert_to_multiindex(index: Index) -> MultiIndex:
17251725
else:
17261726
restore_codes = algos.take_nd(codes, indexer, fill_value=-1)
17271727

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

pandas/core/reshape/reshape.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@ def get_new_columns(self, value_columns: Index | None):
316316
new_levels: FrozenList | list[Index]
317317

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

322325
new_codes = [lab.take(propagator) for lab in value_columns.codes]

pandas/io/common.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,7 @@ def infer_compression(
574574
if compression in _supported_compressions:
575575
return compression
576576

577-
# https://github.com/python/mypy/issues/5492
578-
# Unsupported operand types for + ("List[Optional[str]]" and "List[str]")
579-
valid = ["infer", None] + sorted(_supported_compressions) # type: ignore[operator]
577+
valid = ["infer", None] + sorted(_supported_compressions)
580578
msg = (
581579
f"Unrecognized compression type: {compression}\n"
582580
f"Valid compression types are {valid}"

pandas/io/pytables.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -3481,9 +3481,7 @@ def queryables(self) -> dict[str, Any]:
34813481
(v.cname, v) for v in self.values_axes if v.name in set(self.data_columns)
34823482
]
34833483

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

34883486
def index_cols(self):
34893487
"""return a list of my index cols"""

pandas/tests/frame/indexing/test_setitem.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@ def test_setitem_extension_types(self, obj, dtype):
260260
# property would require instantiation
261261
if not isinstance(dtype.name, property)
262262
]
263-
# mypy doesn't allow adding lists of different types
264-
# https://github.com/python/mypy/issues/5492
265-
+ ["datetime64[ns, UTC]", "period[D]"], # type: ignore[list-item]
263+
+ ["datetime64[ns, UTC]", "period[D]"],
266264
)
267265
def test_setitem_with_ea_name(self, ea_name):
268266
# GH 38386

pandas/tests/resample/test_datetime_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ def test_resample_consistency():
13001300
dates2: List[DatetimeNaTType] = (
13011301
dates1[:2] + [pd.NaT] + dates1[2:4] + [pd.NaT] + dates1[4:]
13021302
)
1303-
dates3 = [pd.NaT] + dates1 + [pd.NaT] # type: ignore[operator]
1303+
dates3 = [pd.NaT] + dates1 + [pd.NaT]
13041304

13051305

13061306
@pytest.mark.parametrize("dates", [dates1, dates2, dates3])

pandas/tests/tools/test_to_datetime.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1086,11 +1086,11 @@ def test_convert_object_to_datetime_with_cache(
10861086
("input", "expected"),
10871087
(
10881088
(
1089-
Series([NaT] * 20 + [None] * 20, dtype="object"), # type: ignore[list-item] # noqa: E501
1089+
Series([NaT] * 20 + [None] * 20, dtype="object"),
10901090
Series([NaT] * 40, dtype="datetime64[ns]"),
10911091
),
10921092
(
1093-
Series([NaT] * 60 + [None] * 60, dtype="object"), # type: ignore[list-item] # noqa: E501
1093+
Series([NaT] * 60 + [None] * 60, dtype="object"),
10941094
Series([NaT] * 120, dtype="datetime64[ns]"),
10951095
),
10961096
(Series([None] * 20), Series([NaT] * 20, dtype="datetime64[ns]")),

requirements-dev.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ cpplint
6868
flake8==5.0.4
6969
flake8-bugbear==22.7.1
7070
isort>=5.2.1
71-
mypy==0.971
71+
mypy==0.981
7272
pre-commit>=2.15.0
7373
pycodestyle
7474
pyupgrade

0 commit comments

Comments
 (0)