Skip to content

Commit 0e9589d

Browse files
gwromeWillAyd
authored andcommitted
Misc Type Annotation fixes (#26372)
* Add type annotations to io.pytables * Start correcting mypy errors in core.window * Complete misc type fixes * Fix import sort on test_groupby.py * Update after code reviews * Whatsnew, second rev indexing, window and pytables * Annotate Set in core.window * Define attributes as List[str]
1 parent ebe6b91 commit 0e9589d

File tree

12 files changed

+34
-57
lines changed

12 files changed

+34
-57
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ Other API Changes
249249
- Comparing :class:`Timestamp` with unsupported objects now returns :py:obj:`NotImplemented` instead of raising ``TypeError``. This implies that unsupported rich comparisons are delegated to the other object, and are now consistent with Python 3 behavior for ``datetime`` objects (:issue:`24011`)
250250
- Bug in :meth:`DatetimeIndex.snap` which didn't preserving the ``name`` of the input :class:`Index` (:issue:`25575`)
251251
- The ``arg`` argument in :meth:`pandas.core.groupby.DataFrameGroupBy.agg` has been renamed to ``func`` (:issue:`26089`)
252+
- The ``arg`` argument in :meth:`pandas.core.window._Window.aggregate` has been renamed to ``func`` (:issue:`26372`)
252253

253254
.. _whatsnew_0250.deprecations:
254255

mypy.ini

+1-34
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,4 @@ ignore_errors=True
1515
ignore_errors=True
1616

1717
[mypy-pandas.core.indexes.timedeltas]
18-
ignore_errors=True
19-
20-
[mypy-pandas.core.indexing]
21-
ignore_errors=True
22-
23-
[mypy-pandas.core.internals.blocks]
24-
ignore_errors=True
25-
26-
[mypy-pandas.core.panel]
27-
ignore_errors=True
28-
29-
[mypy-pandas.core.reshape.merge]
30-
ignore_errors=True
31-
32-
[mypy-pandas.core.reshape.reshape]
33-
ignore_errors=True
34-
35-
[mypy-pandas.core.series]
36-
ignore_errors=True
37-
38-
[mypy-pandas.core.util.hashing]
39-
ignore_errors=True
40-
41-
[mypy-pandas.core.window]
42-
ignore_errors=True
43-
44-
[mypy-pandas.io.pytables]
45-
ignore_errors=True
46-
47-
[mypy-pandas.util._doctools]
48-
ignore_errors=True
49-
50-
[mypy-pandas.util.testing]
51-
ignore_errors=True
18+
ignore_errors=True

pandas/core/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1569,5 +1569,5 @@ def duplicated(self, keep='first'):
15691569
# ----------------------------------------------------------------------
15701570
# abstracts
15711571

1572-
def _update_inplace(self, result, **kwargs):
1572+
def _update_inplace(self, result, verify_is_copy=True, **kwargs):
15731573
raise AbstractMethodError(self)

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import operator
77
import pickle
88
from textwrap import dedent
9-
from typing import FrozenSet, List, Set
9+
from typing import Callable, FrozenSet, List, Set
1010
import warnings
1111
import weakref
1212

@@ -3680,7 +3680,7 @@ class animal locomotion
36803680
result._set_is_copy(self, copy=not result._is_view)
36813681
return result
36823682

3683-
_xs = xs
3683+
_xs = xs # type: Callable
36843684

36853685
def select(self, crit, axis=0):
36863686
"""

pandas/core/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class IndexingError(Exception):
8888

8989

9090
class _NDFrameIndexer(_NDFrameIndexerBase):
91-
_valid_types = None
92-
_exception = KeyError
91+
_valid_types = None # type: str
92+
_exception = Exception
9393
axis = None
9494

9595
def __call__(self, axis=None):

pandas/core/internals/blocks.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import numpy as np
99

10-
from pandas._libs import internals as libinternals, lib, tslib, tslibs
10+
from pandas._libs import lib, tslib, tslibs
11+
import pandas._libs.internals as libinternals
1112
from pandas._libs.tslibs import Timedelta, conversion, is_null_datetimelike
1213
from pandas.util._validators import validate_bool_kwarg
1314

@@ -2050,12 +2051,15 @@ def get_values(self, dtype=None):
20502051
class DatetimeBlock(DatetimeLikeBlockMixin, Block):
20512052
__slots__ = ()
20522053
is_datetime = True
2053-
_can_hold_na = True
20542054

20552055
def __init__(self, values, placement, ndim=None):
20562056
values = self._maybe_coerce_values(values)
20572057
super().__init__(values, placement=placement, ndim=ndim)
20582058

2059+
@property
2060+
def _can_hold_na(self):
2061+
return True
2062+
20592063
def _maybe_coerce_values(self, values):
20602064
"""Input validation for values passed to __init__. Ensure that
20612065
we have datetime64ns, coercing if necessary.

pandas/core/reshape/merge.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import numpy as np
1010

11-
from pandas._libs import hashtable as libhashtable, join as libjoin, lib
11+
from pandas._libs import hashtable as libhashtable, lib
12+
import pandas._libs.join as libjoin
1213
from pandas.errors import MergeError
1314
from pandas.util._decorators import Appender, Substitution
1415

pandas/core/reshape/reshape.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import numpy as np
55

6-
from pandas._libs import algos as _algos, reshape as _reshape
6+
import pandas._libs.algos as _algos
7+
import pandas._libs.reshape as _reshape
78
from pandas._libs.sparse import IntIndex
89

910
from pandas.core.dtypes.cast import maybe_promote

pandas/core/util/hashing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
import numpy as np
77

8-
from pandas._libs import hashing, tslibs
8+
import pandas._libs.hashing as hashing
9+
import pandas._libs.tslibs as tslibs
910

1011
from pandas.core.dtypes.cast import infer_dtype_from_scalar
1112
from pandas.core.dtypes.common import (

pandas/core/window.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from collections import defaultdict
66
from datetime import timedelta
77
from textwrap import dedent
8+
from typing import Set
89
import warnings
910

1011
import numpy as np
@@ -42,7 +43,7 @@
4243
class _Window(PandasObject, SelectionMixin):
4344
_attributes = ['window', 'min_periods', 'center', 'win_type',
4445
'axis', 'on', 'closed']
45-
exclusions = set()
46+
exclusions = set() # type: Set[str]
4647

4748
def __init__(self, obj, window=None, min_periods=None,
4849
center=False, win_type=None, axis=0, on=None, closed=None,
@@ -305,10 +306,10 @@ def _center_window(self, result, window):
305306
result = np.copy(result[tuple(lead_indexer)])
306307
return result
307308

308-
def aggregate(self, arg, *args, **kwargs):
309-
result, how = self._aggregate(arg, *args, **kwargs)
309+
def aggregate(self, func, *args, **kwargs):
310+
result, how = self._aggregate(func, *args, **kwargs)
310311
if result is None:
311-
return self.apply(arg, raw=False, args=args, kwargs=kwargs)
312+
return self.apply(func, raw=False, args=args, kwargs=kwargs)
312313
return result
313314

314315
agg = aggregate
@@ -788,7 +789,7 @@ def __init__(self, obj, *args, **kwargs):
788789
corr = GroupByMixin._dispatch('corr', other=None, pairwise=None)
789790
cov = GroupByMixin._dispatch('cov', other=None, pairwise=None)
790791

791-
def _apply(self, func, name, window=None, center=None,
792+
def _apply(self, func, name=None, window=None, center=None,
792793
check_minp=None, **kwargs):
793794
"""
794795
Dispatch to apply; we are stripping all of the _apply kwargs and

pandas/io/pytables.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import re
1212
import time
13+
from typing import List, Optional, Type, Union
1314
import warnings
1415

1516
import numpy as np
@@ -2297,9 +2298,9 @@ class Fixed(StringMixin):
22972298
parent : my parent HDFStore
22982299
group : the group node where the table resides
22992300
"""
2300-
pandas_kind = None
2301-
obj_type = None
2302-
ndim = None
2301+
pandas_kind = None # type: str
2302+
obj_type = None # type: Type[Union[DataFrame, Series]]
2303+
ndim = None # type: int
23032304
is_table = False
23042305

23052306
def __init__(self, parent, group, encoding=None, errors='strict',
@@ -2459,7 +2460,7 @@ class GenericFixed(Fixed):
24592460
""" a generified fixed version """
24602461
_index_type_map = {DatetimeIndex: 'datetime', PeriodIndex: 'period'}
24612462
_reverse_index_map = {v: k for k, v in _index_type_map.items()}
2462-
attributes = []
2463+
attributes = [] # type: List[str]
24632464

24642465
# indexer helpders
24652466
def _class_to_alias(self, cls):
@@ -3052,7 +3053,7 @@ class Table(Fixed):
30523053
30533054
"""
30543055
pandas_kind = 'wide_table'
3055-
table_type = None
3056+
table_type = None # type: str
30563057
levels = 1
30573058
is_table = True
30583059
is_shape_reversed = False
@@ -3873,7 +3874,7 @@ class LegacyTable(Table):
38733874
IndexCol(name='index', axis=1, pos=0),
38743875
IndexCol(name='column', axis=2, pos=1, index_kind='columns_kind'),
38753876
DataCol(name='fields', cname='values', kind_attr='fields', pos=2)
3876-
]
3877+
] # type: Optional[List[IndexCol]]
38773878
table_type = 'legacy'
38783879
ndim = 3
38793880

@@ -4126,7 +4127,7 @@ class AppendableFrameTable(AppendableTable):
41264127
pandas_kind = 'frame_table'
41274128
table_type = 'appendable_frame'
41284129
ndim = 2
4129-
obj_type = DataFrame
4130+
obj_type = DataFrame # type: Type[Union[DataFrame, Series]]
41304131

41314132
@property
41324133
def is_transposed(self):

pandas/util/testing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pandas._config.localization import ( # noqa:F401
2222
can_set_locale, get_locales, set_locale)
2323

24-
from pandas._libs import testing as _testing
24+
import pandas._libs.testing as _testing
2525
from pandas.compat import raise_with_traceback
2626

2727
from pandas.core.dtypes.common import (

0 commit comments

Comments
 (0)