Skip to content

Commit 3e3bb90

Browse files
TYP: CallableDynamicDoc (#46786)
* TYP: CallableDynamicDoc * using cast * tighten NDFrameIndexerBase * dtype and clarification for _IndexingMixinT Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 1812c6e commit 3e3bb90

File tree

6 files changed

+82
-11
lines changed

6 files changed

+82
-11
lines changed

pandas/_config/config.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@
5858
from typing import (
5959
Any,
6060
Callable,
61+
Generic,
6162
Iterable,
6263
NamedTuple,
6364
cast,
6465
)
6566
import warnings
6667

67-
from pandas._typing import F
68+
from pandas._typing import (
69+
F,
70+
T,
71+
)
6872

6973

7074
class DeprecatedOption(NamedTuple):
@@ -124,7 +128,7 @@ def _get_single_key(pat: str, silent: bool) -> str:
124128
return key
125129

126130

127-
def _get_option(pat: str, silent: bool = False):
131+
def _get_option(pat: str, silent: bool = False) -> Any:
128132
key = _get_single_key(pat, silent)
129133

130134
# walk the nested dict
@@ -164,7 +168,7 @@ def _set_option(*args, **kwargs) -> None:
164168
o.cb(key)
165169

166170

167-
def _describe_option(pat: str = "", _print_desc: bool = True):
171+
def _describe_option(pat: str = "", _print_desc: bool = True) -> str | None:
168172

169173
keys = _select_options(pat)
170174
if len(keys) == 0:
@@ -174,8 +178,8 @@ def _describe_option(pat: str = "", _print_desc: bool = True):
174178

175179
if _print_desc:
176180
print(s)
177-
else:
178-
return s
181+
return None
182+
return s
179183

180184

181185
def _reset_option(pat: str, silent: bool = False) -> None:
@@ -247,16 +251,17 @@ def __dir__(self) -> Iterable[str]:
247251
# of options, and option descriptions.
248252

249253

250-
class CallableDynamicDoc:
251-
def __init__(self, func, doc_tmpl) -> None:
254+
class CallableDynamicDoc(Generic[T]):
255+
def __init__(self, func: Callable[..., T], doc_tmpl: str) -> None:
252256
self.__doc_tmpl__ = doc_tmpl
253257
self.__func__ = func
254258

255-
def __call__(self, *args, **kwds):
259+
def __call__(self, *args, **kwds) -> T:
256260
return self.__func__(*args, **kwds)
257261

262+
# error: Signature of "__doc__" incompatible with supertype "object"
258263
@property
259-
def __doc__(self):
264+
def __doc__(self) -> str: # type: ignore[override]
260265
opts_desc = _describe_option("all", _print_desc=False)
261266
opts_list = pp_options_list(list(_registered_options.keys()))
262267
return self.__doc_tmpl__.format(opts_desc=opts_desc, opts_list=opts_list)

pandas/_libs/indexing.pyi

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import (
2+
Generic,
3+
TypeVar,
4+
)
5+
6+
from pandas.core.indexing import IndexingMixin
7+
8+
_IndexingMixinT = TypeVar("_IndexingMixinT", bound=IndexingMixin)
9+
10+
class NDFrameIndexerBase(Generic[_IndexingMixinT]):
11+
name: str
12+
# in practise obj is either a DataFrame or a Series
13+
obj: _IndexingMixinT
14+
15+
def __init__(self, name: str, obj: _IndexingMixinT) -> None: ...
16+
@property
17+
def ndim(self) -> int: ...

pandas/_libs/json.pyi

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import (
2+
Any,
3+
Callable,
4+
)
5+
6+
def dumps(
7+
obj: Any,
8+
ensure_ascii: bool = ...,
9+
double_precision: int = ...,
10+
indent: int = ...,
11+
orient: str = ...,
12+
date_unit: str = ...,
13+
iso_dates: bool = ...,
14+
default_handler: None
15+
| Callable[[Any], str | int | float | bool | list | dict | None] = ...,
16+
) -> str: ...
17+
def loads(
18+
s: str,
19+
precise_float: bool = ...,
20+
numpy: bool = ...,
21+
dtype: None = ...,
22+
labelled: bool = ...,
23+
) -> Any: ...

pandas/_libs/reduction.pyi

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from typing import Any
2+
3+
import numpy as np
4+
5+
from pandas._typing import ExtensionDtype
6+
7+
def check_result_array(obj: object, dtype: np.dtype | ExtensionDtype) -> None: ...
8+
def extract_result(res: object) -> Any: ...

pandas/core/indexing.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
TYPE_CHECKING,
66
Hashable,
77
Sequence,
8+
cast,
89
final,
910
)
1011
import warnings
@@ -629,6 +630,9 @@ class _LocationIndexer(NDFrameIndexerBase):
629630
_valid_types: str
630631
axis: int | None = None
631632

633+
# sub-classes need to set _takeable
634+
_takeable: bool
635+
632636
@final
633637
def __call__(self, axis=None):
634638
# we need to return a copy of ourselves
@@ -924,7 +928,9 @@ def _getitem_lowerdim(self, tup: tuple):
924928
# is equivalent.
925929
# (see the other place where we call _handle_lowerdim_multi_index_axis0)
926930
with suppress(IndexingError):
927-
return self._handle_lowerdim_multi_index_axis0(tup)
931+
# error "_LocationIndexer" has no attribute
932+
# "_handle_lowerdim_multi_index_axis0"
933+
return cast(_LocIndexer, self)._handle_lowerdim_multi_index_axis0(tup)
928934

929935
tup = self._validate_key_length(tup)
930936

@@ -980,7 +986,11 @@ def _getitem_nested_tuple(self, tup: tuple):
980986
# DataFrame, IndexingError is not raised when slice(None,None,None)
981987
# with one row.
982988
with suppress(IndexingError):
983-
return self._handle_lowerdim_multi_index_axis0(tup)
989+
# error "_LocationIndexer" has no attribute
990+
# "_handle_lowerdim_multi_index_axis0"
991+
return cast(_LocIndexer, self)._handle_lowerdim_multi_index_axis0(
992+
tup
993+
)
984994
elif isinstance(self.obj, ABCSeries) and any(
985995
isinstance(k, tuple) for k in tup
986996
):
@@ -2303,6 +2313,9 @@ class _ScalarAccessIndexer(NDFrameIndexerBase):
23032313
Access scalars quickly.
23042314
"""
23052315

2316+
# sub-classes need to set _takeable
2317+
_takeable: bool
2318+
23062319
def _convert_key(self, key):
23072320
raise AbstractMethodError(self)
23082321

pandas/io/sas/_sas.pyi

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from pandas.io.sas.sas7bdat import SAS7BDATReader
2+
3+
class Parser:
4+
def __init__(self, parser: SAS7BDATReader) -> None: ...
5+
def read(self, nrows: int) -> None: ...

0 commit comments

Comments
 (0)